比赛 20120712 评测结果 AAWWWWWWWW
题目名称 区间权最大 最终得分 20
用户昵称 fuhao 运行时间 1.022 s
代码语言 Pascal 内存使用 5.12 MiB
提交时间 2012-07-12 09:33:05
显示代码纯文本
program Max;
const maxn=100001;
var
 n,m,i,l,r,tot:longint; f:array[0..maxn,1..3] of longint;
 left,right,lson,rson,maxx:array[0..maxn*2] of longint;

 procedure change(var x,y:longint);
 var t:longint;
 begin
  t:=x; x:=y; y:=t;
 end;

 procedure kp(l,r:longint);
 var i,j,mid1,mid2:longint;
 begin
  i:=l; j:=r; mid1:=f[(l+r) shr 1,1]; mid2:=f[(l+r) shr 1,2];
  repeat
   while (f[i,1]<mid1) or (f[i,1]=mid1) and (f[i,2]<mid2) do inc(i);
   while (f[j,1]>mid1) or (f[j,1]=mid1) and (f[j,2]>mid2) do dec(j);
   if i<=j then
    begin
     change(f[i,1],f[j,1]); change(f[i,2],f[j,2]); change(f[i,3],f[j,3]);
     inc(i); dec(j);
    end;
  until i>j;
  if i<r then kp(i,r);
  if l<j then kp(l,j);
 end;

 function mid_left(k:longint):longint;
 var l,r,mid:longint;
 begin
  l:=0; r:=n+1;
  while r-l>1 do
   begin
    mid:=(l+r) shr 1;
    if f[mid,1]<k then l:=mid else r:=mid;
   end;
  mid_left:=r;
 end;

 function mid_right(k:longint):longint;
 var l,r,mid:longint;
 begin
  l:=0; r:=n+1;
  while r-l>1 do
   begin
    mid:=(l+r) shr 1;
    if f[mid,2]>k then r:=mid else l:=mid;
   end;
  mid_right:=l;
 end;

 procedure build(l,r:longint);
 var v,mid:longint;
 begin
  inc(tot); v:=tot;
  left[tot]:=l; right[tot]:=r;
  if r-l>0 then
   begin
    mid:=(l+r) shr 1;
    lson[v]:=tot+1; build(l,mid);
    rson[v]:=tot+1; build(mid+1,r);
    if maxx[lson[v]]>maxx[rson[v]] then
     maxx[v]:=maxx[lson[v]] else
     maxx[v]:=maxx[rson[v]];
   end else maxx[tot]:=f[l,3];
 end;

 function ask(v:longint):longint;
 var t1,t2:longint;
 begin
  if (l<=left[v]) and (right[v]<=r) then exit(maxx[v]) else
  begin
   t1:=0; t2:=0;
   if r>=(left[v]+right[v]) div 2+1 then t1:=ask(rson[v]);
   if l<=(left[v]+right[v]) div 2 then t2:=ask(lson[v]);
   if t1<t2 then ask:=t2 else ask:=t1;
  end;
 end;

begin
  assign(input,'max.in'); reset(input);
  assign(output,'max.out'); rewrite(output);
  read(n,m);
  for i:=1 to n do readln(f[i,1],f[i,2],f[i,3]);
  kp(1,n);
  f[n+1,1]:=maxlongint; f[n+1,2]:=maxlongint;
  build(1,n);
  for i:=1 to m do
   begin
    read(l,r);
    l:=mid_left(l);
    r:=mid_right(r);
    if l>r then writeln(0) else writeln(ask(1));
   end;
  close(input); close(output);
end.