Submission #591730

# Submission time Handle Problem Language Result Execution time Memory
591730 2022-07-07T19:32:02 Z piOOE Street Lamps (APIO19_street_lamps) C++17
100 / 100
2878 ms 185764 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
 
#define all(x) begin(x), end(x)
#define mp make_pair
#define pb push_back
#define sz(x) ((int) size(x))
const int infI = 1e9 + 7;
const ll infL = 3e18;
 
template<typename T>
struct SegTreeDownMin {
    const T MAX = numeric_limits<T>::max() / 2;
    vector <T> t;
    int sz;
 
    void init(int n, T x = 0) {
        sz = n;
        t.assign(sz << 1, x);
    }
 
    void st(int i, T v) {
        int x = i + sz;
        t[x] = v;
        x >>= 1;
        while (x) {
            t[x] = min(t[x << 1], t[x << 1 | 1]);
            x >>= 1;
        }
    }
 
    T get(int l, int r) { // [l, r)
        T ans = MAX;
        l += sz;
        r += sz;
        while (l < r) {
            if (l & 1) {
                //it is a right child
                ans = min(ans, t[l++]);
            }
            if (r & 1) {
                //this is a left child
                ans = min(ans, t[--r]);
            }
            l >>= 1;
            r >>= 1;
        }
        return ans;
    }
 
};
 
template<typename T>
void make_uniq(vector<T> &v) {
    sort(all(v));
    v.resize(unique(all(v)) - begin(v));
}
 
struct Fenwick {
    vector<vector<ll>> t;
    vector<vector<int>> yy;
    int n;
 
 
    Fenwick() = default;
 
    Fenwick(int n) {
        init(n);
    }
 
    void init(int a) {
        n = a;
        yy.resize(n);
        for (int i = 0; i < n; ++i) yy[i].clear();
        t.resize(n);
    }
 
    void fake_add(int x, int y) {
        assert(x < n);
        assert(x >= 0);
        for (int i = x; i < n; i |= (i + 1))
            yy[i].pb(y);
    }
 
    void build() {
        for (int i = 0; i < n; ++i) {
            make_uniq(yy[i]);
            t[i].assign(sz(yy[i]) + 2, 0);
        }
    }
 
    void add(int x, int y, int v) {
        assert(x < n);
        assert(x >= 0);
        for (int i = x; i < n; i |= (i + 1))
            for (int j = lower_bound(all(yy[i]), y) - begin(yy[i]); j < sz(yy[i]); j |= (j + 1))
                t[i][j] += v;
    }
 
    ll get(int x, int y) {
        ll ans = 0;
        assert(x < n);
        for (int i = x; i > -1; i = ((i + 1) & i) - 1)
            for (int j = upper_bound(all(yy[i]), y) - begin(yy[i]) - 1; j > -1; j = ((j + 1) & j) - 1)
                ans += t[i][j];
        return ans;
    }
 
    void add(int x1, int y1, int x2, int y2, int v) {
        add(x1, y1, v);
        add(x1, y2 + 1, -v);
        add(x2 + 1, y1, -v);
        add(x2 + 1, y2 + 1, v);
    }
 
    void fake_add(int x1, int y1, int x2, int y2) {
        fake_add(x1, y1);
        fake_add(x1, y2 + 1);
        fake_add(x2 + 1, y1);
        fake_add(x2 + 1, y2 + 1);
    }
 
};
 
struct binary_fen {
    vector<int> t;
    int n;
 
    binary_fen() = default;
 
    binary_fen(int a) {
        n = a + 1;
        t.assign(n, 0);
    }
 
    void add(int i, int v) {
        assert(i > 0);
        for (; i < n; i += i & -i) {
            t[i] += v;
        }
    }
 
    int get(int i) {
        int ans = 0;
        for (; i > 0; i -= i & -i)
            ans += t[i];
        return ans;
    }
 
    int kth(int k) {
        if (k == 0) return -1;
        int i = 0;
        for (int j = 19; j >= 0; --j) {
            if (i + (1 << j) < n && t[i + (1 << j)] < k) {
                i += (1 << j);
                k -= t[i];
            }
        }
        return i;
    }
 
    pair<int, int> get_seg(int i) {
        int sump = get(i);
        int L = kth(sump) + 1;
        int R = kth(sump + 1);
        return mp(L, R);
    }
 
};
 
const int N = 600001;
 
int a[N], b[N], arr[N], L[N], R[N];
 
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    Fenwick t(n + 2);
    binary_fen fn(n);
    string ss;
    cin >> ss;
    vector<int> LL(n), RR(n);
    int sta = 0;
    for (int i = 0; i < n; ++i) {
        if (ss[i] == '1') {
            a[sta] = i;
            b[sta] = -1;
            ++sta;
        }
    }
    q += sta;
    for (int i = sta; i < q; ++i) {
        string s;
        cin >> s;
        if (s == "toggle") {
            cin >> a[i];
            --a[i];
            b[i] = -1;
        } else {
            cin >> a[i] >> b[i];
            --a[i], --b[i];
        }
    }
    for (int i = 1; i <= n; ++i) fn.add(i, 1);
    for (int i = 0; i < q; ++i) {
        if (b[i] == -1) {
            if (arr[a[i]] == 1) {
                tie(L[i], R[i]) = fn.get_seg(a[i]);
                t.fake_add(L[i], a[i] + 1, a[i], R[i]);
                arr[a[i]] = 0;
                fn.add(a[i] + 1, 1);
            } else {
                fn.add(a[i] + 1, -1);
                arr[a[i]] = 1;
                tie(L[i], R[i]) = fn.get_seg(a[i]);
                t.fake_add(L[i], a[i] + 1, a[i], R[i]);
            }
        }
    }
    t.build();
    SegTreeDownMin<int> st;
    st.init(n);
    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < q; ++i) {
        int tim;
        if (i < sta) tim = q - sta;
        else tim = q - i - 1;
        if (b[i] == -1) {
            if (arr[a[i]] == 1) {
                arr[a[i]] = 0;
                st.st(a[i], 0);
                t.add(L[i], a[i] + 1, a[i], R[i], -tim);
            } else {
                arr[a[i]] = 1;
                st.st(a[i], 1);
                t.add(L[i], a[i] + 1, a[i], R[i], tim);
            }
        } else {
            if (st.get(a[i], b[i])) {
                cout << t.get(a[i], b[i]) - tim << '\n';
            } else {
                cout << t.get(a[i], b[i]) << '\n';
            }
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 250 ms 22248 KB Output is correct
2 Correct 313 ms 24216 KB Output is correct
3 Correct 546 ms 31904 KB Output is correct
4 Correct 1984 ms 133176 KB Output is correct
5 Correct 2009 ms 137476 KB Output is correct
6 Correct 2173 ms 143028 KB Output is correct
7 Correct 169 ms 41128 KB Output is correct
8 Correct 1706 ms 185456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2900 KB Output is correct
2 Correct 3 ms 2900 KB Output is correct
3 Correct 4 ms 2900 KB Output is correct
4 Correct 4 ms 3028 KB Output is correct
5 Correct 2323 ms 131560 KB Output is correct
6 Correct 2198 ms 134372 KB Output is correct
7 Correct 1952 ms 136916 KB Output is correct
8 Correct 1786 ms 185560 KB Output is correct
9 Correct 78 ms 8628 KB Output is correct
10 Correct 84 ms 9156 KB Output is correct
11 Correct 90 ms 9216 KB Output is correct
12 Correct 185 ms 41336 KB Output is correct
13 Correct 1741 ms 185764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2900 KB Output is correct
2 Correct 3 ms 2900 KB Output is correct
3 Correct 4 ms 3028 KB Output is correct
4 Correct 4 ms 3024 KB Output is correct
5 Correct 1027 ms 97676 KB Output is correct
6 Correct 1574 ms 114756 KB Output is correct
7 Correct 2193 ms 142816 KB Output is correct
8 Correct 2878 ms 159508 KB Output is correct
9 Correct 390 ms 33708 KB Output is correct
10 Correct 351 ms 30952 KB Output is correct
11 Correct 393 ms 33872 KB Output is correct
12 Correct 341 ms 31592 KB Output is correct
13 Correct 393 ms 33836 KB Output is correct
14 Correct 350 ms 31788 KB Output is correct
15 Correct 201 ms 41220 KB Output is correct
16 Correct 1752 ms 185468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 250 ms 22248 KB Output is correct
9 Correct 313 ms 24216 KB Output is correct
10 Correct 546 ms 31904 KB Output is correct
11 Correct 1984 ms 133176 KB Output is correct
12 Correct 2009 ms 137476 KB Output is correct
13 Correct 2173 ms 143028 KB Output is correct
14 Correct 169 ms 41128 KB Output is correct
15 Correct 1706 ms 185456 KB Output is correct
16 Correct 3 ms 2900 KB Output is correct
17 Correct 3 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 4 ms 3028 KB Output is correct
20 Correct 2323 ms 131560 KB Output is correct
21 Correct 2198 ms 134372 KB Output is correct
22 Correct 1952 ms 136916 KB Output is correct
23 Correct 1786 ms 185560 KB Output is correct
24 Correct 78 ms 8628 KB Output is correct
25 Correct 84 ms 9156 KB Output is correct
26 Correct 90 ms 9216 KB Output is correct
27 Correct 185 ms 41336 KB Output is correct
28 Correct 1741 ms 185764 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 3 ms 2900 KB Output is correct
31 Correct 4 ms 3028 KB Output is correct
32 Correct 4 ms 3024 KB Output is correct
33 Correct 1027 ms 97676 KB Output is correct
34 Correct 1574 ms 114756 KB Output is correct
35 Correct 2193 ms 142816 KB Output is correct
36 Correct 2878 ms 159508 KB Output is correct
37 Correct 390 ms 33708 KB Output is correct
38 Correct 351 ms 30952 KB Output is correct
39 Correct 393 ms 33872 KB Output is correct
40 Correct 341 ms 31592 KB Output is correct
41 Correct 393 ms 33836 KB Output is correct
42 Correct 350 ms 31788 KB Output is correct
43 Correct 201 ms 41220 KB Output is correct
44 Correct 1752 ms 185468 KB Output is correct
45 Correct 101 ms 13540 KB Output is correct
46 Correct 155 ms 14736 KB Output is correct
47 Correct 876 ms 55336 KB Output is correct
48 Correct 1986 ms 131708 KB Output is correct
49 Correct 91 ms 9612 KB Output is correct
50 Correct 89 ms 9720 KB Output is correct
51 Correct 92 ms 9500 KB Output is correct
52 Correct 98 ms 9608 KB Output is correct
53 Correct 96 ms 9664 KB Output is correct
54 Correct 102 ms 9636 KB Output is correct