#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node {
int Lval, Rval;
int Ld=0, Li=0, Rd=0,Ri=0;
long long ans=0;
int sz=0;
node(){}
node(int val) {
sz=Ld=Li=Rd=Ri=1;
ans=1;
Lval=Rval=val;
}
};
node merge(node L, node R) {
node res;
res.Lval = L.Lval;
res.Rval = R.Rval;
res.sz = L.sz+R.sz;
res.Ld = L.Ld;
res.Li = L.Li;
res.Rd = R.Rd;
res.Ri = R.Ri;
res.ans = L.ans+R.ans;
if(L.Rval > R.Lval) {
res.ans -= L.Rd*(L.Rd+1)/2;
res.ans -= R.Li*(R.Li+1)/2;
long long sz = L.Rd + R.Li;
res.ans += sz*(sz+1)/2;
if(R.sz == R.Li) {
if(R.sz%2 == 1) {
res.Ri = sz;
}
else {
res.Rd = sz;
}
}
if(L.sz == L.Rd) {
if(L.sz%2 == 1) {
res.Ld = sz;
}
else {
res.Li = sz;
}
}
}
else if(L.Rval < R.Lval) {
res.ans -= L.Ri*(L.Ri+1)/2;
res.ans -= R.Ld*(R.Ld+1)/2;
long long sz = L.Ri + R.Ld;
res.ans += sz*(sz+1)/2;
if(R.sz == R.Ld) {
if(R.sz%2 == 1) {
res.Rd = sz;
}
else {
res.Ri = sz;
}
}
if(L.sz == L.Ri) {
if(L.sz%2 == 1) {
res.Li = sz;
}
else {
res.Ld = sz;
}
}
}
return res;
}
signed main () {
int n,q;
cin >> n >> q;
node a[n];
int A[n];
for(int i =0 ;i<n;i++) {
cin >> A[i];
a[i]=node(A[i]);
}
while(q--) {
char c;
cin >> c;
if(c=='?'){
int l, r;
cin >> l >> r;
l--,r--;
node res = a[r];
for(int i = r-1;i>=l;i--) {
res = merge(a[i], res);
}
cout << res.ans << "\n";
}
else if(c == '*') {
int l, r;
cin >> l >> r;
l--,r--;
for(int i = l;i<=r;i++) {
A[i]*=-1;
a[i] = node(A[i]);
}
}
else if(c == '+') {
int l, r, val;
cin >> l >> r >> val;
l--,r--;
for(int i = l;i<=r;i++) {
A[i]+=val;
a[i] = node(A[i]);
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
860 KB |
Output is correct |
2 |
Correct |
42 ms |
860 KB |
Output is correct |
3 |
Correct |
50 ms |
600 KB |
Output is correct |
4 |
Correct |
56 ms |
856 KB |
Output is correct |
5 |
Correct |
48 ms |
600 KB |
Output is correct |
6 |
Correct |
4 ms |
344 KB |
Output is correct |
7 |
Correct |
143 ms |
884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2036 ms |
22352 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
860 KB |
Output is correct |
2 |
Correct |
42 ms |
860 KB |
Output is correct |
3 |
Correct |
50 ms |
600 KB |
Output is correct |
4 |
Correct |
56 ms |
856 KB |
Output is correct |
5 |
Correct |
48 ms |
600 KB |
Output is correct |
6 |
Correct |
4 ms |
344 KB |
Output is correct |
7 |
Correct |
143 ms |
884 KB |
Output is correct |
8 |
Execution timed out |
2074 ms |
7968 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
860 KB |
Output is correct |
2 |
Correct |
42 ms |
860 KB |
Output is correct |
3 |
Correct |
50 ms |
600 KB |
Output is correct |
4 |
Correct |
56 ms |
856 KB |
Output is correct |
5 |
Correct |
48 ms |
600 KB |
Output is correct |
6 |
Correct |
4 ms |
344 KB |
Output is correct |
7 |
Correct |
143 ms |
884 KB |
Output is correct |
8 |
Execution timed out |
2036 ms |
22352 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |