#include<bits/stdc++.h>
#include<fstream>
using namespace std;
//#pragma GCC optimize("O3")
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
ll mod = 998244353, pr = 69;
const int mxn = 3e5 + 5, mxq = 1e5 + 5, sq = 1005, mxv = 5e4 + 1, lg = 60;
//const int base = (1 <<18);
const ll inf = 2e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, q;
ll a[mxn + 1], d[mxn + 1];
int sign(ll x){
if(x < 0)return(0);
if(x > 0)return(1);
return(2);
}
int rev(int x){
if(x == 0)return(1);
if(x == 1)return(0);
return(-1);
}
struct Node{
ll cnt = 0, pref = 0, suf = 0;
};
Node st[4 * mxn + 1];
Node comb(Node a, Node b, int l, int mid, int r){
Node res;
res.cnt = a.cnt + b.cnt; res.pref = a.pref; res.suf = b.suf;
if(sign(d[mid]) == rev(sign(d[mid + 1]))){
res.cnt += a.suf * b.pref;
if(a.pref == mid - l + 1)res.pref += b.pref;
if(b.suf == r - mid)res.suf += a.suf;
}
return(res);
}
void upd(int nd, int l, int r, int id){
if(l == r){
if(d[l] == 0){
st[nd].cnt = st[nd].pref = st[nd].suf = 0;
}else{
st[nd].cnt = st[nd].pref = st[nd].suf = 1;
}
return;
}
int mid = (l + r) >> 1;
if(id <= mid)upd(nd << 1, l, mid, id);
else upd(nd << 1 | 1, mid + 1, r, id);
st[nd] = comb(st[nd << 1], st[nd << 1 | 1], l, mid, r);
}
Node get(int nd, int l, int r, int ql, int qr){
if(ql <= l && qr >= r)return(st[nd]);
int mid = (l + r) >> 1;
if(qr <= mid)return(get(nd << 1, l, mid, ql, qr));
else if(ql > mid)return(get(nd << 1 | 1, mid + 1, r, ql, qr));
else return(comb(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr), max(l, ql), mid, min(r, qr)));
}
void solve(){
cin >> n >> q;
for(int i = 1; i <= n; i++)cin >> a[i];
for(int i = 1; i < n; i++){
d[i] = a[i + 1] - a[i];
upd(1, 1, n, i);
}
while(q--){
char idq; cin >> idq;
if(idq == '*'){
int l, r; cin >> l >> r;
for(int i = l; i <= r; i++)a[i] *= -1;
if(l - 1 >= 1){
d[l - 1] = a[l] - a[l - 1];
upd(1, 1, n, l - 1);
}if(l + 1 <= n){
d[l] = a[l + 1] - a[l];
upd(1, 1, n, l);
}
if(r + 1 <= n){
d[r] = a[r + 1] - a[r];
upd(1, 1, n, r);
}
if(r >= 1){
d[r - 1] = a[r] - a[r - 1];
upd(1, 1, n, r - 1);
}
}else if(idq == '+'){
int l, r; ll v; cin >> l >> r >> v;
for(int i = l; i <= r; i++)a[i] += v;
if(l - 1 >= 1){
d[l - 1] = a[l] - a[l - 1];
upd(1, 1, n, l - 1);
}if(l + 1 <= n){
d[l] = a[l + 1] - a[l];
upd(1, 1, n, l);
}
if(r + 1 <= n){
d[r] = a[r + 1] - a[r];
upd(1, 1, n, r);
}
if(r >= 1){
d[r - 1] = a[r] - a[r - 1];
upd(1, 1, n, r - 1);
}
}else{
int l, r; cin >> l >> r;
cout << get(1, 1, n, l, r - 1).cnt + r - l + 1 << "\n";
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("TESTROOMS.inp", "r", stdin);
//freopen("TESTROOMS.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2011 ms |
4440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2033 ms |
4440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2011 ms |
4440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |