bzoj3196: Tyvj 1730 二逼平衡树
这题太经典了……树套树模板题……写着超级爽,思路流畅,最后卡了一会儿常就过去了……
我就是来粘个代码的……fhqtreap写着比较舒服
1 /************************************************************** 2 Problem: 3196 3 User: 1090900715 4 Language: Pascal 5 Result: Accepted 6 Time:10812 ms 7 Memory:60084 kb 8 ****************************************************************/ 9 10 {$S-}{$I-}{$V-}{$R-} 11 program j01; 12 uses math; 13 type xx=record l,r,size,w,f:longint; end; 14 const maxn=50086; 15 var t:array[0..60*maxn]of xx; 16 root:array[0..4*maxn]of longint; 17 a:array[0..maxn]of longint; 18 n,m,tt,l,r,op,k,ps,i,mm:longint; 19 procedure update(const i:longint);inline; 20 begin 21 if i=0 then exit; 22 t[i].size:=t[t[i].l].size+t[t[i].r].size+1; 23 end; 24 function merge(x,y:longint):longint; 25 begin 26 if (x=0)or(y=0) then exit(x+y); 27 if t[x].f>t[y].f then 28 begin 29 t[x].r:=merge(t[x].r,y);update(x);exit(x); 30 end else 31 begin 32 t[y].l:=merge(x,t[y].l);update(y);exit(y); 33 end; 34 end; 35 procedure splite(i,x:longint;var k1,k2:longint); 36 begin 37 if i=0 then 38 begin 39 k1:=0;k2:=0;exit; 40 end; 41 if t[i].w<x then 42 begin 43 k1:=i;splite(t[i].r,x,t[i].r,k2); 44 end else 45 begin 46 k2:=i;splite(t[i].l,x,k1,t[i].l); 47 end; 48 update(i); 49 end; 50 function insert(i,k:longint):longint; 51 begin 52 if i=0 then exit(k); 53 if t[i].f>t[k].f then 54 begin 55 if t[i].w<t[k].w then t[i].r:=insert(t[i].r,k) 56 else t[i].l:=insert(t[i].l,k); 57 update(i);exit(i); 58 end; 59 splite(i,t[k].w,t[k].l,t[k].r);update(k);exit(k); 60 end; 61 function faskr(i,x:longint):longint; 62 var k1,k2:longint; 63 begin 64 splite(root[i],x,k1,k2); 65 faskr:=t[k1].size;root[i]:=merge(k1,k2); 66 end; 67 function cal(i,x:longint):longint; 68 var k1,k2:longint; 69 begin 70 splite(root[i],x,k1,k2);cal:=t[k1].size; 71 root[i]:=merge(k1,k2); 72 end; 73 74 function faskp(i,x:longint):longint; 75 var k,k1,k2:longint; 76 begin 77 splite(root[i],x,k1,k2);if k1=0 then exit(0); 78 k:=k1;while t[k].r<>0 do k:=t[k].r; 79 root[i]:=merge(k1,k2); 80 exit(t[k].w); 81 end; 82 function faskn(i,x:longint):longint; 83 var k,k1,k2:longint; 84 begin 85 splite(root[i],x,k1,k2);if k2=0 then exit(mm); 86 k:=k2;while t[k].l<>0 do k:=t[k].l; 87 root[i]:=merge(k1,k2); 88 exit(t[k].w); 89 end; 90 procedure add(i,l,r,ps,x:longint); 91 var mid:longint; 92 begin 93 inc(tt);t[tt].f:=random(maxlongint);t[tt].w:=x;t[tt].size:=1; 94 root[i]:=insert(root[i],tt); 95 if l=r then exit; 96 mid:=(l+r) div 2; 97 if ps<=mid then add(i*2,l,mid,ps,x) else add(i*2+1,mid+1,r,ps,x); 98 end; 99 procedure des(i,l,r,ps,x:longint); 100 var mid,k1,k2,k3:longint; 101 begin 102 splite(root[i],x,k1,k2);splite(k2,x+1,k2,k3); 103 root[i]:=merge(merge(k1,t[k2].l),merge(t[k2].r,k3)); 104 if l=r then exit; 105 mid:=(l+r) div 2; 106 if ps<=mid then des(i*2,l,mid,ps,x) else des(i*2+1,mid+1,r,ps,x); 107 end; 108 function askrank(i,l,r,ll,rr,x:longint):longint; 109 var mid:longint; 110 begin 111 if (ll<=l)and(r<=rr) then exit(faskr(i,x)); 112 mid:=(l+r)div 2;askrank:=0; 113 if ll<=mid then askrank:=askrank+askrank(i*2,l,mid,ll,rr,x); 114 if rr>=mid+1 then askrank:=askrank+askrank(i*2+1,mid+1,r,ll,rr,x); 115 end; 116 function calc(i,l,r,ll,rr,x:longint):longint; 117 var mid:longint; 118 begin 119 if (ll<=l)and(r<=rr) then exit(cal(i,x)); 120 mid:=(l+r)div 2;calc:=0; 121 if ll<=mid then calc:=calc+calc(i*2,l,mid,ll,rr,x); 122 if rr>=mid+1 then calc:=calc+calc(i*2+1,mid+1,r,ll,rr,x); 123 end; 124 function askpre(i,l,r,ll,rr,x:longint):longint; 125 var mid:longint; 126 begin 127 if (ll<=l)and(r<=rr) then exit(faskp(i,x)); 128 mid:=(l+r)div 2;askpre:=0; 129 if ll<=mid then askpre:=max(askpre,askpre(i*2,l,mid,ll,rr,x)); 130 if rr>=mid+1 then askpre:=max(askpre,askpre(i*2+1,mid+1,r,ll,rr,x)); 131 end; 132 function asknex(i,l,r,ll,rr,x:longint):longint; 133 var mid:longint; 134 begin 135 if (ll<=l)and(r<=rr) then exit(faskn(i,x+1)); 136 mid:=(l+r)div 2;asknex:=mm; 137 if ll<=mid then asknex:=min(asknex,asknex(i*2,l,mid,ll,rr,x)); 138 if rr>=mid+1 then asknex:=min(asknex,asknex(i*2+1,mid+1,r,ll,rr,x)); 139 end; 140 function ask(const ll,rr,x:longint):longint;inline; 141 var l,r,mid,tmp:longint; 142 begin 143 l:=0;r:=mm; 144 while l<r do 145 begin 146 mid:=(l+r)div 2;tmp:=calc(1,1,n,ll,rr,mid+1); 147 if tmp>x then r:=mid else l:=mid+1; 148 end; 149 exit(l); 150 end; 151 begin 152 readln(n,m); 153 tt:=0;randomize;mm:=0; 154 for i:=1 to n do 155 begin 156 read(a[i]);add(1,1,n,i,a[i]);mm:=max(mm,a[i]); 157 end; 158 for i:=1 to m do 159 begin 160 read(op); 161 if op=1 then 162 begin 163 readln(l,r,k); 164 writeln(askrank(1,1,n,l,r,k)+1); 165 end; 166 if op=2 then 167 begin 168 readln(l,r,k); 169 writeln(ask(l,r,k-1)); 170 end; 171 if op=3 then 172 begin 173 readln(ps,k); 174 des(1,1,n,ps,a[ps]); 175 a[ps]:=k;mm:=max(mm,k); 176 add(1,1,n,ps,a[ps]); 177 end; 178 if op=4 then 179 begin 180 readln(l,r,k); 181 writeln(askpre(1,1,n,l,r,k)); 182 end; 183 if op=5 then 184 begin 185 readln(l,r,k); 186 writeln(asknex(1,1,n,l,r,k)); 187 end; 188 end; 189 end.