Title: Http://www.spoj.com/problems/ORDERS/and http://acm.hdu.edu.cn/showproblem.php?pid=2852
Test instructions: spoj227: Tell the number of points in front of each position is smaller than the current position, to find the original sequence. hdu2852: Design a container that supports several operations: adding/removing elements to find out what is the number of k in a container larger than a.
Analysis: Two ideas are to find the array of the number of K small. began to look for O (N*logn) method, and later found O (N*LOGN*LOGN) can also be too ... Two-step: the same as the previous reverse number of the first processing with a tree array like x small number of how many, and then two ~ (the value is monotonous)
spoj227 Code:
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 2e5+6;int Lowbit[maxn],a[maxn],tree[maxn];void Getlowbit () {for (int i=1;i<maxn;i++) lowbit[i]=i&-i;} void update (int x,int v) {for (int i=x;i<maxn && i;i+=lowbit[i]) tree[i]+=v;} int query (int x) {int ret (0); for (int i=x;i>0;i-=lowbit[i]) ret+=tree[i];return ret;} int Find (int x,int n) {int ret=1e9,down=1,mid,up=n,v;while (down<=up) {mid= (Down+up) >>1;v=query (mid); if (v >=x) {up=mid-1;if (Mid<ret && x==v) Ret=mid;} elsedown=mid+1;} return ret;} int main () {getlowbit (); int ncase,n,x,y,v,i,j;scanf ("%d", &ncase), while (ncase--) {scanf ("%d", &n); Memset ( Tree,0,sizeof (Tree[0]) * (n+3)); for (i=1;i<=n;i++) {update (i,1); scanf ("%d", &a[i]);} for (i=n;i>=1;i--) {a[i]=find (i-a[i],n); update (a[i],-1);} for (i=1;i<n;i++) printf ("%d", A[i]);p rintf ("%d\n", A[n]);} return 0;}
hdu2852 Code:
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1e5+6;int Tree[maxn],cnt[maxn],lowbit[maxn];void Getlowbit () {for (int i=1;i<maxn;i++) lowbit[i]=i&-i;} void update (int x,int v) {for (int i=x;i<maxn && i;i+=lowbit[i]) tree[i]+=v;} int query (int x) {int ret (0); for (int i=x;i>0;i-=lowbit[i]) ret+=tree[i];return ret;} int Find (int a,int k) {int down=a+1,mid,up=maxn-1,c=query (a), V,ret=1e9;while (down<=up) {mid= (down+up) >>1;v= Query (mid)-c;if (v>=k) {up=mid-1;if (Ret>mid) Ret=mid;} elsedown=mid+1;} return ret;} int main () {getlowbit (); int Q,a,k,tp,v;while (scanf ("%d", &q)!=eof) {memset (tree,0,sizeof (tree)); Memset (cnt,0, sizeof (CNT)), while (q--) {scanf ("%d", &TP), if (tp==0) {scanf ("%d", &v); Cnt[v]++;update (v,1);} else if (tp==1) {scanf ("%d", &v), if (cnt[v]>0) {cnt[v]--;update (v,-1);} Elseputs ("No elment!");} ELSE{SCANF ("%d%d", &a,&k); int num=query (maxn-1)-query (a); if (num<k) puts ("Not find!"); elseprintf ("%d\n", FIND (A,K));}}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Tree array for K decimal value (spoj227 ordering the soldiers && hdu2852 KiKi ' s k-number)