#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int INF = 1e15;
struct Node{
pair<int,int> mxl = {-INF,INF}; // tot, val
pair<int,int> mxr = {-INF,INF}; // tot, val
pair<int,int> tot = {-INF,INF}; // tot, val
int sz = 1;
int sum = 0;
};
struct SegTree{
int n;
vector<Node> seg; //mxl,
vector<pair<bool,int>> lazy; // 0 add, 1 set
Node merge(Node idx, Node idx1){
Node ans;
ans.sz = idx.sz + idx1.sz;
ans.sum = idx.sum + idx1.sum;
// if(idx.mxl.first == -INF && idx1.mxl.first == -INF) return ans; // both are null nodes
// if(idx1.mxl.first == -INF) return idx; // just second is a null node
// if(idx.mxl.first == -INF) return idx1;
ans.mxl = idx.mxl;
pair<int,int> templeft = idx.mxl; //idx ALWAYS IN THE LEFT
if(idx.mxl.first == idx.sz){
if(idx1.mxl.second == idx.mxl.second) templeft = {idx.sz + idx1.mxl.first, idx1.mxl.second};// join both lefts
else templeft = idx.mxl;
}
ans.mxl = max(ans.mxl, templeft); // take max
// repeat for right
ans.mxr = idx1.mxr;
pair<int,int> tempright = idx1.mxr;
if(idx1.mxr.first == idx1.sz){
if(idx1.mxr.second == idx.mxr.second) tempright = {idx1.sz + idx.mxr.first, idx.mxr.second};// join both lefts
else tempright = idx1.mxr;
}
ans.mxr = max(ans.mxr, tempright); // take max
// TAKE TOTAL
ans.tot = max(ans.mxl, ans.mxr);
ans.tot = max(idx.tot,idx1.tot);
// JOIN THE MIDDLE, idx right and idx1 left
pair<int,int> temptot = ans.tot;
if(idx.mxr.second == idx1.mxl.second){
temptot = {idx.mxr.first + idx1.mxl.first, idx.mxr.second};
}
ans.tot = max(ans.tot, temptot);
return ans;
}
void build(vector<int> v){
int n1 = v.size();
n = 1;
while(n < n1){
n*=2;
}
Node tmpn;
// cerr<<endl;
seg.assign(2*n, tmpn); lazy.assign(2*n, {0,0});
for(int i = 0; i<n1; i++){
// cerr<<v[i]<<" ";
seg[i+n].mxl = seg[i+n].mxr = seg[i+n].tot = {1,v[i]};
seg[i+n].sz = 1;
seg[i+n].sum = v[i];
}
// cerr<<endl;
for(int i = n-1; i>=1; i--){
// cout<<"BUILDING Seg "<<i<<" from seg 2*i = "<<seg[2*i].tot.first<<" "<<seg[2*i].tot.second<<" and seg 2*i+1 "<<seg[2*i+1].tot.first<<" "<<seg[2*i+1].tot.second<<endl;
seg[i] = merge(seg[2*i], seg[2*i+1]);
}
}
void add(int idx, int v, int l, int r){
seg[idx].sum += v * (r-l+1);
lazy[idx].second += v;
seg[idx].mxl.second += v;
seg[idx].mxr.second += v;
seg[idx].tot.second += v;
}
void set(int idx, int v, int l, int r){
seg[idx].sum = (r-l+1) * v;
lazy[idx].first = 1;
lazy[idx].second = v;
seg[idx].mxl.second = v;
seg[idx].mxl.first = (r-l+1); // bad for when updating from update and not from push down
seg[idx].mxr.second = v;
seg[idx].mxr.first = (r-l+1);
seg[idx].tot.second = v;
seg[idx].tot.first = (r-l+1);
}
void PushDown(int k, int l, int r){
if(lazy[k].first == 0){
//add
// if(lazy[k].second == 0) return;
int m = (l+r)/2;
// if(lazy[2*k].first == 1) lazy[2*k] = {0,0};
if(lazy[2*k].first) set(2*k,lazy[k].second+lazy[2*k].second,l,m);
else add(2*k, lazy[k].second, l,m);
// if(lazy[2*k+1].first == 1) lazy[2*k+1] = {0,0};
if(lazy[2*k+1].first) set(2*k+1,lazy[k].second+lazy[2*k+1].second,m+1,r);
else add(2*k+1,lazy[k].second, m+1, r);
}
else{
//set
int m = (l+r)/2;
// cout<<"K: "<<lazy[k].second<<endl;
set(2*k, lazy[k].second, l, m);
set(2*k+1,lazy[k].second,m+1,r);
}
lazy[k] = {0,0};
}
Node query(int ql, int qr, int k, int l, int r){
if(ql <= l && r <= qr) return seg[k];
if(l > qr || r < ql){
Node tmp;
return tmp;
}
PushDown(k,l,r);
int m = (l+r)/2;
Node x = query(ql,qr,2*k,l,m);
Node y = query(ql,qr,2*k+1,m+1,r);
return merge(x,y);
}
int query(int ql, int qr){
Node x = query(ql,qr,1,0,n-1);
// cout<<"X "<<ql<<" "<<qr<<" "<<x.tot.first<<" "<<x.tot.second<<endl;
return x.tot.first;
}
int query1(int ql, int qr){
Node x = query(ql,qr,1,0,n-1);
// cout<<"X "<<ql<<" "<<qr<<" "<<x.tot.first<<" "<<x.tot.second<<endl;
return x.tot.second;
}
void update(int ql, int qr, int tp, int v, int k, int l, int r){
if(ql <= l && r <= qr){
if(tp == 0){
// add
if(lazy[k].first == 1) set(k,v + lazy[k].second, l,r);
else add(k,v,l,r);
return;
}
else{
// cout<<"HERE "<<k<<" "<<ql<<" "<<qr<<" "<<l<<" "<<r<<" "<<v<<" ";
set(k,v,l,r);
// cout<<seg[k].tot.first<<" "<<seg[k].tot.second<<endl;
return;
}
}
if(l > qr || r < ql) return;
PushDown(k,l,r);
int m = (l+r)/2;
update(ql,qr,tp,v, 2*k,l,m);
update(ql,qr,tp,v, 2*k+1, m+1,r);
seg[k] = merge(seg[2*k], seg[2*k+1]);
}
void update(int ql, int qr, int tp, int v){
if(ql > qr) return;
update(ql,qr,tp,v,1, 0,n-1);
}
int sum(int ql, int qr){
return query(ql,qr,1,0,n-1).sum;
}
};
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q; cin>>n>>q;
vector<int> v(n); for(int& i : v) cin>>i;
vector<int> dif;
for(int i = 1; i<n; i++) dif.push_back(v[i-1] - v[i]);
SegTree seg; seg.build(dif);
int val = v[0];
int cnt = 0; bool ok1=0;
while(q--){
int tp; cin>>tp;
if(tp == 1){
//add
int l,r; cin>>l>>r; l--; r--;
int s,c; cin>>s>>c;
// cout<<"BEFORE UPDATE: ";
// for(int i = 0; i<n-1; i++){
// cout<<seg.query1(i,i)<<" ";
// }
// cout<<endl;
if(l == 0) v[0] += s;
seg.update(l,r-1,0,-c);
seg.update(r,r,0,s + (r-l)*c);
if(l) seg.update(l-1,l-1,0,-s);
// cout<<"AFTER UPDATE1: ";
// for(int i = 0; i<n-1; i++){
// cout<<seg.query1(i,i)<<" ";
// }
// cout<<endl<<endl;
}
else if(tp == 2){
ok1=1;
int l,r; cin>>l>>r; int s,c; cin>>s>>c; l--; r--;
// seg.update(l,r-1,1,c); // set differences
int sum = v[0];
// cout<<"BEFORE UPDATE2: ";
// for(int i = 0; i<n-1; i++){
// cout<<seg.query1(i,i)<<" ";
// }
// cout<<endl;
if(l>1) sum-=seg.sum(0,l-2);
int val = s + (r-l)*c;
int sum1 = v[0];
if(r) sum1-=seg.sum(0,r);
if(l == 0) v[0] = s;
if(l) seg.update(l-1, l-1, 1, -(s-sum));
if(r < n) seg.update(r,r,1, (val-sum1));
if(r > l) seg.update(l,r-1,1,-c);
// cout<<"VAL "<<val<<" "<<sum1<<" Other: "<<s<<" "<<sum<<endl;
// cout<<"AFTER UPDATE2: ";
// for(int i = 0; i<n-1; i++){
// cout<<seg.query1(i,i)<<" ";
// }
// cout<<endl<<endl;
}
else{
cnt++;
int l,r; cin>>l>>r; l--; r--;
if(l == r){
cout<<1<<endl; continue;
}
// cerr<<endl<<l<<" "<<r-1<<endl;
// if(cnt == 90) cout<<"ANS: ";
cout<<seg.query(l,r-1)+1<<endl;
}
}
}
Compilation message
Progression.cpp: In function 'int main()':
Progression.cpp:175:6: warning: unused variable 'val' [-Wunused-variable]
175 | int val = v[0];
| ^~~
Progression.cpp:176:20: warning: variable 'ok1' set but not used [-Wunused-but-set-variable]
176 | int cnt = 0; bool ok1=0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
388 ms |
91336 KB |
Output is correct |
2 |
Correct |
115 ms |
2644 KB |
Output is correct |
3 |
Correct |
106 ms |
2324 KB |
Output is correct |
4 |
Correct |
106 ms |
2392 KB |
Output is correct |
5 |
Correct |
108 ms |
2604 KB |
Output is correct |
6 |
Correct |
107 ms |
2384 KB |
Output is correct |
7 |
Correct |
106 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
368 ms |
92416 KB |
Output is correct |
12 |
Correct |
364 ms |
92064 KB |
Output is correct |
13 |
Correct |
364 ms |
92568 KB |
Output is correct |
14 |
Correct |
359 ms |
93004 KB |
Output is correct |
15 |
Correct |
364 ms |
92868 KB |
Output is correct |
16 |
Correct |
360 ms |
92632 KB |
Output is correct |
17 |
Correct |
362 ms |
92720 KB |
Output is correct |
18 |
Correct |
363 ms |
92864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
90568 KB |
Output is correct |
2 |
Correct |
94 ms |
2388 KB |
Output is correct |
3 |
Correct |
87 ms |
2540 KB |
Output is correct |
4 |
Correct |
71 ms |
2388 KB |
Output is correct |
5 |
Correct |
84 ms |
2500 KB |
Output is correct |
6 |
Correct |
83 ms |
2576 KB |
Output is correct |
7 |
Correct |
85 ms |
2548 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
363 ms |
91956 KB |
Output is correct |
12 |
Correct |
348 ms |
91896 KB |
Output is correct |
13 |
Correct |
352 ms |
90812 KB |
Output is correct |
14 |
Correct |
399 ms |
92048 KB |
Output is correct |
15 |
Correct |
346 ms |
92056 KB |
Output is correct |
16 |
Correct |
347 ms |
94012 KB |
Output is correct |
17 |
Correct |
354 ms |
92564 KB |
Output is correct |
18 |
Correct |
354 ms |
93324 KB |
Output is correct |
19 |
Correct |
297 ms |
93084 KB |
Output is correct |
20 |
Correct |
332 ms |
92340 KB |
Output is correct |
21 |
Correct |
290 ms |
91800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
693 ms |
89544 KB |
Output is correct |
2 |
Incorrect |
173 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
90568 KB |
Output is correct |
2 |
Correct |
94 ms |
2388 KB |
Output is correct |
3 |
Correct |
87 ms |
2540 KB |
Output is correct |
4 |
Correct |
71 ms |
2388 KB |
Output is correct |
5 |
Correct |
84 ms |
2500 KB |
Output is correct |
6 |
Correct |
83 ms |
2576 KB |
Output is correct |
7 |
Correct |
85 ms |
2548 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
363 ms |
91956 KB |
Output is correct |
12 |
Correct |
348 ms |
91896 KB |
Output is correct |
13 |
Correct |
352 ms |
90812 KB |
Output is correct |
14 |
Correct |
399 ms |
92048 KB |
Output is correct |
15 |
Correct |
346 ms |
92056 KB |
Output is correct |
16 |
Correct |
347 ms |
94012 KB |
Output is correct |
17 |
Correct |
354 ms |
92564 KB |
Output is correct |
18 |
Correct |
354 ms |
93324 KB |
Output is correct |
19 |
Correct |
297 ms |
93084 KB |
Output is correct |
20 |
Correct |
332 ms |
92340 KB |
Output is correct |
21 |
Correct |
290 ms |
91800 KB |
Output is correct |
22 |
Correct |
795 ms |
94156 KB |
Output is correct |
23 |
Correct |
142 ms |
2384 KB |
Output is correct |
24 |
Correct |
138 ms |
2384 KB |
Output is correct |
25 |
Correct |
141 ms |
2504 KB |
Output is correct |
26 |
Correct |
145 ms |
2388 KB |
Output is correct |
27 |
Correct |
155 ms |
2308 KB |
Output is correct |
28 |
Correct |
147 ms |
2400 KB |
Output is correct |
29 |
Correct |
1 ms |
544 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
805 ms |
92844 KB |
Output is correct |
33 |
Correct |
801 ms |
92784 KB |
Output is correct |
34 |
Correct |
861 ms |
92952 KB |
Output is correct |
35 |
Correct |
839 ms |
91376 KB |
Output is correct |
36 |
Correct |
690 ms |
91796 KB |
Output is correct |
37 |
Correct |
678 ms |
92032 KB |
Output is correct |
38 |
Correct |
664 ms |
91496 KB |
Output is correct |
39 |
Correct |
811 ms |
92860 KB |
Output is correct |
40 |
Correct |
817 ms |
93888 KB |
Output is correct |
41 |
Correct |
826 ms |
94104 KB |
Output is correct |
42 |
Correct |
825 ms |
93844 KB |
Output is correct |
43 |
Correct |
827 ms |
93428 KB |
Output is correct |
44 |
Correct |
807 ms |
93796 KB |
Output is correct |
45 |
Correct |
805 ms |
94420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
388 ms |
91336 KB |
Output is correct |
2 |
Correct |
115 ms |
2644 KB |
Output is correct |
3 |
Correct |
106 ms |
2324 KB |
Output is correct |
4 |
Correct |
106 ms |
2392 KB |
Output is correct |
5 |
Correct |
108 ms |
2604 KB |
Output is correct |
6 |
Correct |
107 ms |
2384 KB |
Output is correct |
7 |
Correct |
106 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
368 ms |
92416 KB |
Output is correct |
12 |
Correct |
364 ms |
92064 KB |
Output is correct |
13 |
Correct |
364 ms |
92568 KB |
Output is correct |
14 |
Correct |
359 ms |
93004 KB |
Output is correct |
15 |
Correct |
364 ms |
92868 KB |
Output is correct |
16 |
Correct |
360 ms |
92632 KB |
Output is correct |
17 |
Correct |
362 ms |
92720 KB |
Output is correct |
18 |
Correct |
363 ms |
92864 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
600 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |