感人肺腑pascal过不去系列,跪求开O2
先不说这个了,学完cdq分治之后又顺手学了一下整体二分
感觉这两个东西很多相似的地方,干脆都叫cdq分治好了
二分解决k小就是设当前二分的答案为m,把x<=m的标为1,统计区间和,如果大于等于k,说明答案小于等于m,否则大于m
其实仔细想想主席树就是方便干这个事,但这题主席树?总觉得有点虚
什么叫整体二分,就是二分答案,把答案能<=m归为1类,另外归为一类,划分成子问题继续解决(分治的策略)
统计和可以用二维树状数组
还是由于没开O2和pascal跑得慢,实在弄不过去,有算法上优化的话欢迎指教
代码仅供参考,因为TLE……

  1 type node=record
  2        x,y,v:longint;
  3      end;
  4      point=record
  5        x0,y0,x1,y1,k:longint;
  6      end;
  7 
  8 var q:array[0..60010] of point;
  9     ans,d,c:array[0..60010] of longint;
 10     v:array[0..60010] of boolean;
 11     a:array[0..250010] of node;
 12     w:array[0..250010] of longint;
 13     b:array[0..510,0..510] of longint;
 14     mx,n,m,i,j,k,p:longint;
 15 
 16 function lowbit(x:longint):longint;
 17   begin
 18     exit(x and (-x));
 19   end;
 20 
 21 procedure add(x,y,w:longint);
 22   var p:longint;
 23   begin
 24     p:=x;
 25     while y<=n do
 26     begin
 27       x:=p;
 28       while x<=n do
 29       begin
 30         inc(b[x,y],w);
 31         x:=x+lowbit(x);
 32       end;
 33       y:=y+lowbit(y);
 34     end;
 35   end;
 36 
 37 function ask(x,y:longint):longint;
 38   var p:longint;
 39   begin
 40     ask:=0; p:=x;
 41     while y>0 do
 42     begin
 43       x:=p;
 44       while x>0 do
 45       begin
 46         ask:=ask+b[x,y];
 47         x:=x-lowbit(x);
 48       end;
 49       y:=y-lowbit(y);
 50     end;
 51   end;
 52 
 53 function check(i:longint):boolean;
 54   var s:longint;
 55   begin
 56     s:=ask(q[i].x1,q[i].y1);
 57     if s<q[i].k then exit(false);
 58     s:=s-ask(q[i].x0-1,q[i].y1);
 59     if s<q[i].k then exit(false);
 60     s:=s+ask(q[i].x0-1,q[i].y0-1);
 61     if s<q[i].k then exit(false);
 62     s:=s-ask(q[i].x1,q[i].y0-1);
 63     if s>=q[i].k then exit(true) else exit(false);
 64   end;
 65 
 66 procedure swap(var a,b:node);
 67   var c:node;
 68   begin
 69     c:=a;
 70     a:=b;
 71     b:=c;
 72   end;
 73 
 74 procedure sort(l,r:longint);
 75   var i,j,x:longint;
 76   begin
 77     i:=l;
 78     j:=r;
 79     x:=a[(l+r) shr 1].v;
 80     repeat
 81       while a[i].v<x do inc(i);
 82       while x<a[j].v do dec(j);
 83       if not(i>j) then
 84       begin
 85         swap(a[i],a[j]);
 86         inc(i);
 87         dec(j);
 88       end;
 89     until i>j;
 90     if l<j then sort(l,j);
 91     if i<r then sort(i,r);
 92   end;
 93 
 94 procedure work(f,t,l,r:longint);
 95   var m,p,l1,l2:longint;
 96   begin
 97   //  writeln(f,' ',r,' ',l,' ',r);
 98     if f>t then exit;
 99     if l>r then exit;
100     m:=(l+r) shr 1;
101     while (j<k) and (a[j+1].v<=m) do
102     begin
103       add(a[j+1].x,a[j+1].y,1);
104       inc(j);
105     end;
106     while (j>0) and (a[j].v>m) do
107     begin
108       add(a[j].x,a[j].y,-1);
109       dec(j);
110     end;
111     p:=0;
112     for i:=f to t do
113       if check(c[i]) then
114       begin
115         inc(p);
116         ans[c[i]]:=m;
117         v[i]:=true;
118       end
119       else v[i]:=false;
120 
121     l1:=f; l2:=f+p;
122     for i:=f to t do
123       if v[i] then
124       begin
125         d[l1]:=c[i];
126         inc(l1);
127       end
128       else begin
129         d[l2]:=c[i];
130         inc(l2);
131       end;
132     for i:=f to t do c[i]:=d[i];
133     work(l1,t,m+1,r);
134     work(f,l1-1,l,m-1);
135   end;
136 
137 begin
138   readln(n,m);
139   for i:=1 to n do
140     for j:=1 to n do
141     begin
142       inc(k);
143       a[k].x:=i;
144       a[k].y:=j;
145       read(a[k].v);
146     end;
147   sort(1,k);
148   p:=1;
149   w[1]:=a[1].v;
150   a[1].v:=1;
151   for i:=2 to k do
152   begin
153     if w[p]<>a[i].v then
154     begin
155       inc(p);
156       w[p]:=a[i].v;
157     end;
158     a[i].v:=p;
159   end;
160   for i:=1 to m do
161   begin
162     with q[i] do readln(x0,y0,x1,y1,k);
163     c[i]:=i;
164   end;
165   j:=0;
166   work(1,m,1,p);
167   for i:=1 to m do
168     writeln(w[ans[i]]);
169 end.
View Code

 

posted on 2015-04-06 21:37  acphile  阅读(236)  评论(0编辑  收藏  举报