排好序后就跟stars那题一样了
一次遍历排序后的数组,由于比当前遍历元素strong的区间只可能存在于已经遍历过的元素中;所以我们可以放心大胆的直接对树状数组求和
一次遍历排序后的数组,由于比当前遍历元素strong的区间只可能存在于已经遍历过的元素中;所以我们可以放心大胆的直接对树状数组求和
然后向后更新树状数组中统治x的点上的值,因为那些位置都多了一个比 以他们为左端点的区间 强壮 的区间;
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int c[maxn],result[maxn];
typedef struct
{
int e, s, ord;
} node;
node nd[maxn];
bool cmp(node a , node b)
{
/*if(a.e > b.e)
return true;
if(a.e == b.e)
if(a.s < b.s)
return true;
return false;*/
if(a.e == b.e)
return a.s < b.s;
return a.e > b.e;
}
int Lowbit(int x)
{
return x & (-x);
}
void Update(int x)
{
int i;
for(i = x; i < maxn; i += Lowbit(i))
{
c[i]++;
}
}
int Getsum(int x)
{
int sum = 0;
int i;
for( i = x; i > 0; i -= Lowbit(i) )
{
sum += c[i];
}
return sum;
}
int main()
{
int n;
while(scanf("%d", &n), n)
{
memset(c, 0, sizeof(c)); //少写了这句。wa了半天才检查出来。
memset(result, 0, sizeof(result));
int x, y;
for(int i = 1; i <= n; i++)
{
scanf("%d %d", &x, &y);
nd[i].s = x + 1;
nd[i].e = y + 1;
nd[i].ord = i;
}
sort(nd+1, nd+n+1, cmp);
result[nd[1].ord] = Getsum(nd[1].s);
Update(nd[1].s);
for(int i = 2; i <= n; i ++)
{
if(nd[i].s == nd[i-1].s && nd[i].e == nd[i-1].e)
result[nd[i].ord] = result[nd[i-1].ord];
else
result[nd[i].ord] = Getsum(nd[i].s);
Update(nd[i].s);
}
for(int i = 1; i < n; i++)
printf("%d ", result[i]);
printf("%d\n", result[n]);
}
return 0;
}