답안 #635080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635080 2022-08-25T11:49:19 Z K4YAN ZIGZAG (INOI20_zigzag) C++17
100 / 100
951 ms 94248 KB
//Be Name Khoda

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define all(x) x.begin(), x.end()

const ll mxn = 3e5 + 16, INF = 1e16 + 16;
ll n, t, q, w;
vector<ll> g;
char c;

inline void input() {

    cin >> n >> t;
    for(int i = 0; i < n; i++) {
        cin >> q;
        g.push_back(q);
    }

}

struct item {
    ll sz, ans, L, R, bp, bs, kp, ks;
};
struct segtree {

    int sz = 1;
    item NE = {0, 0, 0, 0, 0, 0, 0, 0};
    vector<item> v;
    vector<ll> lz, ls;

    void init(ll n) {
        while(sz < n) sz <<= 1;
        v.assign(2 * sz, NE);
        lz.assign(2 * sz, 1);
        ls.assign(2 * sz, 0);
        return;
    }
    void shift(int x, int lx, int rx) {
        if(lz[x] == 1 && ls[x] == 0) return;
        if(lz[x] == -1) {
            v[x].L *= -1, v[x].R *= -1;
            swap(v[x].bp, v[x].kp);
            swap(v[x].bs, v[x].ks);
            if(rx - lx > 1) {
                lz[2 * x + 1] *= -1, lz[2 * x + 2] *= -1;
                ls[2 * x + 1] *= -1, ls[2 * x + 2] *= -1;
            } lz[x] = 1;
        } if(ls[x] != 0) {
            v[x].L += ls[x], v[x].R += ls[x];
            if(rx - lx > 1) {
                ls[2 * x + 1] += ls[x], ls[2 * x + 2] += ls[x];
            } ls[x] = 0;
        } return;
    }//Shift Vaghean doroste
    item merge(item a, item b) {
        if(a.sz == 0) return b;
        if(b.sz == 0) return a;
        ll s = a.sz + b.sz, L = a.L, R = b.R, ans = a.ans + b.ans, bp, bs, kp, ks;
        if(a.R == b.L) {
            kp = a.kp, ks = b.ks, bp = a.bp, bs = b.bs;
            return {s, ans, L, R, bp, bs, kp, ks};
        }
        if(a.R > b.L) {
            ans += (1LL * a.bs * b.kp);
            if(a.sz & 1) {
                if(a.sz == a.bp) {
                    bp = a.sz + b.kp, kp = 1;
                } else {
                    bp = a.bp, kp = a.kp;
                }
            } else {
                if(a.sz == a.kp) {
                    kp = a.sz + b.kp, bp = 1;
                } else {
                    bp = a.bp, kp = a.kp;
                }
            }
            if(b.sz & 1) {
                if(b.sz == b.ks) {
                    ks = b.sz + a.bs, bs = 1;
                } else {
                    ks = b.ks, bs = b.bs;
                }
            } else {
                if(b.sz == b.bs) {
                    bs = b.sz + a.bs, ks = 1;
                } else {
                    ks = b.ks, bs = b.bs;
                }
            }
            return {s, ans, L, R, bp, bs, kp, ks};
        }
        if(a.R < b.L) {
            ans += (1LL * a.ks * b.bp);
            if(a.sz & 1) {
                if(a.sz == a.kp) {
                    kp = a.sz + b.bp, bp = 1;
                } else {
                    kp = a.kp, bp = a.bp;
                }
            } else {
                if(a.sz == a.bp) {
                    bp = a.sz + b.bp, kp = 1;
                } else {
                    bp = a.bp, kp = a.kp;
                }
            } 
            if(b.sz & 1) {
                if(b.sz == b.bs) {
                    bs = b.sz + a.ks, ks = 1;
                } else {
                    bs = b.bs, ks = b.ks;
                }
            } else {
                if(b.sz == b.ks) {
                    ks = b.sz + a.ks, bs = 1;
                } else {
                    bs = b.bs, ks = b.ks;
                }
            }
            return {s, ans, L, R, bp, bs, kp, ks};
        }
        return NE;
    }
    void build(vector<ll> &g, int x, int lx, int rx) {
        if(rx - lx == 1) {
            if(lx < int(g.size())) {
                v[x] = {1, 1, g[lx], g[lx], 1, 1, 1, 1};
            } return;
        }
        int m = (lx + rx) >> 1;
        build(g, 2 * x + 1, lx, m), build(g, 2 * x + 2, m, rx);
        v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
        return;
    }
    void build(vector<ll> & g) {
        build(g, 0, 0, sz);
        return;
    }
    void zarb(int l, int r, int x, int lx, int rx) {
        shift(x, lx, rx);
        if(rx <= l || r <= lx) return;
        if(l <= lx && rx <= r) {
            lz[x] = -1;
            shift(x, lx, rx);
            return;
        } int m = (lx + rx) >> 1;
        zarb(l, r, 2 * x + 1, lx, m);
        zarb(l, r, 2 * x + 2, m, rx);
        v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
        return;
    }
    void zarb(int l, int r) {
        zarb(l, r, 0, 0, sz);
        return;
    }
    void jam(int l, int r, int q, int x, int lx, int rx) {
        shift(x, lx, rx);
        if(rx <= l || r <= lx) return;
        if(l <= lx && rx <= r) {
            ls[x] += q;
            shift(x, lx, rx);
            return;
        } int m = (lx + rx) >> 1;
        jam(l, r, q, 2 * x + 1, lx, m);
        jam(l, r, q, 2 * x + 2, m, rx);
        v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
        return;
    }
    void jam(int l, int r, int q) {
        jam(l, r, q, 0, 0, sz);
        return;
    }
    item ask(int l, int r, int x, int lx, int rx) {
        shift(x, lx, rx);
        if(rx <= l || r <= lx) return NE;
        if(l <= lx && rx <= r) {
            return v[x];
        } int m = (lx + rx) >> 1;
        item ans = ask(l, r, 2 * x + 1, lx, m);
        ans = merge(ans, ask(l, r, 2 * x + 2, m, rx));
        v[x] = merge(v[2 * x + 1], v[2 * x + 2]);
        return ans;
    } 
    ll ask(int l, int r) {
        return ask(l, r, 0, 0, sz).ans;
    }

}; segtree st;

inline void solve() {

    st.init(n);
    st.build(g);
    for(int i = 0; i < t; i++) {
        cin >> c;
        if(c == '*') {
            cin >> q >> w; q--;
            st.zarb(q, w);
        } else if(c == '+') {
            ll v;
            cin >> q >> w >> v; q--;
            st.jam(q, w, v);
        } else {
            cin >> q >> w; q--;
            cout << st.ask(q, w) << "\n";
        }
    } return;

}

int main() {

    ios::sync_with_stdio(false); cin.tie(NULL);

    input(), solve();

    return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1620 KB Output is correct
2 Correct 10 ms 1860 KB Output is correct
3 Correct 9 ms 1748 KB Output is correct
4 Correct 8 ms 1856 KB Output is correct
5 Correct 9 ms 1820 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 7 ms 1740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 720 ms 85604 KB Output is correct
2 Correct 52 ms 2488 KB Output is correct
3 Correct 518 ms 93752 KB Output is correct
4 Correct 673 ms 93712 KB Output is correct
5 Correct 639 ms 93768 KB Output is correct
6 Correct 652 ms 93792 KB Output is correct
7 Correct 665 ms 90804 KB Output is correct
8 Correct 765 ms 93788 KB Output is correct
9 Correct 618 ms 93780 KB Output is correct
10 Correct 433 ms 93744 KB Output is correct
11 Correct 579 ms 93760 KB Output is correct
12 Correct 658 ms 93812 KB Output is correct
13 Correct 350 ms 93768 KB Output is correct
14 Correct 337 ms 93844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1620 KB Output is correct
2 Correct 10 ms 1860 KB Output is correct
3 Correct 9 ms 1748 KB Output is correct
4 Correct 8 ms 1856 KB Output is correct
5 Correct 9 ms 1820 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 7 ms 1740 KB Output is correct
8 Correct 286 ms 23668 KB Output is correct
9 Correct 238 ms 23628 KB Output is correct
10 Correct 242 ms 24568 KB Output is correct
11 Correct 147 ms 24328 KB Output is correct
12 Correct 273 ms 24736 KB Output is correct
13 Correct 245 ms 24636 KB Output is correct
14 Correct 210 ms 24648 KB Output is correct
15 Correct 237 ms 23704 KB Output is correct
16 Correct 268 ms 24748 KB Output is correct
17 Correct 212 ms 24668 KB Output is correct
18 Correct 105 ms 24636 KB Output is correct
19 Correct 99 ms 24564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1620 KB Output is correct
2 Correct 10 ms 1860 KB Output is correct
3 Correct 9 ms 1748 KB Output is correct
4 Correct 8 ms 1856 KB Output is correct
5 Correct 9 ms 1820 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 7 ms 1740 KB Output is correct
8 Correct 720 ms 85604 KB Output is correct
9 Correct 52 ms 2488 KB Output is correct
10 Correct 518 ms 93752 KB Output is correct
11 Correct 673 ms 93712 KB Output is correct
12 Correct 639 ms 93768 KB Output is correct
13 Correct 652 ms 93792 KB Output is correct
14 Correct 665 ms 90804 KB Output is correct
15 Correct 765 ms 93788 KB Output is correct
16 Correct 618 ms 93780 KB Output is correct
17 Correct 433 ms 93744 KB Output is correct
18 Correct 579 ms 93760 KB Output is correct
19 Correct 658 ms 93812 KB Output is correct
20 Correct 350 ms 93768 KB Output is correct
21 Correct 337 ms 93844 KB Output is correct
22 Correct 286 ms 23668 KB Output is correct
23 Correct 238 ms 23628 KB Output is correct
24 Correct 242 ms 24568 KB Output is correct
25 Correct 147 ms 24328 KB Output is correct
26 Correct 273 ms 24736 KB Output is correct
27 Correct 245 ms 24636 KB Output is correct
28 Correct 210 ms 24648 KB Output is correct
29 Correct 237 ms 23704 KB Output is correct
30 Correct 268 ms 24748 KB Output is correct
31 Correct 212 ms 24668 KB Output is correct
32 Correct 105 ms 24636 KB Output is correct
33 Correct 99 ms 24564 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 951 ms 91024 KB Output is correct
37 Correct 908 ms 94008 KB Output is correct
38 Correct 496 ms 92980 KB Output is correct
39 Correct 951 ms 94156 KB Output is correct
40 Correct 874 ms 91060 KB Output is correct
41 Correct 779 ms 94024 KB Output is correct
42 Correct 825 ms 91028 KB Output is correct
43 Correct 907 ms 94248 KB Output is correct
44 Correct 895 ms 94076 KB Output is correct
45 Correct 937 ms 94236 KB Output is correct
46 Correct 823 ms 94100 KB Output is correct
47 Correct 600 ms 94188 KB Output is correct