1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| type atype=record x,y,w,where:longint; end; var d:array[0..100010] of atype; b:array[0..40000000] of record ch:array[false..true] of longint; sz:longint; end; ep:longint; bg,ed,root,bgx,edx,citywhere:array[0..100010] of longint; w:array[0..100010] of record node,wg:longint; end; all:longint; procedure sort(l,r:longint); var i,j:longint; x,y:atype; begin i:=l; j:=r; x:=d[l+random(r-l+1)]; repeat while d[i].x<x.x do inc(i); while x.x<d[j].x do dec(j); if not(i>j) then begin y:=d[i]; d[i]:=d[j]; d[j]:=y; inc(i); j:=j-1; end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r); end; procedure sort2(l,r:longint); var i,j:longint; x,y:atype; begin i:=l; j:=r; x:=d[l+random(r-l+1)]; repeat while d[i].y<x.y do inc(i); while x.y<d[j].y do dec(j); if not(i>j) then begin y:=d[i]; d[i]:=d[j]; d[j]:=y; inc(i); j:=j-1; end; until i>j; if l<j then sort2(l,j); if i<r then sort2(i,r); end; function create(_s:longint):longint; begin inc(ep); b[ep].sz:=_s; fillchar(b[ep].ch,sizeof(b[ep].ch),0); exit(ep); end; function serere(root,dt,dep:longint):longint; begin inc(ep); serere:=ep; b[serere]:=b[root]; inc(b[serere].sz); if dep<0 then exit; b[serere].ch[dt and (1<<dep)>0]:=serere(b[serere].ch[dt and (1<<dep)>0],dt,dep-1); end; function query(kth,dep:longint):longint; var count,i:longint; begin if dep<0 then exit(0); count:=0; for i:=1 to all do inc(count,b[b[w[i].node].ch[false]].sz*w[i].wg); if count>=kth then begin for i:=1 to all do w[i].node:=b[w[i].node].ch[false]; exit(query(kth,dep-1)); end else begin for i:=1 to all do w[i].node:=b[w[i].node].ch[true]; exit(query(kth-count,dep-1)+1<<dep); end; end; procedure lemon(); var n,i,bsz,csz,cur,j,_Q,_cq,x1,y1,x2,y2,kth,x,y,temp,left,right,mid,ll,rr,final,_ep:longint; ch,ch2:char; begin readln(n,_Q); for i:=1 to n do begin readln(d[i].x,d[i].y,d[i].w); d[i].where:=i; end; sort(1,n); ep:=0; root[0]:=create(0); bsz:=trunc(sqrt(n)); csz:=0; cur:=1; while cur<=n do begin inc(csz); bg[csz]:=cur; ed[csz]:=cur+bsz-1; if ed[csz]>n then ed[csz]:=n; bgx[csz]:=d[bg[csz]].x; edx[csz]:=d[ed[csz]].x; cur:=ed[csz]+1; end; for i:=1 to csz do begin sort2(bg[i],ed[i]); root[bg[i]]:=serere(root[0],d[bg[i]].w,30); for j:=bg[i]+1 to ed[i] do root[j]:=serere(root[j-1],d[j].w,30); end; for i:=1 to n do citywhere[d[i].where]:=i; for _cq:=1 to _Q do begin _ep:=ep; repeat read(ch); until ch in ['Q','S']; repeat read(ch2); until ch2=' '; if ch='Q' then begin readln(x1,y1,x2,y2,kth); all:=0; if x1>x2 then begin temp:=x1; x1:=x2; x2:=temp; end; if y1>y2 then begin temp:=y1; y1:=y2; y2:=temp; end; for i:=1 to csz do if (x1<=bgx[i])and(edx[i]<=x2) then begin if (d[bg[i]].y>y2)or(d[ed[i]].y<y1) then continue; left:=bg[i]; right:=ed[i]; while left<>right do begin mid:=(left+right+1)>>1; if d[mid].y>y2 then right:=mid-1 else left:=mid; end; rr:=left; left:=bg[i]; right:=ed[i]; while left<>right do begin mid:=(left+right)>>1; if d[mid].y>=y1 then right:=mid else left:=mid+1; end; ll:=left; if ll<>bg[i] then begin inc(all); w[all].node:=root[ll-1]; w[all].wg:=-1; end; inc(all); w[all].node:=root[rr]; w[all].wg:=1; end else if ((bgx[i]<=x1)and(x1<=edx[i]))or((bgx[i]<=x2)and(x2<=edx[i])) then begin inc(all); w[all].node:=create(0); w[all].wg:=1; for j:=bg[i] to ed[i] do if (x1<=d[j].x)and(d[j].x<=x2)and(y1<=d[j].y)and(d[j].y<=y2) then w[all].node:=serere(w[all].node,d[j].w,30); if b[w[all].node].sz=0 then dec(all); end; final:=query(kth,30); if final=maxlongint then writeln('It doesn',chr(39),'t exist.') else writeln(final); ep:=_ep; end else begin readln(x,y); x:=citywhere[x+1]; y:=citywhere[y+1]; temp:=d[x].w; d[x].w:=d[y].w; d[y].w:=temp; for i:=1 to csz do if (bg[i]<=x)and(x<=ed[i]) then begin root[bg[i]]:=serere(root[0],d[bg[i]].w,30); for j:=bg[i]+1 to ed[i] do root[j]:=serere(root[j-1],d[j].w,30); end else if (bg[i]<=y)and(y<=ed[i]) then begin root[bg[i]]:=serere(root[0],d[bg[i]].w,30); for j:=bg[i]+1 to ed[i] do root[j]:=serere(root[j-1],d[j].w,30); end; end; if ep>38000000 then begin ep:=0; root[0]:=Create(0); for i:=1 to csz do begin root[bg[i]]:=serere(root[0],d[bg[i]].w,30); for j:=bg[i]+1 to ed[i] do root[j]:=serere(root[j-1],d[j].w,30); end; end; end; end; begin lemon(); end.
|