# bzoj1798[Ahoi2009]Seq 维护序列seq

7 43
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7

2
35
8

## HINT

【样例说明】

N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000
M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

 1 program rrr(input,output);
2 type
3   treetype=record
4      l,r:longint;
5      mul,plus,sum:int64;
6   end;
7 var
8   a:array[0..400040]of treetype;
9   num:array[0..100010]of int64;
10   n,m,i,opt,x,y:longint;
11   c,p:int64;
12 procedure build(k,l,r:longint);
13 var
14   mid,i:longint;
15 begin
16    a[k].l:=l;a[k].r:=r;a[k].mul:=1;a[k].plus:=0;
17    if l=r then begin a[k].sum:=num[l];exit; end;
18    mid:=(l+r)>>1;i:=k<<1;
19    build(i,l,mid);
20    build(i+1,mid+1,r);
21    a[k].sum:=(a[i].sum+a[i+1].sum) mod p;
22 end;
23 procedure pushdown(k:longint);
24 var
25   i:longint;
26 begin
27    if (a[k].plus=0) and (a[k].mul=1) then exit;
28    if a[k].l=a[k].r then begin a[k].plus:=0;a[k].mul:=1;exit; end;
29    i:=k<<1;
30    a[i].sum:=(a[i].sum*a[k].mul+a[k].plus*(a[i].r-a[i].l+1)) mod p;
31    a[i+1].sum:=(a[i+1].sum*a[k].mul+a[k].plus*(a[i+1].r-a[i+1].l+1)) mod p;
32    a[i].mul:=a[i].mul*a[k].mul mod p;a[i].plus:=(a[i].plus*a[k].mul+a[k].plus) mod p;
33    a[i+1].mul:=a[i+1].mul*a[k].mul mod p;a[i+1].plus:=(a[i+1].plus*a[k].mul+a[k].plus) mod p;
34    a[k].mul:=1;a[k].plus:=0;
35 end;
36 procedure changemul(k:longint);
37 var
38   mid,i:longint;
39 begin
40    pushdown(k);
41    if (x<=a[k].l) and (a[k].r<=y) then begin a[k].sum:=a[k].sum*c mod p;a[k].mul:=c mod p;exit; end;
42    mid:=(a[k].l+a[k].r)>>1;i:=k<<1;
43    if x<=mid then changemul(i);
44    if mid<y then changemul(i+1);
45    a[k].sum:=(a[i].sum+a[i+1].sum) mod p;
46 end;
47 procedure changeplus(k:longint);
48 var
49   mid,i:longint;
50 begin
51    pushdown(k);
52    if (x<=a[k].l) and (a[k].r<=y) then begin a[k].sum:=(a[k].sum+c*(a[k].r-a[k].l+1)) mod p;a[k].plus:=c mod p;exit; end;
53    mid:=(a[k].l+a[k].r)>>1;i:=k<<1;
54    if x<=mid then changeplus(i);
55    if mid<y then changeplus(i+1);
56    a[k].sum:=(a[i].sum+a[i+1].sum) mod p;
57 end;
59 var
60   mid,i:longint;
61 begin
62    pushdown(k);
63    if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].sum);
64    mid:=(a[k].l+a[k].r)>>1;i:=k<<1;
68 end;
69 begin
70    assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
72    for i:=1 to n do read(num[i]);
73    build(1,1,n);
83 end.