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
|
#include <bits/stdc++.h>
const int MAXN=1e5+10;
typedef long long intEx;
struct Node{
int l;
int r;
int max;
int min;
int add;
intEx sum;
Node* lch;
Node* rch;
Node(int,int);
void Add(int);
void PushDown();
void Maintain();
void Add(int,int,int);
void Div(int,int,int);
int QueryMin(int,int);
intEx QuerySum(int,int);
};
int n;
int q;
int a[MAXN];
int ReadInt();
inline int FDiv(int,int);
int main(){
n=ReadInt(),q=ReadInt();
for(int i=0;i<n;i++)
a[i]=ReadInt();
Node* N=new Node(0,n-1);
for(int i=0;i<q;i++){
int t=ReadInt(),l=ReadInt(),r=ReadInt();
if(t==1){
int d=ReadInt();
N->Add(l,r,d);
}
else if(t==2){
int d=ReadInt();
N->Div(l,r,d);
}
else if(t==3)
printf("%d\n",N->QueryMin(l,r));
else if(t==4)
printf("%lld\n",N->QuerySum(l,r));
}
return 0;
}
intEx Node::QuerySum(int l,int r){
if(l<=this->l&&this->r<=r)
return this->sum;
else{
this->PushDown();
if(r<=this->lch->r)
return this->lch->QuerySum(l,r);
if(this->rch->l<=l)
return this->rch->QuerySum(l,r);
return this->lch->QuerySum(l,r)+this->rch->QuerySum(l,r);
}
}
int Node::QueryMin(int l,int r){
if(l<=this->l&&this->r<=r)
return this->min;
else{
this->PushDown();
if(r<=this->lch->r)
return this->lch->QueryMin(l,r);
if(this->rch->l<=l)
return this->rch->QueryMin(l,r);
return std::min(this->lch->QueryMin(l,r),this->rch->QueryMin(l,r));
}
}
void Node::Add(int l,int r,int d){
if(l<=this->l&&this->r<=r)
this->Add(d);
else{
this->PushDown();
if(l<=this->lch->r)
this->lch->Add(l,r,d);
if(this->rch->l<=r)
this->rch->Add(l,r,d);
this->Maintain();
}
}
void Node::Div(int l,int r,int d){
if(l<=this->l&&this->r<=r){
if(this->max-FDiv(this->max,d)==this->min-FDiv(this->min,d))
this->Add(FDiv(this->max,d)-this->max);
else{
this->PushDown();
this->lch->Div(l,r,d);
this->rch->Div(l,r,d);
this->Maintain();
}
}
else{
this->PushDown();
if(l<=this->lch->r)
this->lch->Div(l,r,d);
if(this->rch->l<=r)
this->rch->Div(l,r,d);
this->Maintain();
}
}
inline void Node::Add(int d){
this->max+=d;
this->min+=d;
this->add+=d;
this->sum+=1ll*(this->r-this->l+1)*d;
}
inline void Node::Maintain(){
this->sum=this->lch->sum+this->rch->sum;
this->max=std::max(this->lch->max,this->rch->max);
this->min=std::min(this->lch->min,this->rch->min);
}
inline void Node::PushDown(){
if(this->add!=0){
this->lch->Add(this->add);
this->rch->Add(this->add);
this->add=0;
}
}
Node::Node(int l,int r):l(l),r(r),max(a[l]),min(a[r]),add(0),sum(a[l]),lch(NULL),rch(NULL){
if(l!=r){
int mid=(l+r)>>1;
this->lch=new Node(l,mid);
this->rch=new Node(mid+1,r);
this->Maintain();
}
}
inline int ReadInt(){
int x=0,sgn=1;
register char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')
sgn=-sgn;
ch=getchar();
}
while(isdigit(ch)){
x=x*10+ch-'0';
ch=getchar();
}
return x*sgn;
}
inline int FDiv(int x,int d){
if(x>=0)
return x/d;
else
return (x-d+1)/d;
}
|