首页 > 代码库 > 3211: 花神游历各国

3211: 花神游历各国

3211: 花神游历各国

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 1042  Solved: 381
[Submit][Status]

Description

 

Input

 

Output

每次x=1时,每行一个整数,表示这次旅行的开心度

 

Sample Input

4

1 100 5 5

5

1 1 2

2 1 2

1 1 2

2 2 3

1 1 4

Sample Output

101

11

11

HINT

 

对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9


 

 

Source

SPOJ2713 gss4 数据已加强

 
题解:嗯哼!!!果然是加强版的数据,可惜还是一遍AC了(HansBug:^_^,咦?最近phile呢?),其实和“上帝造题的7分钟2”基本上一样,就是它的强化版,重点在于处理优化掉一些不必要的操作即可(比如当某区间内的数字全部<=1呵呵呵),具体不再赘述,详见我的代码模板:线段树5(传送门在此)
 
 1 var 2    i,j,k,l,m,n:longint; 3    a,b:array[0..1000000] of int64; 4 function max(x,y:longint):longint;inline; 5          begin 6               if x>y then max:=x else max:=y; 7          end; 8 function min(x,y:longint):longint;inline; 9          begin10               if x<y then min:=x else min:=y;11          end;12 13 procedure built(z,x,y:longint);inline;14           begin15                if (x=y) then16                   begin17                        read(a[z]);18                        if a[z]<=1 then b[z]:=1 else b[z]:=0;19                   end20                else21                    begin22                         built(z*2,x,(x+y) div 2);23                         built(z*2+1,(x+y) div 2+1,y);24                         a[z]:=a[z*2]+a[z*2+1];25                         if (b[z*2]=1) and (b[z*2+1]=1) then b[z]:=1 else b[z]:=0;26                    end;27           end;28 function op(z,x,y,l,r:longint):int64;inline;29          var a2,a3:int64;30          begin31               if l>r then exit(0);32               if b[z]=1 then exit(0);33               if (x=l) and (y=r) and (l=r) then34                  begin35                       a2:=a[z];36                       a[z]:=trunc(sqrt(a[z]));37                       if a[z]<=1 then b[z]:=1;38                       exit(a[z]-a2);39                  end;40               a2:=op(z*2,x,(x+y) div 2,l,min(r,(x+y) div 2));41               a3:=op(z*2+1,(x+y) div 2+1,y,max((x+y) div 2+1,l),r);42               a[z]:=a[z]+a2+a3;43               if (b[z*2]=1) AND (b[z*2+1]=1) then b[z]:=1;44               exit(a2+a3);45          end;46 function cal(z,x,y,l,r:longint):int64;inline;47          var a2,a3:int64;48          begin49               if l>r then exit(0);50               if (x=l) and (y=r) then exit(a[z]);51               a2:=cal(z*2,x,(x+y) div 2,l,min(r,(x+y) div 2));52               a3:=cal(z*2+1,(x+y) div 2+1,y,max((x+y) div 2+1,l),r);53               exit(a2+a3);54          end;55 begin56      readln(n);57      built(1,1,n);58      readln;59      readln(m);60      for i:=1 to m do61          begin62               readln(j,k,l);63               case j of64                    1:writeln(cal(1,1,n,k,l));65                    2:op(1,1,n,k,l);66               end;67          end;68      readln;69 end.70        

 

3211: 花神游历各国