답안 #928124

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
928124 2024-02-15T21:04:50 Z TAhmed33 가로등 (APIO19_street_lamps) C++
100 / 100
4102 ms 385316 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
struct Implicit {
    vector <int> tree, tl, tr; int cnt;
    int create () {
        ++cnt; tl.push_back(-1); tr.push_back(-1); tree.push_back(0); return cnt;
    }
    void init () {
        tree.push_back(0); tl.push_back(-1); tr.push_back(-1); cnt = 0;
    }
    void update (int l, int r, int a, int b, int node) {
        if (l == r) {
            tree[node] += b; return;
        }
        int mid = (l + r) >> 1;
        if (a <= mid) {
            if (tl[node] == -1) {
                int z = create(); tl[node] = z;
            } else tree[node] -= tree[tl[node]];
            update(l, mid, a, b, tl[node]);
            tree[node] += tree[tl[node]];
        } else {
            if (tr[node] == -1) {
                int z = create(); tr[node] = z; 
            }
            else tree[node] -= tree[tr[node]];
            update(mid + 1, r, a, b, tr[node]);
            tree[node] += tree[tr[node]];
        }
    }
    int get (int l, int r, int a, int b, int node) {
        if (l > b || r < a) return 0;
        if (l >= a && r <= b) return tree[node];
        int sum = 0, mid = (l + r) >> 1;
        if (tl[node] != -1) sum += get(l, mid, a, b, tl[node]);
        if (tr[node] != -1) sum += get(mid + 1, r, a, b, tr[node]);
        return sum;
    }
};
typedef long long ll;
const int MAXN = 3e5 + 25;
bool a[MAXN];
int n, q;
set <array <int, 3>> cur;
Implicit tree[MAXN << 1];
void init () {
    for (int i = 1; i <= n; i++) {
        if (!a[i]) continue;
        int j = i; while (j + 1 <= n && a[j + 1]) j++;
        cur.insert({i, j, 0});
        i = j;
    }
}   
void update (int l, int r, int a, int b, int c, int node) {
    tree[node].update(1, n, b, c, 0);
    if (l == r) return;
    int mid = (l + r) >> 1, tl = node + 1, tr = node + 2 * (mid - l + 1);
    if (a <= mid) update(l, mid, a, b, c, tl); 
    else update(mid + 1, r, a, b, c, tr);
}
int dfs (int l, int r, int a, int b, int c, int node) {
    if (l > b || r < a) return 0;
    if (l >= a && r <= b) return tree[node].get(1, n, c, n, 0);
    int mid = (l + r) >> 1, tl = node + 1, tr = node + 2 * (mid - l + 1);
    return dfs(l, mid, a, b, c, tl) + dfs(mid + 1, r, a, b, c, tr);
}
void add (array <int, 3> x, int y) {
    cur.erase(x);
    update(1, n, x[0], x[1], y - x[2], 1);
}
void insert (array <int, 3> x) {
    cur.insert(x);
}
int query (int l, int r, int x) { 
    int sum = dfs(1, n, 1, l, r, 1);
    auto z = cur.upper_bound({l, n + 1, 0});
    if (z != cur.begin()) {
        z--; auto g = *z; 
        if (g[0] <= l && r <= g[1]) sum += x - g[2];
    }
    return sum;
}
int main () {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> q;
    for (int i = 1; i < 2 * n; i++) tree[i].init();
    for (int i = 1; i <= n; i++) {
        char x; cin >> x; a[i] = x == '1';
    }
    init();
    for (int t = 1; t <= q; t++) {
        string x; cin >> x;
        if (x == "query") {
            int l, r; cin >> l >> r; r--;
            cout << query(l, r, t) << '\n';
        } else {
            int x; cin >> x; a[x] ^= 1;
            if (!a[x]) {
                auto z = *(--cur.lower_bound({x + 1, 0, 0}));
                add(z, t); 
                auto l = z; l[2] = t; l[1] = x - 1;
                if (l[0] <= l[1]) insert(l);
                l = z; l[2] = t; l[0] = x + 1;
                if (l[0] <= l[1]) insert(l);
            } else {
                int mn = x, mx = x;
                if (x < n && a[x + 1]) {
                    auto z = *(cur.lower_bound({x + 1, x + 1, 0}));
                    add(z, t);
                    mx = z[1];
                }
                if (x > 1 && a[x - 1]) {
                    auto z = *(--cur.lower_bound({x, x, 0}));
                    add(z, t);
                    mn = z[0];
                }
                array <int, 3> z = {mn, mx, t};
                insert(z);
            }
        }   
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 47184 KB Output is correct
2 Correct 10 ms 47192 KB Output is correct
3 Correct 10 ms 47196 KB Output is correct
4 Correct 10 ms 47452 KB Output is correct
5 Correct 10 ms 47452 KB Output is correct
6 Correct 11 ms 47432 KB Output is correct
7 Correct 10 ms 47236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 51616 KB Output is correct
2 Correct 227 ms 52236 KB Output is correct
3 Correct 453 ms 59072 KB Output is correct
4 Correct 2488 ms 301900 KB Output is correct
5 Correct 3090 ms 329092 KB Output is correct
6 Correct 3001 ms 314600 KB Output is correct
7 Correct 300 ms 110688 KB Output is correct
8 Correct 297 ms 111784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 47960 KB Output is correct
2 Correct 13 ms 47964 KB Output is correct
3 Correct 12 ms 47960 KB Output is correct
4 Correct 10 ms 47452 KB Output is correct
5 Correct 4102 ms 385316 KB Output is correct
6 Correct 3827 ms 372480 KB Output is correct
7 Correct 2736 ms 328200 KB Output is correct
8 Correct 302 ms 111956 KB Output is correct
9 Correct 83 ms 50960 KB Output is correct
10 Correct 88 ms 51280 KB Output is correct
11 Correct 90 ms 51492 KB Output is correct
12 Correct 292 ms 110372 KB Output is correct
13 Correct 292 ms 111952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 47448 KB Output is correct
2 Correct 12 ms 47708 KB Output is correct
3 Correct 12 ms 47860 KB Output is correct
4 Correct 13 ms 47964 KB Output is correct
5 Correct 517 ms 126572 KB Output is correct
6 Correct 1577 ms 253564 KB Output is correct
7 Correct 2709 ms 313880 KB Output is correct
8 Correct 4026 ms 349888 KB Output is correct
9 Correct 243 ms 51360 KB Output is correct
10 Correct 204 ms 50360 KB Output is correct
11 Correct 244 ms 51404 KB Output is correct
12 Correct 196 ms 50508 KB Output is correct
13 Correct 249 ms 51532 KB Output is correct
14 Correct 190 ms 50500 KB Output is correct
15 Correct 279 ms 110496 KB Output is correct
16 Correct 288 ms 111804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 47184 KB Output is correct
2 Correct 10 ms 47192 KB Output is correct
3 Correct 10 ms 47196 KB Output is correct
4 Correct 10 ms 47452 KB Output is correct
5 Correct 10 ms 47452 KB Output is correct
6 Correct 11 ms 47432 KB Output is correct
7 Correct 10 ms 47236 KB Output is correct
8 Correct 163 ms 51616 KB Output is correct
9 Correct 227 ms 52236 KB Output is correct
10 Correct 453 ms 59072 KB Output is correct
11 Correct 2488 ms 301900 KB Output is correct
12 Correct 3090 ms 329092 KB Output is correct
13 Correct 3001 ms 314600 KB Output is correct
14 Correct 300 ms 110688 KB Output is correct
15 Correct 297 ms 111784 KB Output is correct
16 Correct 13 ms 47960 KB Output is correct
17 Correct 13 ms 47964 KB Output is correct
18 Correct 12 ms 47960 KB Output is correct
19 Correct 10 ms 47452 KB Output is correct
20 Correct 4102 ms 385316 KB Output is correct
21 Correct 3827 ms 372480 KB Output is correct
22 Correct 2736 ms 328200 KB Output is correct
23 Correct 302 ms 111956 KB Output is correct
24 Correct 83 ms 50960 KB Output is correct
25 Correct 88 ms 51280 KB Output is correct
26 Correct 90 ms 51492 KB Output is correct
27 Correct 292 ms 110372 KB Output is correct
28 Correct 292 ms 111952 KB Output is correct
29 Correct 11 ms 47448 KB Output is correct
30 Correct 12 ms 47708 KB Output is correct
31 Correct 12 ms 47860 KB Output is correct
32 Correct 13 ms 47964 KB Output is correct
33 Correct 517 ms 126572 KB Output is correct
34 Correct 1577 ms 253564 KB Output is correct
35 Correct 2709 ms 313880 KB Output is correct
36 Correct 4026 ms 349888 KB Output is correct
37 Correct 243 ms 51360 KB Output is correct
38 Correct 204 ms 50360 KB Output is correct
39 Correct 244 ms 51404 KB Output is correct
40 Correct 196 ms 50508 KB Output is correct
41 Correct 249 ms 51532 KB Output is correct
42 Correct 190 ms 50500 KB Output is correct
43 Correct 279 ms 110496 KB Output is correct
44 Correct 288 ms 111804 KB Output is correct
45 Correct 69 ms 49748 KB Output is correct
46 Correct 111 ms 49896 KB Output is correct
47 Correct 1126 ms 152876 KB Output is correct
48 Correct 2529 ms 301876 KB Output is correct
49 Correct 108 ms 51800 KB Output is correct
50 Correct 110 ms 51456 KB Output is correct
51 Correct 98 ms 51496 KB Output is correct
52 Correct 126 ms 52020 KB Output is correct
53 Correct 100 ms 52044 KB Output is correct
54 Correct 100 ms 51856 KB Output is correct