poj2352
每次只需计算小于等于当前x值得个数有多少即可。
ac代码:
treap
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; #define per(i,a,b) for(int i=a;i <= b;i++) #define Max(a,b) a=max(a,b) #define Min(a,b) a=min(a,b) #define Sz(x) (int)x.size() typedef long long ll; ll gcd(ll a,ll b){while(b){ll t=b;b=a%b;a=t;} return a;} const int inf=0x3f3f3f3f; #define siz 15005 int n,x,y,ans[siz]; struct TreapNode { int rnd,sz; TreapNode* child[2]; int x; TreapNode(int X):x(X){ rnd=rand(); sz=1; child[0]=child[1]=NULL; } void update(){sz=1;if(child[0]!=NULL)sz+=child[0]->sz; if(child[1]!=NULL)sz+=child[1]->sz;} }*root,*tmp; void Rotate(TreapNode*&u,int type) { tmp=u->child[type^1]; u->child[type^1]=tmp->child[type]; tmp->child[type]=u; u->update();tmp->update(); u=tmp; } void Insert(TreapNode*&u,int xx) { if(u==NULL){u=new TreapNode(xx);} else{ int type=xx > u->x; Insert(u->child[type],xx); if(u->child[type]->rnd > u->rnd)Rotate(u,type^1); } u->update();//子节点新插入,所以要更新! } int FindNum(TreapNode*u,int key)//小于key的数量 { int ret=0; if(u == NULL)return 0; if(u->x <= key){ ret=1; if(u->child[0]!=NULL)ret+=u->child[0]->sz; return ret+FindNum(u->child[1],key); } else if(u->x > key)return FindNum(u->child[0],key); } int main() { std::ios::sync_with_stdio(false); while(scanf("%d",&n)!=EOF){ memset(ans,0,sizeof(ans)); root=NULL; for(int i=1;i<=n;i++){ scanf("%d %d",&x,&y); ans[FindNum(root,x)]++; Insert(root,x); } per(i,0,n-1)printf("%d\n",ans[i]); } return 0; }
树状数组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include<stdio.h> #include <cstdlib> #include<malloc.h> #include<algorithm> #include<cmath> #include<map> #include<string.h> #include<string> #include<vector> #include<stack> #include<set> #include<queue> #include<list> #include<time.h> int Max(int a, int b) { return a > b ? a : b; } int Min(int a, int b) { return a > b ? b : a; } #define FOR(i,a,b) for(int i=a;i<=b;i++) const int MAX = 0x3f3f3f3f; typedef long long LL; typedef unsigned long long ull; #define max 15005 #define maxn 32005 int c[maxn]; int ans[maxn]; int n; int lowbit(int k) { return k&-k; } void add(int x,int v) { for (int i = x; i <= maxn; i += lowbit(i)) { c[i] += v; } } int query(int x) { int ans = 0; for (int i = x; i > 0; i -= lowbit(i)) { ans += c[i]; } return ans; } int main() { //freopen("e://outtest.txt", "w", stdout); //freopen("e://test.txt", "r", stdin); while (scanf("%d", &n) != EOF&&n!=0) { memset(c, 0, sizeof(c)); memset(ans, 0, sizeof(ans)); int x, y; FOR(i, 1, n) { scanf("%d %d", &x, &y); ++x; ans[query(x)]++; add(x, 1); } FOR(j, 0, n - 1)printf("%d\n", ans[j]); } return 0; }