您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: