半个月的期末。。然后CZL和CTL神犇就刷了几百道题orz!!!!

区间K大值,主席树入门

这个数据结构就是先离散化后对[1-i]建树,每次多建个相关的链(好神奇的做法)

然后这样就要记录儿子了,所以数组写起来好难看所以就用了指针

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<iostream>
 7 #define inc(i,l,r) for(int i=l;i<=r;i++)
 8 #define dec(i,l,r) for(int i=l;i>=r;i--)
 9 #define link(x) for(edge *j=h[x];j;j=j->next)
10 #define mem(a) memset(a,0,sizeof(a))
11 #define inf 1e9
12 #define ll long long
13 #define succ(x) (1<<x)
14 #define NM 100000+5
15 using namespace std;
16 int read(){
17     int x=0,f=1;char ch=getchar();
18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
19     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
20     return x*f;
21 }
22 struct node{
23     int s;
24     node *l,*r;
25 }T[400*NM],*root[NM],*o;
26 int n,m,cas,a[NM],b[NM],_x,_y,_t,cnt;
27 node* mod(node *p,int x,int y){
28     int t=x+y>>1;node *r=++o;
29     r->s=p->s+1;
30     r->l=p->l;r->r=p->r;
31     if(x==y)return r;
32     if(_t<=t)r->l=mod(p->l,x,t);
33     else r->r=mod(p->r,t+1,y);
34     return r;
35 }
36 int query(node *_r,node *r,int x,int y,int k){
37     int t=x+y>>1,v=r->l->s-_r->l->s;
38     if(x==y)return b[x];
39     if(k<=v)return query(_r->l,r->l,x,t,k);
40     else return query(_r->r,r->r,t+1,y,k-v);
41 }
42 int main(){
43 //    freopen("data.in","r",stdin);
44     cas=read();
45     while(cas--){
46         mem(root);o=T;root[0]=++o;root[0]->l=root[0]->r=root[0];
47         n=read();m=read();
48         inc(i,1,n)b[i]=a[i]=read();
49         sort(b+1,b+n+1);
50         cnt=unique(b+1,b+n+1)-b-1;
51         inc(i,1,n)
52         a[i]=lower_bound(b+1,b+1+cnt,a[i])-b;
53 //        inc(i,1,n)printf("%d ",a[i]);printf("\n");
54         inc(i,1,n){
55             _t=a[i];
56             root[i]=mod(root[i-1],1,cnt);
57         }
58         while(m--){
59             _x=read();_y=read();_t=read();
60             printf("%d\n",query(root[_x-1],root[_y],1,cnt,_t));
61         }
62     }
63     return 0;
64 }
View Code

 

posted on 2016-01-28 00:13  onlyRP  阅读(200)  评论(0编辑  收藏  举报