可持久化线段树求区间第k大

  • 老爷布置题==poj2104

  老爷口谕,有n个数-10^9<=n<=10^9,每个数的范围在[0,10^5],给定m个询问,每次询问区间[L,R]的第K小值

  可持久化线段树,舔的老爷的模板

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 #define maxn 100005
 5 int val[maxn],hash[maxn],hh;
 6 int tree[maxn*20],root[maxn*20],Rson[maxn*20],Lson[maxn*20],tot;
 7 void build(int x,int &y,int l,int r,int v){
 8     tree[y=++tot]=tree[x]+1;
 9     if(l==r)return;
10     int mid=(l+r)>>1;
11     if(v<=mid){
12         Rson[y]=Rson[x];
13         build(Lson[x],Lson[y],l,mid,v);
14     }
15     else{
16         Lson[y]=Lson[x];
17         build(Rson[x],Rson[y],mid+1,r,v);
18     }
19 }
20 int query(int x,int y,int l,int r,int k){
21     if(l==r)return l;
22     int mid=(l+r)>>1,tmp=tree[Lson[y]]-tree[Lson[x]];
23     if(k<=tmp)return query(Lson[x],Lson[y],l,mid,k);
24     else return query(Rson[x],Rson[y],mid+1,r,k-tmp);
25 }
26 int main(){
27     freopen("1.in","r",stdin);
28     int n,m,r,l,k;
29     scanf("%d%d",&n,&m);
30     for(int i=1;i<=n;i++){
31         scanf("%d",&val[i]);
32         hash[++hh]=val[i];
33     }
34     sort(hash+1,hash+hh+1);
35     hh=unique(hash+1,hash+hh+1)-(hash+1);
36     for(int i=1;i<=n;i++){
37         val[i]=lower_bound(hash+1,hash+hh+1,val[i])-hash;
38         build(root[i-1],root[i],1,hh,val[i]);
39     }
40     for(int i=1;i<=m;i++){
41         scanf("%d%d%d",&l,&r,&k);
42         printf("%d
",hash[query(root[l-1],root[r],1,hh,k)]);
43     }
44     return 0;
45 }
View Code
  • 清橙1505->原来是这个橙

  lca+可持久化线段树,yooo~

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 #define maxn 100005
 6 int cnt,v[maxn<<1],next[maxn<<1],first[maxn];
 7 int dep[maxn],f[maxn][25],val[maxn],hash[maxn],hh;
 8 int tree[maxn*20],root[maxn],Lson[maxn*20],Rson[maxn*20],tot;
 9 
10 void add(int st,int end){
11     v[++cnt]=end;
12     next[cnt]=first[st];
13     first[st]=cnt;
14 }
15 void build(int x,int &y,int l,int r,int v){
16     tree[y=++tot]=tree[x]+1;
17     if(l==r)return;
18     int mid=(l+r)>>1;
19     if(v<=mid){
20         Rson[y]=Rson[x];
21         build(Lson[x],Lson[y],l,mid,v);
22     }
23     else{
24         Lson[y]=Lson[x];
25         build(Rson[x],Rson[y],mid+1,r,v);
26     }
27 }
28 void dfs(int x){
29     build(root[f[x][0]],root[x],1,hh,val[x]);
30     for(int e=first[x];e;e=next[e]){
31         if(v[e]!=f[x][0]){
32             f[v[e]][0]=x;
33             dep[v[e]]=dep[x]+1;
34             dfs(v[e]);
35         }
36     }
37 }
38 int query(int a,int b,int c,int d,int l,int r,int k){
39     if(l==r)return l;
40     int mid=(l+r)>>1,tmp=tree[Lson[c]]+tree[Lson[d]]-tree[Lson[b]]-tree[Lson[a]];
41     if(k<=tmp)return query(Lson[a],Lson[b],Lson[c],Lson[d],l,mid,k);
42     else return query(Rson[a],Rson[b],Rson[c],Rson[d],mid+1,r,k-tmp);
43 }
44 int lca(int a,int b){
45     int log;
46     if(dep[a]<dep[b])swap(a,b);
47     for(log=1;(1<<log)<=dep[a];log++);
48     log--;
49     for(int i=log;i>=0;i--)
50         if(dep[a]-(1<<i)>=dep[b])a=f[a][i];
51     if(a==b)return a;
52     for(int i=log;i>=0;i--)
53         if(f[a][i]&&f[a][i]!=f[b][i])
54             a=f[a][i],b=f[b][i];
55     return f[a][0];
56 }
57 int main(){
58     freopen("1.in","r",stdin);
59     int n,m,a,b,c;
60     scanf("%d%d",&n,&m);
61     for(int i=1;i<=n;i++){
62         scanf("%d",&val[i]);
63         hash[++hh]=val[i];
64     }
65     sort(hash+1,hash+1+hh);
66     hh=unique(hash+1,hash+1+hh)-(hash+1);
67     for(int i=1;i<=n;i++)
68         val[i]=lower_bound(hash+1,hash+1+hh,val[i])-hash;
69     for(int i=1;i<n;i++){
70         scanf("%d%d",&a,&b);
71         add(a,b),add(b,a);
72     }
73     dfs(1);
74     for(int j=1;(1<<j)<n;j++)
75         for(int i=1;i<=n;i++)
76             f[i][j]=f[f[i][j-1]][j-1];
77     for(int i=1;i<=m;i++){
78         scanf("%d%d%d",&a,&b,&c);
79         int d=lca(b,c),k;
80         if(a==1)k=1;
81         else if(a==2)k=dep[b]+dep[c]-2*dep[d]+1;
82         else k=(dep[b]+dep[c]-2*dep[d]+3)/2;
83         printf("%d
",hash[query(root[f[d][0]],root[d],root[b],root[c],1,hh,k)]);
84     }
85     return 0;
86 }
View Code
原文地址:https://www.cnblogs.com/Ngshily/p/4995971.html