POJ-2528
Problem POJ-2528
https://vjudge.net/problem/POJ-2528
注意到数据范围,因此需要进行离散化。离散化需要注意,在距离大于1的两点需要在中间增加一点。
其他就是普通的线段树。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 7;
int n;
int arrL[maxn],arrR[maxn],tot,discreteArr[maxn << 3], ans;
int segmentTree[maxn << 2],lazyMark[maxn << 2];
bool judge[maxn << 3];
void init(){
memset(judge,false,sizeof judge);
memset(segmentTree,0,sizeof segmentTree);
memset(lazyMark,0,sizeof lazyMark);
memset(discreteArr,0,sizeof discreteArr);
tot = ans = 0;
}
void pushUp(int rt){
}
void pushDown(int rt){
if(lazyMark[rt] != 0){
lazyMark[rt << 1] = lazyMark[rt];
lazyMark[rt << 1 | 1] = lazyMark[rt];
segmentTree[rt << 1] = lazyMark[rt];
segmentTree[rt << 1 | 1] = lazyMark[rt];
lazyMark[rt] = 0;
}
}
void modify(int l,int r,int rt,int L,int R, int x){
if(L <= l && R >= r){
segmentTree[rt] = x;
lazyMark[rt] = x;
return;
}
pushDown(rt);
int mid= (l + r) >> 1;
if(L <= mid && R >= l) modify(l, mid, rt << 1, L, R, x);
if(L <= r && R >= mid + 1) modify(mid + 1, r, rt << 1 | 1,L, R, x);
}
int query(int x,int rt,int l,int r){
if(l == r){
return segmentTree[rt];
}
pushDown(rt);
int mid = (l + r) >> 1;
if(x <= mid ) return query(x,rt << 1,l,mid);
else return query(x,rt << 1 | 1,mid + 1,r);
}
int f(int x){
int ret = lower_bound(discreteArr,discreteArr + tot,x) - discreteArr + 1;
return ret;
}
void discretize(){
for(int i = 0;i < n; ++i){
discreteArr[tot++] = arrL[i];
discreteArr[tot++] = arrR[i];
}
sort(discreteArr,discreteArr + tot);
tot = unique(discreteArr, discreteArr + tot) - discreteArr;
int temp = tot;
for(int i = 1;i < temp ; ++i){
if(discreteArr[i] - discreteArr[i-1] > 1){
discreteArr[tot++] = discreteArr[i-1] + 1;
}
}
sort(discreteArr,discreteArr + tot);
}
int main() {
int T;
cin >> T;
while (T--) {
init();
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arrL[i] >> arrR[i];
}
discretize();
for (int i = 0; i < n; ++i) {
// cout << f(arrL[i]) << ' ' << f(arrR[i]) << endl;
modify(1, tot, 1, f(arrL[i]), f(arrR[i]), i);
}
for (int i = 1; i <= tot; ++i) {
int temp = query(i, 1, 1, tot);
if (judge[temp]) continue;
judge[temp] = true;
ans++;
}
cout << ans << endl;
}
}