标签:线段树
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 44910 | Accepted: 13059 |
Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
Source
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 20110; int xis[N], ans, cnt, ret; bool flag[N]; struct node { int l, r; int col; }tree[N << 2]; struct node2 { int l, r; }post[N]; int BinSearh(int target) { int l = 1, mid, r = ret; while (l <= r) { mid = (l + r) >> 1; if (xis[mid] > target) { r = mid - 1; } else if (xis[mid] < target) { l = mid + 1; } else { break; } } return mid; } void build(int p, int l, int r) { tree[p].l = l; tree[p].r = r; tree[p].col = 0; if (l == r) { return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); } void update(int p, int l, int r, int col) { if (l <= tree[p].l && tree[p].r <= r) { tree[p].col = col; return; } if (tree[p].col) { tree[p << 1].col = tree[p].col; tree[p << 1 | 1].col = tree[p].col; tree[p].col = 0; } int mid = (tree[p].l + tree[p].r) >> 1; if (l > mid) { update(p << 1 | 1, l, r, col); } else if (r <= mid) { update(p << 1, l, r, col); } else { update(p << 1, l, mid, col); update(p << 1 | 1, mid + 1, r, col); } } void query(int p) { if (tree[p].col) { if (!flag[tree[p].col]) { ans++; flag[tree[p].col] = 1; } return; } query(p << 1); query(p << 1 | 1); } int main() { int t, n; scanf("%d", &t); while (t--) { scanf("%d", &n); memset (flag, 0, sizeof(flag)); map <int, int> new_x; new_x.clear(); cnt = ans = 0; for (int i = 1; i <= n; ++i) { scanf("%d%d", &post[i].l, &post[i].r); xis[++cnt] = post[i].l; xis[++cnt] = post[i].r; } sort (xis + 1, xis + cnt + 1); ret = unique(xis + 1, xis + cnt + 1) - xis - 1; build(1, 1, ret); for (int i = 1; i <= n; ++i) { int l = BinSearh(post[i].l); int r = BinSearh(post[i].r); update(1, l, r, i); } query(1); printf("%d\n", ans); } return 0; }
标签:线段树
原文地址:http://blog.csdn.net/guard_mine/article/details/41650319