Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39795 | Accepted: 11552 |
Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
6
5
1 4
2 6
8 10
3 4
7 10
3
5 6
4 5
6 8
3
1 10
1 3
6 10
5
1 4
2 6
8 10
3 4
7 10
4
2 4
3 5
1 3
5 7
3
1 10
1 4
5 10
ans:
4
2
3
4
3
2
#include <stdio.h> #include <string.h> #include <algorithm> #define inf 10000002 #define maxn 10002 #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 using namespace std; bool tree[maxn << 4]; struct Node{ int l, r; } post[maxn]; int hash[inf], tmp[maxn << 1]; bool update(int left, int right, int l, int r, int rt) { if(tree[rt]) return false; if(left == l && right == r){ return tree[rt] = true; } bool rst, a, b; int mid = (l + r) >> 1; if(right <= mid) rst = update(left, right, lson); else if(left > mid) rst = update(left, right, rson); else { a = update(left, mid, lson); b = update(mid + 1, right, rson); rst = a || b; } tree[rt] = tree[rt << 1] && tree[rt << 1 | 1]; return rst; } int main() { //freopen("stdin.txt", "r", stdin); int t, n, i, id, num, count, ans; scanf("%d", &t); while(t--){ scanf("%d", &n); for(i = id = 0; i < n; ++i){ scanf("%d%d", &post[i].l, &post[i].r); tmp[id++] = post[i].l; tmp[id++] = post[i].r; } sort(tmp, tmp + id); num = unique(tmp, tmp + id) - tmp; for(i = count = 0; i < num; ++i){ hash[tmp[i]] = count; if(i < num - 1){ if(tmp[i] + 1 == tmp[i+1]) ++count; else count += 2; } } memset(tree, 0, sizeof(tree)); for(i = n - 1, ans = 0; i >= 0; --i){ if(update(hash[post[i].l], hash[post[i].r], 0, count, 1)) ++ans; } printf("%d\n", ans); } return 0; }
//#define DEBUG #include <stdio.h> #include <string.h> #include <algorithm> #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 #define maxn 10002 using std::sort; int hash[maxn << 2], vis[maxn << 2], visColor[maxn], ans; int tree[maxn << 4], ori[maxn << 1], idHash, idVis, idOri; int findHash(int n) { int left = 0, right = idHash - 1, mid; while(left <= right){ mid = (left + right) >> 1; if(n < hash[mid]) right = mid - 1; else if(n > hash[mid]) left = mid + 1; else return mid; } } void pushDown(int rt) { tree[rt << 1] = tree[rt << 1 | 1] = tree[rt]; tree[rt] = -1; } void build(int l, int r, int rt) { tree[rt] = -1; if(r == l) return; int mid = (l + r) >> 1; build(lson); build(rson); } void update(int left, int right, int val, int l, int r, int rt) { if(left == l && right == r){ tree[rt] = val; return; } if(tree[rt] != -1) pushDown(rt); int mid = (l + r) >> 1; if(right <= mid) update(left, right, val, lson); else if(left > mid) update(left, right, val, rson); else{ update(left, mid, val, lson); update(mid + 1, right, val, rson); } } void query(int l, int r, int rt) { if(tree[rt] != -1){ if(!visColor[tree[rt]]){ ++ans; visColor[tree[rt]] = 1; } return; } //最后延迟段必定覆盖所有ori区间点,因为映射的每个点都被用到 if(l == r) return; int mid = (l + r) >> 1; query(lson); query(rson); } int main() { #ifdef DEBUG freopen("stdin.txt", "r", stdin); freopen("stdout.txt", "w", stdout); #endif int cas, n, a, b, i, temp; scanf("%d", &cas); while(cas--){ scanf("%d", &n); for(i = idVis = idOri = 0; i < n; ++i){ scanf("%d%d", &a, &b); ori[idOri++] = a; ori[idOri++] = b; vis[idVis++] = a; vis[idVis++] = b; } sort(vis, vis + idVis); //temporary for(temp = idVis, i = 1; i < temp; ++i) if(vis[i] - vis[i - 1] > 1) vis[idVis++] = vis[i] - 1; sort(vis, vis + idVis); hash[0] = vis[0]; for(i = idHash = 1; i < idVis; ++i) if(vis[i] != vis[i - 1]) hash[idHash++] = vis[i]; for(i = 0; i < idOri; ++i) ori[i] = findHash(ori[i]); build(0, idHash - 1, 1); //映射区间 memset(visColor, 0, sizeof(visColor)); for(i = 0; i < n; ++i){ update(ori[i << 1], ori[i << 1 | 1], i, 0, idHash - 1, 1); } ans = 0; query(0, idHash - 1, 1); printf("%d\n", ans); } return 0; }