题意:
If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj。
然后给一堆牛的 s 和 e。
问比妹纸牛壮的牛有多少只。
解析:
这题巨坑,有重复的牛要特判。
然后先按照s降序排序,如果s相等,按照e升序排序。
然后用s找比s小的,用树状数组来维护。
就酱。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
#define lson lo, mi, rt << 1
#define rson mi + 1, hi, rt << 1 | 1
using namespace std;
const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int c[maxn];
int n;
int lowbit(int x)
{
return x & -x;
}
void update(int x, int num)
{
while (x <= n)
{
c[x] += num;
x += lowbit(x);
}
}
int query(int x)
{
int res = 0;
while (x)
{
res += c[x];
x -= lowbit(x);
}
return res;
}
struct Cow
{
int s, e;
int id;
} cow[maxn];
int ans[maxn];
bool cmp(Cow a, Cow b)
{
if (a.e == b.e)
return a.s < b.s;
return a.e > b.e;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
while (~scanf("%d", &n) && n)
{
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &cow[i].s, &cow[i].e);
cow[i].s++;
cow[i].e++;
cow[i].id = i;
}
sort(cow + 1, cow + n + 1, cmp);
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++)
{
//cout << cow[i].id <<" " << cow[i].s << " " << cow[i].e << endl;
if (i > 1 && cow[i].s == cow[i - 1].s && cow[i].e == cow[i - 1].e)
ans[cow[i].id] = ans[cow[i - 1].id];
else
ans[cow[i].id] = query(cow[i].s);
update(cow[i].s, 1);
}
for (int i = 1; i < n; i++)
{
printf("%d ", ans[i]);
}
printf("%d\n", ans[n]);
}
return 0;
}