Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15393 Accepted Submission(s): 7682
當N = 0,輸入結束。
#include<iostream>
#include<cstdio>
using namespace std;
const int MAX = 100010;
struct ss
{
int left, right, value, mark;
}segtree[MAX*4];
int N, a, b, first;
void build(int root, int l, int r)
{
segtree[root].left = l;
segtree[root].right = r;
if(l == r)
{
segtree[root].value = 0;
segtree[root].mark = 0;
return;
}
build(root*2, l, (l+r)/2);
build(root*2+1, (l+r)/2+1, r);
segtree[root].value = 0;
segtree[root].mark= 0;
}
void add(int root, int l, int r)
{
if(segtree[root].left > r || segtree[root].right < l) return;
if(l <= segtree[root].left && r >= segtree[root].right)
{
segtree[root].mark ++;
return;
}
add(root*2, l, r);
add(root*2+1, l, r);
}
void answer(int root, int l, int r)
{
if(l == r)
{
if(first == 0) cout <<" ";
else first = 0;
cout << segtree[root].value + segtree[root].mark;
return;
}
segtree[root*2].mark += segtree[root].mark;
segtree[root*2+1].mark += segtree[root].mark;
answer(root*2, l, (l+r)/2);
answer(root*2+1, (l+r)/2+1, r);
}
int main()
{
while(scanf("%d", &N) != EOF && N)
{
first = 1;
build(1, 1, N);
for(int i = 0;i < N; ++i)
{
scanf("%d %d", &a, &b);
add(1, a, b);
}
answer(1, 1, N);
cout << endl;
}
return 0;
}