POJ2104 & 主席还是可持久化还是 函数式
题意:
区间第K大。
SOL:
非常有意思的树,尽管我搞不清楚名字。
原理参见clj的可持久化数据结构研究。
wa了整整一天,然后重打,然后1a...
code:
/*========================================================================== # Last modified: 2016-02-23 07:35 # Filename: 2104_2.cpp # Description: ==========================================================================*/ #define me AcrossTheSky #include <cstdio> #include <cmath> #include <ctime> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #define lowbit(x) (x)&(-x) #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) #define FORP(i,a,b) for(int i=(a);i<=(b);i++) #define FORM(i,a,b) for(int i=(a);i>=(b);i--) #define ls(a,b) (((a)+(b)) << 1) #define rs(a,b) (((a)+(b)) >> 1) #define getlc(a) ch[(a)][0] #define getrc(a) ch[(a)][1] #define maxn 100005 #define maxm 100000 #define pi 3.1415926535898 #define _e 2.718281828459 #define INF 1070000000 using namespace std; typedef long long ll; typedef unsigned long long ull; template<class T> inline void read(T& num) { bool start=false,neg=false; char c; num=0; while((c=getchar())!=EOF) { if(c=='-') start=neg=true; else if(c>='0' && c<='9') { start=true; num=num*10+c-'0'; } else if(start) break; } if(neg) num=-num; } /*==================split line==================*/ int v[maxn*20],ch[maxn*20][2],id[maxn],a[maxn],b[maxn],root[maxn]; int cnt=0; int n,m; bool cmp(const int &x,const int &y){ return a[x]<a[y]; } void updata(int node){ v[node]=v[ch[node][0]]+v[ch[node][1]];} void insert(int &node,int l,int r,int k){ int t=++cnt; ch[t][0]=ch[node][0]; ch[t][1]=ch[node][1]; node=cnt; v[node]++; if (l==r) return; int mid=rs(l,r); if (k<=mid) insert(ch[node][0],l,mid,k); else insert(ch[node][1],mid+1,r,k); updata(node); } int query(int o,int p,int l,int r,int k){ if (l==r) return a[id[l]]; int mid=rs(l,r),s=v[ch[o][0]]-v[ch[p][0]]; if (k<=s) return query(ch[o][0],ch[p][0],l,mid,k); else return query(ch[o][1],ch[p][1],mid+1,r,k-s); }/* int out[30][maxn]; void check(int node,int dep){ if (node==0) return; out[dep][++out[dep][0]]=node; check(ch[node][0],dep+1); check(ch[node][1],dep+1); } void print(){ for (int i=1;i<=INF && out[i][0];i++) { FORP(j,1,out[i][0]) printf("%d ",v[out[i][j]]); cout << endl; } cout << endl; }*/ int main(){ read(n); read(m); FORP(i,1,n) read(a[i]),id[i]=i; sort(id+1,id+1+n,cmp); FORP(i,1,n) b[id[i]]=i; FORP(i,1,n){ root[i]=root[i-1]; insert(root[i],1,n,b[i]); } //FORP(i,0,n) {memset(out,0,sizeof(out)); check(root[i],1); print();} FORP(i,1,m){ int x,y,k; read(x); read(y); read(k); printf("%d\n",query(root[y],root[x-1],1,n,k)); } }
Sometimes it s the very people who no one imagines anything of. who do the things that no one can imagine.