# 【bzoj1798】[Ahoi2009]Seq 维护序列seq

  1 #include<algorithm>
2 #include<iostream>
3 #include<cstdlib>
4 #include<cstring>
5 #include<cstdio>
6 #include<cmath>
7 using namespace std;
8
9 typedef long long LL;
10
11 #define N 100010
12
13 int n,m;
15 LL p;
16
18
19 void pushup(int now)
20 {
21     sum[now]=(sum[now<<1]+sum[now<<1|1])%p;
22 }
23
24 void pushdown(int now,int d)
25 {
26     if (add[now]!=0 || mul[now]!=1)
27     {
28         mul[now<<1]=mul[now<<1]*mul[now]%p;
29         mul[now<<1|1]=mul[now<<1|1]*mul[now]%p;
35         mul[now]=1;
36     }
37 }
38
39 void build(int nowl,int nowr,int now)
40 {
41     sum[now]=0;
42     mul[now]=1;
43     if (nowl==nowr)
44     {
45         scanf("%lld",&sum[now]);
46         return ;
47     }
48     int mid=(nowl+nowr)>>1;
49     build(nowl,mid,now<<1);
50     build(mid+1,nowr,now<<1|1);
51     pushup(now);
52 }
53
54 void updata_mul(int nowl,int nowr,int now,int l,int r,int c)
55 {
56     if (nowl>=l && nowr<=r)
57     {
59         sum[now]=sum[now]*c%p;
60         mul[now]=mul[now]*c%p;
61         return ;
62     }
63     pushdown(now,nowr-nowl+1);
64     int mid=(nowl+nowr)>>1;
65     if (l<=mid)
66         updata_mul(nowl,mid,now<<1,l,r,c);
67     if (r>mid)
68         updata_mul(mid+1,nowr,now<<1|1,l,r,c);
69     pushup(now);
70 }
71
72 void updata_add(int nowl,int nowr,int now,int l,int r,int c)
73 {
74     if (nowl>=l && nowr<=r)
75     {
77         sum[now]=(sum[now]+c*(nowr-nowl+1))%p;
78         return ;
79     }
80     pushdown(now,nowr-nowl+1);
81     int mid=(nowl+nowr)>>1;
82     if (l<=mid)
84     if (r>mid)
86     pushup(now);
87 }
88
89 LL query(int nowl,int nowr,int now,int l,int r)
90 {
91     LL res(0);
92     if (nowl>=l && nowr<=r)
93         return sum[now];
94     pushdown(now,nowr-nowl+1);
95     int mid=(nowl+nowr)>>1;
96     if (l<=mid)
97         res=(res+query(nowl,mid,now<<1,l,r))%p;
98     if (r>mid)
99         res=(res+query(mid+1,nowr,now<<1|1,l,r))%p;
100     return res;
101 }
102
103 int main()
104 {
105     scanf("%d%lld",&n,&p);
106     build(1,n,1);
107     scanf("%d",&m);
108     while (m--)
109     {
112         {
115         }
117         {
128 }