답안 #795846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
795846 2023-07-27T17:35:55 Z MISM06 ZIGZAG (INOI20_zigzag) C++14
100 / 100
946 ms 149444 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < int , pii > piii;
typedef pair < ll , ll > pll;
typedef pair < ll , pll > plll;
#define pb  push_back
#define all(x)  x.begin(), x.end()
#define sze     size()
#define F first
#define S second
#define kids    int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1;
#define wall__ cout << "________________________________________\n";

const ll N = 3e5 + 10, mod = 1e9 + 7;

struct node {
    ll ans, vl, vr, pre[2], suf[2], pis[2], sis[2], t, len;
    node () {
        ans = 0; vl = 0; vr = 0; pre[0] = pre[1] = 0; suf[0] = suf[1] = 0; pis[0] = pis[1] = 0; sis[0] = sis[1] = 0; t = 1;
        len = 0;
    }
    void set_len (int x) {
        len = x; t = len % 2; t ^= 1;
    }
    void merge (node x, node y) {
        len = x.len + y.len;
        set_len(len);
        vl = x.vl; vr = y.vr;
        ans = x.ans + y.ans;
        if (x.vr < y.vl) ans += x.suf[0] * y.pre[0];
        if (x.vr > y.vl) ans += x.suf[1] * y.pre[1];
        
        pre[0] = x.pre[0];
        
        pre[1] = x.pre[1];
        
        suf[0] = y.suf[0];
        
        suf[1] = y.suf[1];

        if (x.vr > y.vl) {
            if (x.t == 0) pre[0] += y.pre[1] * x.pis[0];
            
            if (x.t == 1) pre[1] += y.pre[1] * x.pis[1];
            
            if (y.t == 0) suf[0] += y.sis[0] * x.suf[1];
            
            if (y.t == 1) suf[1] += y.sis[1] * x.suf[1];
        } else if (x.vr < y.vl) {
            if (x.t == 1) pre[0] += y.pre[0] * x.pis[0];
            
            if (x.t == 0) pre[1] += y.pre[0] * x.pis[1];
            
            if (y.t == 1) suf[0] += y.sis[0] * x.suf[0];
            
            if (y.t == 0) suf[1] += y.sis[1] * x.suf[0];
        }
        
        pis[0] = pis[1] = 0; sis[0] = sis[1] = 0;
        if (x.vr > y.vl) {
            if (x.pis[0] && x.t == 0 && y.pis[1]) pis[0] = 1;
            if (x.pis[1] && x.t == 1 && y.pis[1]) pis[1] = 1;
            if (y.sis[0] && y.t == 0 && x.sis[1]) sis[0] = 1;
            if (y.sis[1] && y.t == 1 && x.sis[1]) sis[1] = 1;
        } else if (x.vr < y.vl) {
            if (x.pis[0] && x.t == 1 && y.pis[0]) pis[0] = 1;
            if (x.pis[1] && x.t == 0 && y.pis[0]) pis[1] = 1;
            if (y.sis[0] && y.t == 1 && x.sis[0]) sis[0] = 1;
            if (y.sis[1] && y.t == 0 && x.sis[0]) sis[1] = 1;
        }
    }
    void relax (ll x, ll y) {
        if (x == -1) {
            swap(pre[0], pre[1]);
            swap(suf[0], suf[1]);
            vl *= -1ll;
            vr *= -1ll;
            swap(sis[0], sis[1]);
            swap(pis[1], pis[0]);
        }
        vl += y;
        vr += y;
    }
    
    void make (ll x) {
        set_len(1);
        vl = x; vr = x;
        pre[0] = pre[1] = 1;
        suf[0] = suf[1] = 1;
        ans = 1;
        pis[0] = pis[1] = 1;
        sis[0] = sis[1] = 1;
    }
};

int n, a[N];
struct segtree {
    node seg[N << 2];
    ll la1[N << 2], la2[N << 2];
    void build (int v = 1, int tl = 1, int tr = n) {
        la1[v] = 1; la2[v] = 0;
        if (tl == tr) {
            seg[v].make(a[tl]);
            return;
        } kids;
        build(cl, tl, mid);
        build(cr, mid + 1, tr);
        seg[v].merge(seg[cl], seg[cr]);
    }
    void shift (int v, int tl, int tr) {
        seg[v].relax(la1[v], la2[v]);
        int cl = v << 1, cr = v << 1 | 1;
        if (tl != tr) {
            la1[cl] *= la1[v];
            la2[cl] *= la1[v];
            la2[cl] += la2[v];
            la1[cr] *= la1[v];
            la2[cr] *= la1[v];
            la2[cr] += la2[v];
        }
        la1[v] = 1;
        la2[v] = 0;
    }
    void upd (int s, ll val, int l, int r, int v = 1, int tl = 1, int tr = n) {
        shift(v, tl, tr);
        if (l > r) return;
        if (l == tl && r == tr) {
            if (s == 0) la2[v] += val;
            else la1[v] *= -1ll;
            shift(v, tl, tr);
            return;
        } kids
        upd(s, val, l, min(r, mid), cl, tl, mid);
        upd(s, val, max(l, mid + 1), r, cr , mid + 1, tr);
        seg[v].merge(seg[cl], seg[cr]);
    }
    node ask (int l, int r, int v = 1, int tl = 1, int tr = n) {
        shift(v, tl, tr);
        if (l == tl && r == tr) return seg[v];
        kids
        if (r <= mid) return ask(l, r, cl, tl, mid);
        else if (l > mid) return ask(l, r, cr, mid + 1, tr);
        else {
            node res;
            res.merge(ask(l, mid, cl, tl, mid), ask(mid + 1, r, cr, mid + 1, tr));
            return res;
        }
    }
} f;

void solve () {
    
    int q; cin >> n >> q;
    for (int i =1 ; i <= n; i++) cin >> a[i];
    f.build();
    while(q--) {
        char t; cin >> t; int l, r; cin >> l >> r;
        if (t == '?') {
            cout << f.ask(l, r).ans << '\n';
        } else if (t == '+') {
            ll x; cin >> x;
            f.upd(0, x, l, r);
        } else {
            f.upd(1, 0, l, r);
        }
    }
    
}   

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    solve();
    return 0;
}
/*
11 1
1 2 3 4 3 4 3 4 3 4 5
? 1 11
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 122828 KB Output is correct
2 Correct 52 ms 122760 KB Output is correct
3 Correct 55 ms 122752 KB Output is correct
4 Correct 58 ms 122744 KB Output is correct
5 Correct 56 ms 122776 KB Output is correct
6 Correct 52 ms 122460 KB Output is correct
7 Correct 60 ms 122968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 773 ms 144140 KB Output is correct
2 Correct 105 ms 124620 KB Output is correct
3 Correct 569 ms 148864 KB Output is correct
4 Correct 718 ms 148972 KB Output is correct
5 Correct 611 ms 148892 KB Output is correct
6 Correct 671 ms 148840 KB Output is correct
7 Correct 736 ms 145768 KB Output is correct
8 Correct 717 ms 148904 KB Output is correct
9 Correct 652 ms 148884 KB Output is correct
10 Correct 448 ms 148932 KB Output is correct
11 Correct 580 ms 148864 KB Output is correct
12 Correct 747 ms 148940 KB Output is correct
13 Correct 331 ms 148896 KB Output is correct
14 Correct 292 ms 148872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 122828 KB Output is correct
2 Correct 52 ms 122760 KB Output is correct
3 Correct 55 ms 122752 KB Output is correct
4 Correct 58 ms 122744 KB Output is correct
5 Correct 56 ms 122776 KB Output is correct
6 Correct 52 ms 122460 KB Output is correct
7 Correct 60 ms 122968 KB Output is correct
8 Correct 265 ms 128760 KB Output is correct
9 Correct 282 ms 128744 KB Output is correct
10 Correct 278 ms 129828 KB Output is correct
11 Correct 201 ms 129592 KB Output is correct
12 Correct 274 ms 129752 KB Output is correct
13 Correct 270 ms 129760 KB Output is correct
14 Correct 231 ms 129828 KB Output is correct
15 Correct 255 ms 128844 KB Output is correct
16 Correct 325 ms 129996 KB Output is correct
17 Correct 269 ms 129864 KB Output is correct
18 Correct 128 ms 129788 KB Output is correct
19 Correct 117 ms 129696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 122828 KB Output is correct
2 Correct 52 ms 122760 KB Output is correct
3 Correct 55 ms 122752 KB Output is correct
4 Correct 58 ms 122744 KB Output is correct
5 Correct 56 ms 122776 KB Output is correct
6 Correct 52 ms 122460 KB Output is correct
7 Correct 60 ms 122968 KB Output is correct
8 Correct 773 ms 144140 KB Output is correct
9 Correct 105 ms 124620 KB Output is correct
10 Correct 569 ms 148864 KB Output is correct
11 Correct 718 ms 148972 KB Output is correct
12 Correct 611 ms 148892 KB Output is correct
13 Correct 671 ms 148840 KB Output is correct
14 Correct 736 ms 145768 KB Output is correct
15 Correct 717 ms 148904 KB Output is correct
16 Correct 652 ms 148884 KB Output is correct
17 Correct 448 ms 148932 KB Output is correct
18 Correct 580 ms 148864 KB Output is correct
19 Correct 747 ms 148940 KB Output is correct
20 Correct 331 ms 148896 KB Output is correct
21 Correct 292 ms 148872 KB Output is correct
22 Correct 265 ms 128760 KB Output is correct
23 Correct 282 ms 128744 KB Output is correct
24 Correct 278 ms 129828 KB Output is correct
25 Correct 201 ms 129592 KB Output is correct
26 Correct 274 ms 129752 KB Output is correct
27 Correct 270 ms 129760 KB Output is correct
28 Correct 231 ms 129828 KB Output is correct
29 Correct 255 ms 128844 KB Output is correct
30 Correct 325 ms 129996 KB Output is correct
31 Correct 269 ms 129864 KB Output is correct
32 Correct 128 ms 129788 KB Output is correct
33 Correct 117 ms 129696 KB Output is correct
34 Correct 50 ms 122364 KB Output is correct
35 Correct 45 ms 122432 KB Output is correct
36 Correct 937 ms 146124 KB Output is correct
37 Correct 934 ms 149176 KB Output is correct
38 Correct 565 ms 148172 KB Output is correct
39 Correct 919 ms 149188 KB Output is correct
40 Correct 916 ms 146188 KB Output is correct
41 Correct 822 ms 149252 KB Output is correct
42 Correct 813 ms 146220 KB Output is correct
43 Correct 939 ms 149192 KB Output is correct
44 Correct 946 ms 149144 KB Output is correct
45 Correct 901 ms 149136 KB Output is correct
46 Correct 806 ms 149208 KB Output is correct
47 Correct 570 ms 149444 KB Output is correct