POJ2481 Cows
2014-07-29 10:55
218 查看
/*线段树*/ #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 100000 + 5; struct node { int s, e, id; node() {}; bool operator <(const node &a)const { if (a.s == s) return e > a.e; return s < a.s; } void clear() { s = e = id = 0; } }; int n, tree[maxn << 2], ans[maxn]; int x, y; node s[maxn]; int query(int l, int r, int root, int L, int R) { if (L <= l && R >= r) return tree[root]; int mid = (l + r) >> 1, tmp = 0; if (L <= mid) tmp += query(l, mid, root << 1, L, R); if (R > mid) tmp += query(mid + 1, r, root << 1 | 1, L, R); return tmp; } void update(int l, int r, int root, int pos) { ++tree[root]; if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) update(l, mid, root << 1, pos); else update(mid + 1, r, root << 1 | 1, pos); } int main() { while (cin >> n && n) { for (int i = 1; i <= n; ++i) { scanf("%d%d", &s[i].s, &s[i].e); s[i].id = i; } sort(s + 1, s + n + 1); memset(tree, 0, sizeof(tree)); for (int i = 1; i <= n; ++i) { if (s[i].s == s[i - 1].s && s[i].e == s[i - 1].e) { ans[s[i].id] = ans[s[i - 1].id]; } else { ans[s[i].id] = query(1, maxn, 1, s[i].e, maxn); } update(1, maxn, 1, s[i].e); } cout << ans[1]; for (int i = 2; i <= n; ++i) cout << " " << ans[i]; cout << endl; } return 0; }
相关文章推荐
- poj 2481 Cows
- POJ2481——Cows
- poj 2481 Cows(线段树)
- POJ 2481 Cows(树状数组)
- poj 2481 Cows
- poj2481 Cows(树状数组)
- POJ 2481 Cows
- poj 2481 Cows 树状数组+逆序对变形
- POJ 2481-Cows(BIT)
- poj 2481 Cows
- poj 2481 Cows
- POJ 2481 Cows
- POJ 2481 Cows(树状数组)
- POJ 2481 Cows(线段树[单点更新])
- POJ - 2481 Cows(线段树单点更新)
- poj2481 Cows&&poj2352 Stars
- poj 2481 - Cows(树状数组)
- POJ 2481 Cows 树状数组
- POJ 2481 Cows
- POJ 2481 Cows