线段树

裸的线段树板子。

在取模意义下的区间加和区间乘以及区间求和。

洛谷 P3373 线段树2

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<cstdio>
#include<iostream>
#include<cstring>

using std::cin;
using std::endl;
using std::cerr;
using std::cout;
using std::string;

typedef long long LL;

const int inf=0x7fffffff;
const LL linf=0x7ffffffffffffff;

LL mod;

class SegmentTree
{
private:
typedef struct TreeNode
{
int l,r;
LL val,mul,add;
TreeNode *lch,*rch;

TreeNode(int l,int r):l(l),r(r),mul(1LL),add(0LL)
{
lch=rch=NULL;
}

~TreeNode()
{
if(lch!=NULL)
delete rch;
if(rch!=NULL)
delete lch;
}

LL Size()
{
return (LL)(r-l+1);
}

LL Value(string opt)
{
if(opt=="left")
return lch==NULL?0:lch->val;
else if(opt=="right")
return rch==NULL?0:rch->val;
else return 0;
}

void Update(LL _mul,LL _add)
{
val=(val*_mul+Size()*_add)%mod;
mul=mul*_mul%mod;
add=(add*_mul+_add)%mod;
}

void Mul(LL k)
{
val=val*k%mod;
mul=mul*k%mod;
add=add*k%mod;
}

void Add(LL k)
{
val=(val+Size()*k)%mod;
add=(add+k)%mod;
}

void Pushup()
{
val=(Value("left")+Value("right"))%mod;
}

void Pushdown()
{
if(lch!=NULL)
lch->Update(mul,add);
if(rch!=NULL)
rch->Update(mul,add);
mul=1,add=0;
}

}*Tree;

Tree root;

void Build(Tree &node,int l,int r)
{
node=new TreeNode(l,r);
if(l==r)
{
scanf("%lld",&node->val);
return;
}
int mid=l+r>>1;
Build(node->lch,l,mid);
Build(node->rch,mid+1,r);
node->Pushup();
}

void Add(Tree node,int l,int r,LL k)
{
if(l==node->l&&r==node->r)
{
node->Add(k);
return;
}
node->Pushdown();
int mid=node->l+node->r>>1;
if(r<=mid)
Add(node->lch,l,r,k);
else if(l>mid)
Add(node->rch,l,r,k);
else
{
Add(node->lch,l,mid,k);
Add(node->rch,mid+1,r,k);
}
node->Pushup();
}

void Mul(Tree node,int l,int r,LL k)
{
if(l==node->l&&r==node->r)
{
node->Mul(k);
return;
}
node->Pushdown();
int mid=node->l+node->r>>1;
if(r<=mid)
Mul(node->lch,l,r,k);
else if(l>mid)
Mul(node->rch,l,r,k);
else
{
Mul(node->lch,l,mid,k);
Mul(node->rch,mid+1,r,k);
}
node->Pushup();
}

LL Sum(Tree node,int l,int r)
{
if(l==node->l&&r==node->r)
return node->val;
node->Pushdown();
int mid=node->l+node->r>>1;
if(r<=mid)
return Sum(node->lch,l,r);
else if(l>mid)
return Sum(node->rch,l,r);
else
return (Sum(node->lch,l,mid)+Sum(node->rch,mid+1,r))%mod;
}

public:
SegmentTree(int n):root(NULL)
{
Build(root,1,n);
}

~SegmentTree()
{
delete root;
}

void Add(int l,int r)
{
LL k;
scanf("%lld",&k);
Add(root,l,r,k);
}

void Mul(int l,int r)
{
LL k;
scanf("%lld",&k);
Mul(root,l,r,k);
}

void Sum(int l,int r)
{
printf("%lld\n",Sum(root,l,r));
}
};

int main()
{
int n,m;
scanf("%d%d%lld",&n,&m,&mod);
static SegmentTree *T=new SegmentTree(n);
for(int i=1;i<=m;i++)
{
int opt,x,y;
scanf("%d%d%d",&opt,&x,&y);
switch(opt)
{
case 1: T->Mul(x,y); break;
case 2: T->Add(x,y); break;
case 3: T->Sum(x,y); break;
}
}
delete T;
return 0;
}