# cf765F.Souvenirs

editorial看得很烦躁啊……又回想起了被英语阅读支配的恐惧

  1 program cf765F;
2 uses math;
3 const maxn=100000+10;
4       maxm=300000+10;
5       maxv=1000000000;
6 type query=record next,l,r:longint;end;
7 var a,last:array[0..maxn] of longint;
8     ques:array[0..maxm] of query;
9     ans:array[0..maxm] of longint;
10     mark:array[0..maxn*4] of longint;
11     segv:array[0..maxn*40] of record l,r,right:longint;end;
12     n,m,root,tot,i,j,l,r,id:longint;
13 procedure build(x,l,r:longint);
14 var mid:longint;
15 begin
16   mark[x]:=maxv+1;
17   if l<r then
18   begin
19     mid:=(l+r)shr 1;
20     build(x+x,l,mid);
21     build(x+x+1,mid+1,r);
22   end;
23 end;
24 procedure update(x,l,r,nowl,nowr,v:longint);
25 var mid:longint;
26 begin
27   if(nowl<=l)and(r<=nowr) then
28   begin
29     mark[x]:=min(mark[x],v);
30     exit;
31   end;
32   mid:=(l+r)shr 1;
33   if nowl<=mid then update(x+x,l,mid,nowl,nowr,v);
34   if nowr>mid then update(x+x+1,mid+1,r,nowl,nowr,v);
35 end;
37 var mid:longint;
38 begin
39   if l=r then exit(mark[x]);
40   mid:=(l+r)shr 1;
43 end;
44 function findright(x,l,r,nowl,nowr:longint):longint;
45 var mid:longint;
46 begin
47   if((nowl<=l)and(r<=nowr))or(x=0) then exit(segv[x].right);
48   mid:=(l+r)shr 1;
49   if nowr<=mid then exit(findright(segv[x].l,l,mid,nowl,nowr));
50   if nowl>mid then exit(findright(segv[x].r,mid+1,r,nowl,nowr));
51   exit(max(findright(segv[x].l,l,mid,nowl,nowr),findright(segv[x].r,mid+1,r,nowl,nowr)));
52 end;
53 procedure modify(var x:longint;l,r,p,v:longint);
54 var mid:longint;
55 begin
56   if x=0 then
57   begin
58     inc(tot);
59     x:=tot;
60   end;
61   segv[x].right:=max(segv[x].right,v);
62   if l<r then
63   begin
64     mid:=(l+r)shr 1;
65     if p<=mid then modify(segv[x].l,l,mid,p,v)
66       else modify(segv[x].r,mid+1,r,p,v);
67   end;
68 end;
69 begin
71   for i:=1 to n do read(a[i]);
73   for i:=1 to m do readln(ques[i].l,ques[i].r);
74   fillchar(last,sizeof(last),0);
75   for i:=1 to m do
76   begin
77     ques[i].next:=last[ques[i].r];
78     last[ques[i].r]:=i;
79   end;
80   build(1,1,n);
81   root:=0;
82   tot:=0;
83   segv[0].right:=0;
84   for i:=1 to n do
85   begin
86     if i>1 then
87     begin
88       l:=a[i];
89       r:=maxv;
90       while true do
91       begin
92         id:=findright(root,0,maxv,l,r);
93         if id=0 then break;
94         update(1,1,n,1,id,a[id]-a[i]);
95         if a[i]=a[id] then break;
96         r:=(a[i]+a[id])shr 1;
97       end;
98       l:=0;
99       r:=a[i];
100       while true do
101       begin
102         id:=findright(root,0,maxv,l,r);
103         if id=0 then break;
104         update(1,1,n,1,id,a[i]-a[id]);
105         if a[i]=a[id] then break;
106         l:=(a[i]+a[id])shr 1+1;
107       end;
108     end;
109     j:=last[i];
110     while j>0 do
111     begin
118 end.