#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using vll = vector<ll>;
int N, Q;
vll diff;
struct Node
{
int l, r;
ll lp = 0; //lp (everything below already includes lp)
int ns = 1; //node size
bool ih = 0; //is homogenous (if homogenous, adjust everything below!)
ll fv = 0; //first value
int fs = 1; //first size
ll lv = 0; //last value
int ls = 1; //last size
int ts = 1; //total size
ll sm = 0; //post lp
};
Node singlenode(int i)
{
return {i, i, 0, 1, 1, diff[i], 1, diff[i], 1, 1, diff[i]};
}
Node combine(Node& A, Node& B)
{
Node res;
res.l = A.l;
res.r = B.r;
res.lp = 0;
res.ns = A.ns + B.ns;
res.ih = 0;
res.fv = A.fv;
if(A.fs == A.ts && B.fv == A.fv)
res.fs = A.fs + B.fs;
res.lv = B.lv;
if(B.ls == B.ts && A.lv == B.lv)
res.ls = A.ls + B.ls;
res.ts = max(A.ts, B.ts);
if(A.lv == B.fv)
res.ts = max(res.ts, A.ls + B.fs);
res.sm = A.sm + B.sm;
return res;
}
Node combine2(Node A, Node B)
{
return combine(A, B);
}
void add(Node& A, ll V)
{
A.lp += V;
A.fv += V;
A.lv += V;
A.sm += V * A.ns;
}
void set(Node& A, ll V)
{
A.lp = 0;
A.ih = 1;
A.fv = A.lv = V;
A.fs = A.ls = A.ts = A.ns;
A.sm = A.ns * V;
}
struct segtree
{
Node v;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(int L, int R)
{
if(L == R) v = singlenode(L);
else
{
left = new segtree(L, (L+R)/2);
right = new segtree((L+R)/2+1, R);
v = combine(left->v, right->v);
}
}
void pushdown()
{
if(v.ih)
{
v.ih = 0;
set(left->v, v.fv);
set(right->v, v.fv);
}
else
{
add(left->v, v.lp);
add(right->v, v.lp);
v.lp = 0;
}
}
ll rangesum(int L, int R)
{
if(R < v.l || v.r < L) return 0;
else if(L <= v.l && v.r <= R)
{
return v.sm;
}
else
{
pushdown();
return left->rangesum(L, R) + right->rangesum(L, R);
}
}
Node getRange(int L, int R)
{
if(L <= v.l && v.r <= R) return v;
else
{
pushdown();
if(L <= left->v.r && right->v.l <= R) return combine2(left->getRange(L, R), right->getRange(L, R));
else if(L <= left->v.r) return left->getRange(L, R);
else return right->getRange(L, R);
}
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> Q;
vll D(1+N);
for(int i = 1; i <= N; i++) cin >> D[i];
if(N == 1)
{
for(int j = 1; j <= Q; j++)
{
int Z;
cin >> Z;
int u;
if(Z <= 2) cin >> u >> u >> u >> u;
else
{
cin >> u >> u;
cout << 1 << '\n';
}
}
return 0;
}
diff = vll(1+N);
segtree ST(1, N-1);
// cerr << "done \n";
for(int j = 1; j <= Q; j++)
{
ll o;
cin >> o;
if(o == 3)
{
int L, R;
cin >> L >> R;
if(L == R) cout << 1 << '\n';
else
{
cout << ST.getRange(L, R-1).ts + 1 << '\n';
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
144 ms |
65856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
348 ms |
68472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
165 ms |
65892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
348 ms |
68472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
144 ms |
65856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |