void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j)
{
do i ++ ; while (q[i] < x);
do j -- ; while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j), quick_sort(q, j + 1, r);
}
注意边界问题:
递归时
如果 quick_sort(q, l, j) 与 quick_sort(q, j+1, r)
注意 x 不等于 q[ r ] 否则容易死循环,例 数组q为 0,1.死循环
同理,
如果 quick_sort(q, l, i-1) 与 quick_sort(q, i, r)
注意 x 不等于 q[ l ] 否则容易死循环,例 数组q为 0,1.死循环
#include
using namespace std;
const int N = 100010;
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, k = q[l + r >> 1];
while (i < j)
{
do i++; while (q[i] < k);
do j--; while (q[j] > k);
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
}
int main()
{
int n;
scanf("%d", &n);
int i, q[N];
for (i = 0; i < n; i++)
scanf("%d", &q[i]);
quick_sort(q, 0, n - 1);
for (i = 0; i < n; i++)
printf("%d ", q[i]);
return 0;
}
//虽然可以全部求出后,直接得出答案,但我们可以优化一下 ,提高效率
//每次一分为二后, 只需管 k 所在的那一半即可(优化效率)
//重点是从小到大的第 k 个数 ,因此核心是数组第 k 个数的下标
//quick_sort()的 k 代表当前数组的第 k 个位置,所以需要当前数组左侧长(右侧长 - 左侧长) 与其作比较
//如果当前数组左侧长大于等于 k ,quick_sort()的 k 还是原来的 k,因为该次递归时,相对位置不变
//如果当前数组左侧长小于 k ,quick_sort()的 k 为 k - len,因为该次递归时,相对位置变了
//最后即可得出 q[k]的值
#include
using namespace std;
const int N = 100010;
void quick_sort(int q[], int l, int r, int k)
{
if(l >= r) return;
int i = l - 1, j = r + 1;
int x = q[l + r >> 1];
while(i < j)
{
do i++; while(q[i] < x);
do j--; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
int len = j - l + 1;
if(len >= k) //如果位于左侧
quick_sort(q, l, j, k); // k 相对位置不变
else //如果位于右侧
quick_sort(q, j + 1 , r, k - len); // k 相对位置变了
}
int main()
{
int q[N];
int i, n , k;
scanf("%d%d",&n, &k);
for(i = 0; i < n; i++)
scanf("%d",&q[i]);
quick_sort(q, 0, n - 1, k);
printf("%d\n",q[k - 1]);
return 0;
}
void merge_sort(int q[], int l, int r)
{
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid);
merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r)
if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
else tmp[k ++ ] = q[j ++ ];
while (i <= mid) tmp[k ++ ] = q[i ++ ];
while (j <= r) tmp[k ++ ] = q[j ++ ];
for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
}
#include
using namespace std;
#define N 100010
int q[N],t[N];
void merge_sort(int q[],int l, int r)
{
if(l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid);
merge_sort(q, mid + 1, r);
int k = 0, i = l,j = mid + 1;
while(i <= mid && j <= r)
{
if(q[i] <= q[j]) t[k++] = q[i++];
else t[k++] = q[j++];
}
while(i <= mid) t[k++] = q[i++];
while(j <= r) t[k++] = q[j++];
for(i = l, j = 0; i <= r; i++,j++)
q[i] = t[j];
}
int main()
{
int i, n;
scanf("%d",&n);
for(i = 0; i < n;i++)
scanf("%d",&q[i]);
merge_sort(q, 0, n - 1);
for(i = 0; i < n; i++)
printf("%d ",q[i]);
return 0;
}
//1. 暴力解法可以,但最坏情况,n*(n-1)大约为5*10e9
//2. 所以可以考虑归并
//3. 仔细思考归并算法的过程
//4. 在合并时,两个子序列为有序序列,再联系一下题目要求,有思路了
//5. q[i…mid]为有序序列,如果q[i]>q[j],那么q[i…mid]是不是都大于q[j],所以 num += mid - i + 1
#include
using namespace std;
#define N 100010
int q[N];
int t[N];
long long num = 0;
void merge_sort(int q[], int l, int r)
{
if(l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid);
merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while(i <= mid && j <=r)
{
if(q[i] <= q[j]) t[k++] = q[i++];
else
{
t[k++] = q[j++];
num += mid - i + 1;
}
}
while(i <= mid) t[k++] = q[i++];
while(j <= r) t[k++]= q[j++];
for(i = l, j = 0; i <= r; i++, j++)
q[i] = t[j];
};
int main()
{
int i,n;
scanf("%d",&n);
for(i = 0; i < n; i++)
scanf("%d",&q[i]);
merge_sort(q, 0 , n - 1);
printf("%lld\n",num);
return 0;
}
分成满足某个性质及不满足该性质的两个区间,再用二分法找到区间边界的点
整数模板
bool check(int x) {/* ... */} // 检查x是否满足某种性质
// 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用:
int bsearch_1(int l, int r)
{
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid; // check()判断mid是否满足性质
else l = mid + 1;
}
return l;
}
// 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用:
int bsearch_2(int l, int r)
{
while (l < r)
{
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
浮点数二分算法模板
bool check(double x) {/* ... */} // 检查x是否满足某种性质
double bsearch_3(double l, double r)
{
const double eps = 1e-6; // eps 表示精度,取决于题目对精度的要求
while (r - l > eps)
{
double mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
return l;
}
#include
using namespace std;
#define N 100010
int q[N];
int main()
{
int n, x, k, i;
scanf("%d%d",&n,&x);
for(i = 0; i < n; i++)
scanf("%d",&q[i]);
while(x--)
{
scanf("%d",&k);
i = 0;
int j = n - 1;
while(i < j)
{
int mid = i + j >> 1;
if(q[mid] >= k) j = mid;
else i = mid + 1;
}
if(q[j] != k) printf("-1 -1\n");
else
{
printf("%d ", j);
i = 0;j = n - 1;
while(i < j)
{
int mid = i + j + 1>> 1;
if(q[mid] <= k) i = mid;
else j = mid - 1;
}
printf("%d\n", j);
}
}
return 0;
}
注意,一般要求保留 x 为小数时,计算时,精确到 1e - (x+2) ,即多两位,确保精确度。
#include
using namespace std;
#define N 1e-8
int main()
{
double n, l, r;
scanf("%lf",&n);
l = -10000, r = 10000;
while(r - l >N)
{
double mid = (l + r)/2;
if(mid * mid * mid > n) r = mid;
else l = mid;
}
printf("%.6f",l);
return 0;
}
基本概念:高精度算法(High Accuracy Algorithm)是处理大数字的数学计算方法。在一般的科学计算中,会经常算到小数点后几百位或者更多,当然也可能是几千亿几百亿的大数字。一般这类数字我们统称为高精度数,高精度算法是用计算机对于超大数据的一种模拟加,减,乘,除,乘方,阶乘,开方等运算。对于非常庞大的数字无法在计算机中正常存储,于是,将这个数字拆开,拆成一位一位的,或者是四位四位的存储到一个数组中, 用一个数组去表示一个数字,这样这个数字就被称为是高精度数。高精度算法就是能处理高精度数各种运算的算法,但又因其特殊性,故从普通数的算法中分离,自成一家。
注意点:
// C = A + B, A >= 0, B >= 0
vector<int> add(vector<int> &A, vector<int> &B)
{
if (A.size() < B.size()) return add(B, A);
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i ++ )
{
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(t);
return C;
}
#include
#include
using namespace std;
vector<int> add(vector<int> &A, vector<int> &B)
{
if(A.size() < B.size())
return add(B, A);
vector<int> C;
int t = 0, i;
for(i = 0; i < A.size(); i++)
{
t += A[i];
if(i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if(t) C.push_back(t);
return C;
}
int main()
{
string a, b;
cin >> a >> b;
vector<int> A, B, C;
int i;
for(i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for(i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
C = add(A, B);
for(i = C.size() - 1; i >= 0; i--)
printf("%d",C[i]);
return 0;
}
// C = A - B, 满足A >= B, A >= 0, B >= 0
vector<int> sub(vector<int> &A, vector<int> &B)
{
vector<int> C;
for (int i = 0, t = 0; i < A.size(); i ++ )
{
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
#include
#include
using namespace std;
bool cmp(vector<int> &A, vector<int> &B)
{
if(A.size() != B.size())
return A.size() > B.size();
int i;
for(i = A.size() - 1; i >= 0; i--)
{
if(A[i] != B[i])
return A[i] > B[i];
}
return true;
}
vector<int> sub(vector<int> &A, vector<int> &B)
{
vector<int> C;
int i, t = 0;
for(i = 0; i < A.size(); i++)
{
t = A[i] - t;
if(i < B.size()) t -= B[i];
C.push_back((t + 10)%10);
if(t < 0) t = 1;
else t = 0;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
int i;
string a, b;
vector<int> A, B, C;
cin >> a >> b;
for(i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for(i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
if(cmp(A, B))
C = sub(A, B);
else
{
C = sub(B, A);
printf("-");
}
for(i = C.size() - 1; i >= 0; i--)
printf("%d",C[i]);
return 0;
}
// C = A * b, A >= 0, b > 0
vector<int> mul(vector<int> &A, int b)
{
vector<int> C;
int t = 0;
for (int i = 0; i < A.size() || t; i ++ )
{
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
#include
#include
using namespace std;
vector<int> mul(vector<int> &A,int b)
{
vector<int> C;
int i, t = 0;
for(i = 0; i < A.size() || t; i++)
{
if(i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b;
vector<int> A,C;
cin >> a >> b;
int i;
for(i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
C = mul(A, b);
for(i = C.size() - 1; i >= 0; i--)
printf("%d",C[i]);
return 0;
}
// A / b = C ... r, A >= 0, b > 0
vector<int> div(vector<int> &A, int b, int &r)
{
vector<int> C;
r = 0;
for (int i = A.size() - 1; i >= 0; i -- )
{
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
#include
#include
#include
using namespace std;
vector<int> div(vector<int> &A, int b, int &r)
{
vector<int> C;
int i;
for(i = A.size() - 1, r = 0; i >=0; i--)
{
r = r * 10 + A[i];
C.push_back(r/b);
r %= b;
}
reverse(C.begin(), C.end());
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b, r , i;
vector<int> A, C;
cin >> a >> b;
for(i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
C = div(A, b, r);
for(i = C.size() - 1; i >= 0; i-- )
cout << C[i];
cout << endl << r << endl;
return 0;
}
S[i] = a[1] + a[2] + ... a[i]
a[l] + ... + a[r] = S[r] - S[l - 1]
#include
using namespace std;
#define N 100010
int nums[N];
int s[N];
int main()
{
int n, m, i;
cin >> n >> m;
for(i = 1; i <=n; i++)
scanf("%d",&nums[i]);
for(i = 1; i <=n; i++)
s[i] = s[i - 1] + nums[i];
while(m--)
{
int l, r;
cin >> l >> r;
cout << s[r] - s[l - 1]<<endl;
}
return 0;
}
S[i, j] = 第i行j列格子左上部分所有元素的和
以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵的和为:
S[x2, y2] - S[x1 - 1, y2] - S[x2, y1 - 1] + S[x1 - 1, y1 - 1]
#include
using namespace std;
#define N 1010
int a[N][N];
int s[N][N];
int main()
{
int n, m, q;
int i, j;
cin >> n >> m >> q;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
scanf("%d",&a[i][j]);
s[i][j] = s[i - 1][j] + s[i][j - 1] + a[i][j] - s[i - 1][j - 1];
}
while(q--)
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >>y2;
cout << s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1] << endl;
}
return 0;
}
数组的首元素为0,不用它,因为需用到 b[1] += b[0] .
给区间[l, r]中的每个数加上c:B[l] += c, B[r + 1] -= c
#include
using namespace std;
#define N 100010
int a[N]; //前缀和
int b[N]; //差分
void insert(int l, int r, int c)
{
b[l] += c;
b[r + 1] -= c;
}
int main()
{
int n, m;
int i;
scanf("%d%d",&n, &m);
for(i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
insert(i, i, a[i]);
}
while(m--)
{
int l, r, c;
cin >> l >> r >> c;
insert(l, r, c);
}
for(i = 1; i <= n; i++)
{
b[i] +=b[i - 1]; //等价于 a[i] = a[i - 1] + b[i],输出 a[i] 就行
cout << b[i] << ' ';
}
cout << endl;
return 0;
}
数组的首元素为0,不用它,因为 b[1][1] 需用到 b[0][0] .
给以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵中的所有元素加上c:
S[x1, y1] += c, S[x2 + 1, y1] -= c, S[x1, y2 + 1] -= c, S[x2 + 1, y2 + 1] += c
#include
using namespace std;
#define N 1010
int a[N][N]; //前缀和
int b[N][N]; //差分
void insert(int x1, int y1, int x2, int y2, int c)
{
b[x1][y1] += c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y1] -= c;
b[x2 + 1][y2 + 1] += c;
}
int main()
{
int n, m, q;
int i, j;
scanf("%d%d%d",&n, &m, &q);
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
scanf("%d",&a[i][j]);
insert(i, j, i, j, a[i][j]);
}
while(q--)
{
int x1, y1, x2, y2, c;
cin >> x1 >> y1 >> x2 >> y2 >> c;
insert(x1, y1, x2, y2, c);
}
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
cout << b[i][j] << ' ';
}
cout << endl;
}
return 0;
}
核心:优化时间复杂度
特点:一般而言,两个指针单向移动,以便达到优化时间复杂度的目的
//双指针算法 —— 模板题 AcWIng 799. 最长连续不重复子序列, AcWing 800. 数组元素的目标和
for (int i = 0, j = 0; i < n; i ++ )
{
while (j < i && check(i, j)) j ++ ;
// 具体问题的逻辑
}
常见问题分类:
(1) 对于一个序列,用两个指针维护一段区间
(2) 对于两个序列,维护某种次序,比如归并排序中合并两个有序序列的操作
#include
using namespace std;
#define N 100010
int res;
int a[N]; //整数序列
int s[N]; //用来统计重复序列
int main()
{
int n;
int i, j = 0;
cin >> n;
for(i = 0; i < n; i++)
cin >> a[i];
for(i = 0; i < n; i++)
{
s[a[i]]++;
while(j < i && s[a[i]] > 1)
{
s[a[j]] --;
j++;
}
res = max(res, i - j + 1);
}
cout << res << endl;
return 0;
}
#include
using namespace std;
#define N 100010
int a[N];
int b[N];
int main()
{
int n, m, x;
cin >> n >> m >> x;
int i, j;
for(i = 0; i < n ;i++) cin >> a[i];
for(j = 0; j < m ;j++) cin >> b[j];
for(i = 0, j = m - 1; i < n; i++)
{
while(j > 0 && a[i] + b[j] > x) j--;
if(a[i] + b[j] == x)
{
cout << i << ' ' << j << endl;
break;
}
}
return 0;
}
#include
using namespace std;
#define N 100100
int a[N];
int b[N];
int main()
{
int i, j;
int n, m;
cin >> n >> m;
for(i = 0; i < n; i++) cin >> a[i];
for(i = 0; i < m; i++) cin >> b[i];
for(i = 0, j = 0; i < n && j < m; j++)
{
if( a[i] == b[j]) i++;
}
if(i == n) cout << "Yes" <<endl;
else cout << "No" << endl;
return 0;
}
注:x 的负数等于 x 的补码,补码等于反码加一
位运算 —— 模板题 AcWing 801. 二进制中1的个数
求n的第k位(二进制)数字: n >> k & 1
返回n的最后一位1(二进制):lowbit(n) = n & -n
#include
using namespace std;
#define N 100010
int a[N];
int b[N];
int lowbit(int n)
{
return n & -n;
}
int main()
{
int n;
int i, j;
cin >> n;
for(i = 0; i < n; i++) cin >> a[i];
for(i = 0; i < n; i++)
{
while(a[i])
{
a[i] = a[i] - lowbit(a[i]);
b[i]++;
}
}
for(i = 0; i < n; i++)
cout << b[i] << ' ';
return 0;
}
离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。例如:
原数据:1,999,100000,15;处理后:1,3,4,2;
原数据:{100,200},{20,50000},{1,400};
处理后:{3,4},{2,6},{1,5};
离散化 —— 模板题 AcWing 802. 区间和
vector<int> alls; // 存储所有待离散化的值
sort(alls.begin(), alls.end()); // 将所有值排序
alls.erase(unique(alls.begin(), alls.end()), alls.end()); // 去掉重复元素
// 二分求出x对应的离散化的值
int find(int x) // 找到第一个大于等于x的位置
{
int l = 0, r = alls.size() - 1;
while (l < r)
{
int mid = l + r >> 1;
if (alls[mid] >= x) r = mid;
else l = mid + 1;
}
return r + 1; // 映射到1, 2, ...n
}
c11标准 可以用 for(auto item:a).
c98标准不允许 auto 及 for(auto item:a) 操作
//a 为 vector
//c98标准可以用下面的操作思想 替代 c11标准的for(auto item:a).
vector<int>::iterator b = a.begin();
for(; b != a.end(); b++)
{
cout << *b << endl;
}
vs2022 c11标准 展示
#include
#include
#include
using namespace std;
#define N 300010
typedef pair<int, int> PII;
int a[N], s[N];
vector<int> alls;
vector<PII> add, query;
int find(int x)
{
int l = 0;
int r = alls.size() - 1;
while (l < r)
{
int mid = r + l >> 1;
if (alls[mid] >= x) r = mid;
else l = mid + 1;
}
return l + 1;
}
int main()
{
int n, m;
cin >> n >> m;
int i;
while (n--)
{
int x, c;
cin >> x >> c;
add.push_back({ x, c });
alls.push_back(x);
}
while (m--)
{
int l, r;
cin >> l >> r;
query.push_back({ l, r });
alls.push_back(l);
alls.push_back(r);
}
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(), alls.end()), alls.end());
for (auto item : add)
{
int x = find(item.first);
a[x] += item.second;
}
for (i = 1; i <= alls.size(); i++)
{
s[i] = s[i - 1] + a[i];
}
for (auto item : query)
{
int l = find(item.first);
int r = find(item.second);
cout << s[r] - s[l - 1] << endl;
}
return 0;
}
devc++ c98标准展示
#include
#include
#include
using namespace std;
#define N 300010
typedef pair<int, int> PII;
int a[N], s[N];
vector<int> alls;
vector<PII> add, query;
int find(int x)
{
int l = 0;
int r = alls.size() - 1;
while (l < r)
{
int mid = r + l >> 1;
if (alls[mid] >= x) r = mid;
else l = mid + 1;
}
return l + 1;
}
int main()
{
int n, m;
cin >> n >> m;
int i;
while (n--)
{
int x, c;
cin >> x >> c;
add.push_back({ x, c });
alls.push_back(x);
}
while (m--)
{
int l, r;
cin >> l >> r;
query.push_back({ l, r });
alls.push_back(l);
alls.push_back(r);
}
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(), alls.end()), alls.end());
vector<PII>::iterator item = add.begin();
for (; item != add.end(); item++ )
{
int x = find(item->first);
a[x] += item->second;
}
for (i = 1; i <= alls.size(); i++)
{
s[i] = s[i - 1] + a[i];
}
vector<PII>::iterator item2 = query.begin();
for (; item2 != query.end(); item2++ )
{
int l = find(item2->first);
int r = find(item2->second);
cout << s[r] - s[l - 1] << endl;
}
return 0;
}
vs2022 c11标准 展示
模板是本区间与下一个区间存在空隙时,将本区间放入结果,所以最后一个区间也要加到结果里去。
区间合并 —— 模板题 AcWing 803. 区间合并
// 将所有存在交集的区间合并
void merge(vector<PII> &segs)
{
vector<PII> res;
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
for (auto seg : segs)
if (ed < seg.first)
{
if (st != -2e9) res.push_back({st, ed});
st = seg.first, ed = seg.second;
}
else ed = max(ed, seg.second);
if (st != -2e9) res.push_back({st, ed});
segs = res;
}
devc++ c98标准 展示
如果你的编译器是c11标准的,将我的merge函数换成模板中的merge函数就ok了
#include
#include
#include
using namespace std;
typedef pair<int, int> PII;
vector<PII> segs;
void merge(vector<PII> &segs)
{
vector<PII> res;
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
vector<PII>::iterator seg = segs.begin();
for(; seg != segs.end(); seg++)
{
if(ed < seg->first)
{
if(st != -2e9)
res.push_back({st, ed});
st = seg->first;
ed = seg->second;
}
else
ed = max(ed, seg->second);
}
if(st != -2e9) res.push_back({st, ed});
segs = res;
}
int main()
{
int n;
int l, r;
int i;
cin >> n;
for(i = 0; i < n; i++)
{
cin >> l >> r;
segs.push_back({l,r});
}
merge(segs);
cout << segs.size() << endl;
return 0;
}