思路:排序 + 线段树;
题目说的是[si,ei],[sj,ej],要的是前者包含后者,前者强。然后问的是对于给定的区间有几个比她强(前面的定义)的区间。我们可以先让s满足包含的条件,再来考虑e的问题,现在要先排个序,s小的排前面,s相同时e大的排前面。
先把第一个区间插入进去,对于后面的如果和前一个区间完全相同,就HH[p[i].id] = HH[p[i - 1].id],如果不一样,就Query一下,看看在s满足条件的情况下,有多少个区间的e也是满足条件的。最后再讲这个区间的e插入进去。
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <limits.h> using namespace std; #define MEM(a,b) memset(a,b,sizeof a) typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; inline int Readint(){ char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); } return x; } const int maxn = 1e5 + 10; int sum[maxn << 2]; inline void PushUp(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void Built(int L,int R,int rt){ sum[rt] = 0; if (L == R) return ; int mid = (L + R) >> 1; Built(L,mid,rt << 1); Built(mid + 1,R,rt << 1 | 1); return ; } void Insert(int L,int R,int rt,int p){ if (L == R){ // cout << "ok\n"; sum[rt]++; return ; } int mid = (L + R) >> 1; if (p <= mid) Insert(L,mid,rt << 1,p); else Insert(mid + 1,R,rt << 1 | 1,p); PushUp(rt); return ; } int Query(int L,int R,int rt,int l,int r){ if (l <= L && R <= r){ return sum[rt]; } int mid = (L + R) >> 1; int res = 0; if (l <= mid) res += Query(L,mid,rt << 1,l,r); if (r > mid) res += Query(mid + 1,R,rt << 1 | 1,l,r); return res; } struct node{ int id,s,e; bool operator < (const node& b)const{ if (s == b.s) return e > b.e; else return s < b.s; } }p[maxn]; int HH[maxn]; int n; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(scanf("%d",&n) != EOF && n){ MEM(HH, 0); int MAX = 0; for (int i = 0;i < n;++i){ p[i].s = Readint(); p[i].e = Readint(); p[i].id = i; MAX = max(MAX,p[i].e); } Built(0,MAX,1); sort(p,p + n); HH[p[0].id] = 0; Insert(0,MAX,1,p[0].e); for (int i = 1;i < n;++i){ if (p[i].s == p[i - 1].s && p[i].e == p[i - 1].e) HH[p[i].id] = HH[p[i - 1].id]; else HH[p[i].id] = Query(0,MAX,1,p[i].e,MAX); Insert(0,MAX,1,p[i].e); } for (int i = 0;i < n;i++){ printf("%d%c",HH[i],(i == n - 1)?'\n':' '); } } return 0; }