Submission #992164

# Submission time Handle Problem Language Result Execution time Memory
992164 2024-06-04T04:41:04 Z saddd ZIGZAG (INOI20_zigzag) C++17
100 / 100
763 ms 63072 KB
#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());
struct ST{
    ll sm[4 * mxn + 1];
    bool lz[4 * mxn + 1];
    void applyy(int nd, int v){
        if(v){
            sm[nd] = -sm[nd]; lz[nd] ^= 1;
        }
    }
    void push(int nd){
        applyy(nd << 1, lz[nd]); applyy(nd << 1 | 1, lz[nd]); lz[nd] = 0;
    }
    void upd(int nd, int l, int r, int id, ll v){
        if(l == r){
            sm[nd] = v; return;
        }
        int mid = (l + r) >> 1;
        push(nd);
        if(id <= mid)upd(nd << 1, l, mid, id, v);
        else upd(nd << 1 | 1, mid + 1, r, id, v);
        sm[nd] = sm[nd << 1] + sm[nd << 1 | 1];
    }
    void upd2(int nd, int l, int r, int ql, int qr){
        if(ql > r || qr < l){
            return;
        }
        if(ql <= l && qr >= r){
            applyy(nd, 1);
            return;
        }
        int mid = (l + r) >> 1;
        push(nd);
        upd2(nd << 1, l, mid, ql, qr); upd2(nd << 1 | 1, mid + 1, r, ql, qr);
        sm[nd] = sm[nd << 1] + sm[nd << 1 | 1];
    }
    ll get(int nd, int l, int r, int ql, int qr){
        if(ql > r || qr < l)return(0);
        if(ql <= l && qr >= r)return(sm[nd]);
        int mid = (l + r) >> 1;
        push(nd);
        return(get(nd << 1, l, mid, ql, qr) + get(nd << 1 | 1, mid + 1, r, ql, qr));
    }
};
ST cool;
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(ll x){
    if(x == 0)return(1);
    if(x == 1)return(0);
    return(2);
}
struct Node{
    ll cnt = 0, pref = 0, suf = 0;
    int vall, valr;
};
Node st[4 * mxn + 1];
ll sm[4 * mxn + 1];
bool lz[4 * mxn + 1];
void applyy(int nd, int v){
    if(v){
        st[nd].vall = rev(st[nd].vall); st[nd].valr = rev(st[nd].valr); lz[nd] ^= 1; 
    }
}
void push(int nd){
    applyy(nd << 1, lz[nd]); applyy(nd << 1 | 1, lz[nd]); lz[nd] = 0;
}
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(a.valr == rev(b.vall) && a.valr != 2){
        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;
    }
    res.vall = a.vall; res.valr = b.valr;
    return(res);
}
void upd2(int nd, int l, int r, int ql, int qr){
    if(l > r)return;
    if(ql > r || qr < l){
        return;
    }
    if(ql <= l && qr >= r){
        applyy(nd, 1);
        return;
    }
    int mid = (l + r) >> 1;
    push(nd);
    upd2(nd << 1, l, mid, ql, qr); upd2(nd << 1 | 1, mid + 1, r, ql, qr);
    st[nd] = comb(st[nd << 1], st[nd << 1 | 1], l, mid, r);
}

void upd(int nd, int l, int r, int id){
    if(l > r)return;
    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;
        }
        st[nd].vall = st[nd].valr = sign(d[l]);
        return;
    }
    int mid = (l + r) >> 1;
    push(nd);
    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){
    assert(l <= r);
    if(ql <= l && qr >= r)return(st[nd]);
    int mid = (l + r) >> 1;
    push(nd);
    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)));
}
ll getv(int x){
    return(cool.get(1, 0, n - 1, 0, x - 1));
}
void solve(){  
    cin >> n >> q;
    for(int i = 1; i <= n; i++)cin >> a[i];
    for(int i = 0; i < n; i++){
        d[i] = a[i + 1] - a[i];
        upd(1, 1, n - 1, i); cool.upd(1, 0, n - 1, i, d[i]);
    }
    while(q--){
        char idq; cin >> idq;
        if(idq == '*'){
            int l, r; cin >> l >> r;
            
            d[l - 1] = getv(l) * -1 - getv(l - 1);
            if(r + 1 <= n){
                d[r] = getv(r + 1) - getv(r) * -1;
            }
            if(r + 1 <= n){
                cool.upd(1, 0, n - 1, r, d[r]);
                upd(1, 1, n - 1, r);
            }
            cool.upd(1, 0, n - 1, l - 1, d[l - 1]);
            if(l - 1 >= 1)upd(1, 1, n - 1, l - 1);
            if(l <= r){
                upd2(1, 1, n - 1, l, r - 1); cool.upd2(1, 0, n - 1, l, r - 1);
            }
    
        }else if(idq == '+'){
            int l, r; ll v; cin >> l >> r >> v;
            d[l - 1] = (getv(l) + v) - getv(l - 1);
            if(r + 1 <= n){
                d[r] = getv(r + 1) - (getv(r) + v);
            }
            if(r + 1 <= n){
                cool.upd(1, 0, n - 1, r, d[r]);
                upd(1, 1, n - 1, r);
            }
            cool.upd(1, 0, n - 1, l - 1, d[l - 1]);
            if(l - 1 >= 1)upd(1, 1, n - 1, l - 1);
            
        }else{
            int l, r; cin >> l >> r;
            if(l == r){
                cout << 1 << "\n";
                continue;
            }
            cout << get(1, 1, n - 1, l, r - 1).cnt + r - l + 1 << "\n";
        }
        //for(int i = 1; i <= n; i++)cout << getv(i) << " ";
        //cout << "\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);
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 43556 KB Output is correct
2 Correct 14 ms 43612 KB Output is correct
3 Correct 13 ms 43612 KB Output is correct
4 Correct 13 ms 43728 KB Output is correct
5 Correct 12 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 13 ms 43704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 707 ms 57952 KB Output is correct
2 Correct 46 ms 44804 KB Output is correct
3 Correct 585 ms 58960 KB Output is correct
4 Correct 681 ms 58968 KB Output is correct
5 Correct 627 ms 58780 KB Output is correct
6 Correct 659 ms 58948 KB Output is correct
7 Correct 682 ms 57256 KB Output is correct
8 Correct 709 ms 58848 KB Output is correct
9 Correct 632 ms 58964 KB Output is correct
10 Correct 498 ms 58896 KB Output is correct
11 Correct 691 ms 58780 KB Output is correct
12 Correct 643 ms 58932 KB Output is correct
13 Correct 345 ms 58188 KB Output is correct
14 Correct 345 ms 58192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 43556 KB Output is correct
2 Correct 14 ms 43612 KB Output is correct
3 Correct 13 ms 43612 KB Output is correct
4 Correct 13 ms 43728 KB Output is correct
5 Correct 12 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 13 ms 43704 KB Output is correct
8 Correct 231 ms 49320 KB Output is correct
9 Correct 213 ms 49248 KB Output is correct
10 Correct 220 ms 49748 KB Output is correct
11 Correct 125 ms 49644 KB Output is correct
12 Correct 222 ms 50760 KB Output is correct
13 Correct 222 ms 50768 KB Output is correct
14 Correct 203 ms 51024 KB Output is correct
15 Correct 200 ms 49856 KB Output is correct
16 Correct 221 ms 50772 KB Output is correct
17 Correct 201 ms 51160 KB Output is correct
18 Correct 100 ms 48564 KB Output is correct
19 Correct 104 ms 48464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 43556 KB Output is correct
2 Correct 14 ms 43612 KB Output is correct
3 Correct 13 ms 43612 KB Output is correct
4 Correct 13 ms 43728 KB Output is correct
5 Correct 12 ms 43612 KB Output is correct
6 Correct 6 ms 43612 KB Output is correct
7 Correct 13 ms 43704 KB Output is correct
8 Correct 707 ms 57952 KB Output is correct
9 Correct 46 ms 44804 KB Output is correct
10 Correct 585 ms 58960 KB Output is correct
11 Correct 681 ms 58968 KB Output is correct
12 Correct 627 ms 58780 KB Output is correct
13 Correct 659 ms 58948 KB Output is correct
14 Correct 682 ms 57256 KB Output is correct
15 Correct 709 ms 58848 KB Output is correct
16 Correct 632 ms 58964 KB Output is correct
17 Correct 498 ms 58896 KB Output is correct
18 Correct 691 ms 58780 KB Output is correct
19 Correct 643 ms 58932 KB Output is correct
20 Correct 345 ms 58188 KB Output is correct
21 Correct 345 ms 58192 KB Output is correct
22 Correct 231 ms 49320 KB Output is correct
23 Correct 213 ms 49248 KB Output is correct
24 Correct 220 ms 49748 KB Output is correct
25 Correct 125 ms 49644 KB Output is correct
26 Correct 222 ms 50760 KB Output is correct
27 Correct 222 ms 50768 KB Output is correct
28 Correct 203 ms 51024 KB Output is correct
29 Correct 200 ms 49856 KB Output is correct
30 Correct 221 ms 50772 KB Output is correct
31 Correct 201 ms 51160 KB Output is correct
32 Correct 100 ms 48564 KB Output is correct
33 Correct 104 ms 48464 KB Output is correct
34 Correct 5 ms 43356 KB Output is correct
35 Correct 5 ms 43356 KB Output is correct
36 Correct 763 ms 59744 KB Output is correct
37 Correct 740 ms 63060 KB Output is correct
38 Correct 381 ms 60892 KB Output is correct
39 Correct 751 ms 62912 KB Output is correct
40 Correct 715 ms 59732 KB Output is correct
41 Correct 687 ms 62988 KB Output is correct
42 Correct 674 ms 59988 KB Output is correct
43 Correct 723 ms 62800 KB Output is correct
44 Correct 718 ms 62768 KB Output is correct
45 Correct 724 ms 62800 KB Output is correct
46 Correct 653 ms 63072 KB Output is correct
47 Correct 414 ms 62032 KB Output is correct