답안 #631922

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631922 2022-08-19T07:22:02 Z AA_Surely ZIGZAG (INOI20_zigzag) C++14
100 / 100
1193 ms 98832 KB
#include <bits/stdc++.h>

#define FOR(i, x, n) for(int i = x; i < n; i++)
#define F0R(i, n) FOR(i, 0, n)
#define ROF(i, x, n) for(int i = n - 1; i >= x; i--)
#define R0F(i, n) ROF(i, 0, n)

#define WTF cout << "WTF" << endl

#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
#define PB push_back

#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define U(x) (x < 0 ? -1 : (x == 0 ? 0 : 1))

using namespace std;

typedef long long LL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;

#define lc now << 1
#define rc now << 1 | 1
#define endl '\n'

const int N = 3e5 + 2;

LL n, q;
LL ns[N];

struct NumTree {
    PLL lz[N << 2];
    
    void build() {
        F0R(i, (N << 2)) lz[i].F = 1;
        return;
    }

    void addLz(int now, PLL v) {
        lz[now].F *= v.F;
        lz[now].S *= v.F;
        lz[now].S += v.S;
        return;
    }

    void shift(int now) {
        addLz(lc, lz[now]);
        addLz(rc, lz[now]);

        lz[now].F = 1; lz[now].S = 0;
    }

    void upd(int lq, int rq, PLL val, int now = 1, int ls = 0, int rs = n - 1) {
        if (rq < lq || rq < ls || rs < lq) return;
        if (lq <= ls && rs <= rq) return addLz(now, val);
        int mid = (ls + rs) >> 1;
        shift(now);
        upd(lq, rq, val, lc, ls, mid); upd(lq, rq, val, rc, mid + 1, rs);
        return;
    }

    LL get(int id, int now = 1, int ls = 0, int rs = n - 1) {
        if (ls == rs) return ns[ls] * lz[now].F + lz[now].S;
        int mid = (ls + rs) >> 1;
        shift(now);
        if (id <= mid) return get(id, lc, ls, mid);
        return get(id, rc, mid + 1, rs);
    }
} num;

struct Node {
    LL lft, rgt;
    LL pref, suff, len;
    LL ans;
};

inline Node operator + (const Node &a, const Node &b) {
    if (!a.len) return b;
    if (!b.len) return a;

    Node c;

    c.lft = a.lft; 
    c.rgt = b.rgt;
    c.len = a.len + b.len;
    c.pref = a.pref + (a.pref == a.len && (!!U(a.rgt) && !!U(b.lft)) && U(a.rgt) != U(b.lft) ? b.pref : 0LL);
    c.suff = b.suff + (b.suff == b.len && (!!U(a.rgt) && !!U(b.lft)) && U(a.rgt) != U(b.lft)? a.suff : 0LL);
    c.ans = a.ans + b.ans + ((!!U(a.rgt) && !!U(b.lft)) && U(a.rgt) != U(b.lft) ? a.suff * b.pref : 0LL);

    return c;
}

struct ZigZagTree {
    Node tree[N << 2];
    PLL lz[N << 2];

    void addLz(int now, PLL v) {
        tree[now].lft *= v.F;
        tree[now].lft += v.S;

        tree[now].rgt *= v.F;
        tree[now].rgt += v.S;
        
        lz[now].F *= v.F;
        lz[now].S *= v.F;
        lz[now].S += v.S;
        return;
    }

    inline void shift(int now) {
        addLz(lc, lz[now]);
        addLz(rc, lz[now]);

        lz[now] = {1, 0};
        return;
    }

    void build(int now = 1, int ls = 0, int rs = n - 2) {
        if (rs < ls) return;
        if (ls == rs) {
            lz[now].F = 1;
            tree[now].lft = tree[now].rgt = ns[ls + 1] - ns[ls];
            tree[now].len = 1;
            tree[now].suff = tree[now].pref = tree[now].ans = (!!U(ns[ls + 1] - ns[ls]));
            return;
        }
    
        int mid = (ls + rs) >> 1;
        lz[now].F = 1;
        build(lc, ls, mid); build(rc, mid + 1, rs);
        tree[now] = tree[lc] + tree[rc];

        return;
    }

    void change(int id, LL val, int now = 1, int ls = 0, int rs = n - 2) {
        if (rs < ls) return;
        if (ls == rs) {
            tree[now].lft = tree[now].rgt = val;
            tree[now].pref = tree[now].suff = tree[now].ans = !!val;
            return;
        }

        int mid = (ls + rs) >> 1;
        shift(now);

        if (id <= mid) change(id, val, lc, ls, mid);
        else change(id, val, rc, mid + 1, rs);

        tree[now] = tree[lc] + tree[rc];
        return;
    }

    void mult(int lq, int rq, int now = 1, int ls = 0, int rs = n - 2) {
        if (rs < ls) return;
        if (rq < lq || rq < ls || rs < lq) return;
        if (lq <= ls && rs <= rq) return addLz(now, {-1, 0});

        int mid = (ls + rs) >> 1;
        shift(now);

        mult(lq, rq, lc, ls, mid); mult(lq, rq, rc, mid + 1, rs);
        tree[now] = tree[lc] + tree[rc];
        return;
    }

    Node get(int lq, int rq, int now = 1, int ls = 0, int rs = n - 2) {
        if (rs < ls || rq < lq || rq < ls || rs < lq) return tree[0];
        if (lq <= ls && rs <= rq) return tree[now];
        int mid = (ls + rs) >> 1;
        shift(now);
        return get(lq, rq, lc, ls, mid) + get(lq, rq, rc, mid + 1, rs);
    }

} zig;

void init() {
    cin >> n >> q;
    F0R(i, n) cin >> ns[i];

    num.build();
    zig.build();

    return;
}

void cross() {
    int l, r; 
    cin >> l >> r;
    l--; r--;

    num.upd(l, r, {-1, 0});
    zig.mult(l, r - 1);
    
    if (l) {
        LL p1 = num.get(l - 1);
        LL p2 = num.get(l);
        zig.change(l - 1, p2 - p1);
    }

    if (r != n - 1) {
        LL p1 = num.get(r);
        LL p2 = num.get(r + 1);
        zig.change(r, p2 - p1);
    }

    return;
}

void pls() {
    int l, r, v;
    cin >> l >> r >> v;
    l--; r--;

    num.upd(l, r, {1, v});

    if (l) {
        LL p1 = num.get(l - 1);
        LL p2 = num.get(l);
        zig.change(l - 1, p2 - p1);
    }

    if (r != n - 1) {
        LL p1 = num.get(r);
        LL p2 = num.get(r + 1);
        zig.change(r, p2 - p1);
    }
}


void query() {
    int l, r;
    cin >> l >> r;
    l--; r--;
    
    cout << zig.get(l, r - 1).ans + (r - l + 1) << endl;
    return;
}

int main() {
    IOS;
    
    init();

    while(q--) {
        char tp; cin >> tp;
        if (tp == '*') cross();
        if (tp == '+') pls();
        if (tp == '?') query();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 38632 KB Output is correct
2 Correct 23 ms 38740 KB Output is correct
3 Correct 26 ms 38612 KB Output is correct
4 Correct 24 ms 38684 KB Output is correct
5 Correct 22 ms 38640 KB Output is correct
6 Correct 20 ms 37856 KB Output is correct
7 Correct 26 ms 38664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 890 ms 90148 KB Output is correct
2 Correct 71 ms 38096 KB Output is correct
3 Correct 736 ms 90284 KB Output is correct
4 Correct 886 ms 90236 KB Output is correct
5 Correct 762 ms 90228 KB Output is correct
6 Correct 933 ms 90280 KB Output is correct
7 Correct 838 ms 90252 KB Output is correct
8 Correct 921 ms 90380 KB Output is correct
9 Correct 813 ms 90184 KB Output is correct
10 Correct 621 ms 90276 KB Output is correct
11 Correct 759 ms 90200 KB Output is correct
12 Correct 875 ms 90328 KB Output is correct
13 Correct 473 ms 91152 KB Output is correct
14 Correct 430 ms 91092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 38632 KB Output is correct
2 Correct 23 ms 38740 KB Output is correct
3 Correct 26 ms 38612 KB Output is correct
4 Correct 24 ms 38684 KB Output is correct
5 Correct 22 ms 38640 KB Output is correct
6 Correct 20 ms 37856 KB Output is correct
7 Correct 26 ms 38664 KB Output is correct
8 Correct 315 ms 52756 KB Output is correct
9 Correct 284 ms 52812 KB Output is correct
10 Correct 308 ms 53912 KB Output is correct
11 Correct 197 ms 53464 KB Output is correct
12 Correct 290 ms 53888 KB Output is correct
13 Correct 295 ms 53836 KB Output is correct
14 Correct 314 ms 53856 KB Output is correct
15 Correct 292 ms 52856 KB Output is correct
16 Correct 296 ms 53912 KB Output is correct
17 Correct 276 ms 53912 KB Output is correct
18 Correct 133 ms 53828 KB Output is correct
19 Correct 140 ms 53796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 38632 KB Output is correct
2 Correct 23 ms 38740 KB Output is correct
3 Correct 26 ms 38612 KB Output is correct
4 Correct 24 ms 38684 KB Output is correct
5 Correct 22 ms 38640 KB Output is correct
6 Correct 20 ms 37856 KB Output is correct
7 Correct 26 ms 38664 KB Output is correct
8 Correct 890 ms 90148 KB Output is correct
9 Correct 71 ms 38096 KB Output is correct
10 Correct 736 ms 90284 KB Output is correct
11 Correct 886 ms 90236 KB Output is correct
12 Correct 762 ms 90228 KB Output is correct
13 Correct 933 ms 90280 KB Output is correct
14 Correct 838 ms 90252 KB Output is correct
15 Correct 921 ms 90380 KB Output is correct
16 Correct 813 ms 90184 KB Output is correct
17 Correct 621 ms 90276 KB Output is correct
18 Correct 759 ms 90200 KB Output is correct
19 Correct 875 ms 90328 KB Output is correct
20 Correct 473 ms 91152 KB Output is correct
21 Correct 430 ms 91092 KB Output is correct
22 Correct 315 ms 52756 KB Output is correct
23 Correct 284 ms 52812 KB Output is correct
24 Correct 308 ms 53912 KB Output is correct
25 Correct 197 ms 53464 KB Output is correct
26 Correct 290 ms 53888 KB Output is correct
27 Correct 295 ms 53836 KB Output is correct
28 Correct 314 ms 53856 KB Output is correct
29 Correct 292 ms 52856 KB Output is correct
30 Correct 296 ms 53912 KB Output is correct
31 Correct 276 ms 53912 KB Output is correct
32 Correct 133 ms 53828 KB Output is correct
33 Correct 140 ms 53796 KB Output is correct
34 Correct 15 ms 37844 KB Output is correct
35 Correct 15 ms 37912 KB Output is correct
36 Correct 1143 ms 95684 KB Output is correct
37 Correct 1193 ms 98704 KB Output is correct
38 Correct 580 ms 97636 KB Output is correct
39 Correct 1056 ms 98624 KB Output is correct
40 Correct 999 ms 95608 KB Output is correct
41 Correct 950 ms 98696 KB Output is correct
42 Correct 927 ms 95804 KB Output is correct
43 Correct 1053 ms 98832 KB Output is correct
44 Correct 1119 ms 98608 KB Output is correct
45 Correct 1022 ms 98532 KB Output is correct
46 Correct 913 ms 98644 KB Output is correct
47 Correct 669 ms 98800 KB Output is correct