Submission #172578

# Submission time Handle Problem Language Result Execution time Memory
172578 2020-01-02T04:55:38 Z rama_pang Street Lamps (APIO19_street_lamps) C++14
100 / 100
1769 ms 78128 KB
#include <bits/stdc++.h>
using namespace std;

template<typename Data>
struct FenwickTree { // Range Update & Point Query
    int N;
    vector<Data> Tree;

    FenwickTree() { N = 0; Tree.clear(); }

    /* Standard Tree Updates */
    int resize(int n) {
        N = n;
        Tree.resize(N + 1);
    }

    void update_tree(int p, Data x) {
        for (int i = p; i <= N; i += i & -i) {
            Tree[i] += x;
        }
    }

    /* Range Update & Point Query */
    void update(int l, int r, Data x) {
        update_tree(l, x); // Update Prefix Difference
        update_tree(r + 1, -x); // Update Prefix Difference
    }

    Data query(int p) {
        Data res = 0;
        for (int i = p; i > 0; i -= i & -i) {
            res += Tree[i];
        }
        return res;
    }

};

template<typename Data>
struct FenwickTree2D { // 2D Range Update & Point Query
    int N;
    vector<vector<Data>> Coordinate;
    vector<FenwickTree<Data>> Tree;

    FenwickTree2D() { N = 0; Tree.clear(); }
    FenwickTree2D(int n) { N = n; resize(n); }

    /* Standard Tree Updates */
    void build() {
        for (int i = 1; i <= N; i++) {
            sort(begin(Coordinate[i]), end(Coordinate[i]));
            Coordinate[i].resize(unique(begin(Coordinate[i]), end(Coordinate[i])) - begin(Coordinate[i]));
            Tree[i].resize(Coordinate[i].size());
        }
    }

    int resize(int n) {
        N = n;
        Tree.resize(n + 1);
        Coordinate.resize(n + 1);
    }

    void insert_query_interval(int a, int b) {
        for (int i = a; i > 0; i -= i & -i) {
            Coordinate[i].emplace_back(b);
        }
    }

    void update_tree(int p, int l, int r, Data x) {
        for (int i = p; i <= N; i += i & -i) {
            int idx_l = lower_bound(begin(Coordinate[i]), end(Coordinate[i]), l) - begin(Coordinate[i]) + 1; // first element that is >= l
            int idx_r = prev(upper_bound(begin(Coordinate[i]), end(Coordinate[i]), r)) - begin(Coordinate[i]) + 1; // last element that is <= r
            Tree[i].update(idx_l, idx_r, x);
        }
    }

    /* 2D Range Update & Point Query Functions */
    void update(int lo, int hi, int l, int r, Data x) { // Range update rectangle (lo...hi) * (l...r)
        update_tree(lo, l, r, x); // Update Prefix Difference
        update_tree(hi + 1, l, r, -x); // Update Prefix Difference
    }

    Data query(int r, int c) {
        Data res = 0;
        for (int i = r; i > 0; i -= i & -i) {
            int idx = prev(upper_bound(begin(Coordinate[i]), end(Coordinate[i]), c)) - begin(Coordinate[i]) + 1; // last element that is <= r
            res += Tree[i].query(idx);
        }
        return res;
    }

};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int n, q;
    string s;

    cin >> n >> q;
    cin >> s;

    set<int> lamps; // lamps that are off - allows us to get range of consecutive on lamps
    FenwickTree2D<int> BIT(n);

    lamps.emplace(0);
    lamps.emplace(n + 1);
    for (int i = 1; i <= n; i++) {
        if (s[i - 1] == '0') {
            lamps.emplace(i);
        }
    }

    vector<tuple<string, int, int>> events;
    events.emplace_back("", 0, 0);

    for (int qi = 1; qi <= q; qi++) {
        string t;
        cin >> t;
        if (t == "toggle") {
            int i;
            cin >> i;
            events.emplace_back(t, i, 0);
        } else if (t == "query") {
            int a, b;
            cin >> a >> b;
            b--;
            events.emplace_back(t, a, b);
            BIT.insert_query_interval(a, b);
        }
    }

    BIT.build();

    for (int qi = 1; qi <= q; qi++) {
        string t;
        t = get<0>(events[qi]);

        if (t == "toggle") {
            int i;
            i = get<1>(events[qi]);
            if (lamps.count(i) == 0) { // the i-th lamp is on
                int l = *prev(lamps.lower_bound(i)) + 1;
                int r = *lamps.upper_bound(i) - 1;
                lamps.emplace(i); // turn off the lamp
                BIT.update(l, i, i, r, qi); // Add ending time (ans = End - Start)
            } else { // the i-th lamp is off
                int l = *prev(lamps.lower_bound(i)) + 1;
                int r = *lamps.upper_bound(i) - 1;
                lamps.erase(i); // turn on the lamp
                BIT.update(l, i, i, r, -qi); // Add start time (ans = End - Start)
            }
        } else if (t == "query") {
            int a, b;
            a = get<1>(events[qi]);
            b = get<2>(events[qi]);

            int l = *prev(lamps.upper_bound(a)) + 1;
            int r = *lamps.upper_bound(a) - 1;
            int ans = BIT.query(a, b) + ((l <= a && b <= r)? qi : 0); // Add current time if range doesn't have ending time yet

            cout << ans << "\n";
        }
    }

    return 0;
}

Compilation message

street_lamps.cpp: In member function 'int FenwickTree<Data>::resize(int)':
street_lamps.cpp:15:5: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
street_lamps.cpp: In member function 'int FenwickTree2D<Data>::resize(int)':
street_lamps.cpp:61:5: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 23140 KB Output is correct
2 Correct 318 ms 23400 KB Output is correct
3 Correct 537 ms 25168 KB Output is correct
4 Correct 1564 ms 59900 KB Output is correct
5 Correct 1493 ms 61472 KB Output is correct
6 Correct 1447 ms 57232 KB Output is correct
7 Correct 1745 ms 76960 KB Output is correct
8 Correct 1206 ms 64324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 1016 ms 52768 KB Output is correct
6 Correct 1410 ms 57384 KB Output is correct
7 Correct 1475 ms 60484 KB Output is correct
8 Correct 1195 ms 65448 KB Output is correct
9 Correct 181 ms 14568 KB Output is correct
10 Correct 206 ms 24936 KB Output is correct
11 Correct 215 ms 25204 KB Output is correct
12 Correct 1716 ms 77960 KB Output is correct
13 Correct 1187 ms 65468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 10 ms 508 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 1602 ms 70832 KB Output is correct
6 Correct 1522 ms 64372 KB Output is correct
7 Correct 1397 ms 57100 KB Output is correct
8 Correct 1160 ms 45840 KB Output is correct
9 Correct 363 ms 21808 KB Output is correct
10 Correct 272 ms 21068 KB Output is correct
11 Correct 365 ms 21796 KB Output is correct
12 Correct 274 ms 21072 KB Output is correct
13 Correct 368 ms 21840 KB Output is correct
14 Correct 268 ms 21068 KB Output is correct
15 Correct 1769 ms 78128 KB Output is correct
16 Correct 1245 ms 65236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 263 ms 23140 KB Output is correct
9 Correct 318 ms 23400 KB Output is correct
10 Correct 537 ms 25168 KB Output is correct
11 Correct 1564 ms 59900 KB Output is correct
12 Correct 1493 ms 61472 KB Output is correct
13 Correct 1447 ms 57232 KB Output is correct
14 Correct 1745 ms 76960 KB Output is correct
15 Correct 1206 ms 64324 KB Output is correct
16 Correct 3 ms 504 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 1016 ms 52768 KB Output is correct
21 Correct 1410 ms 57384 KB Output is correct
22 Correct 1475 ms 60484 KB Output is correct
23 Correct 1195 ms 65448 KB Output is correct
24 Correct 181 ms 14568 KB Output is correct
25 Correct 206 ms 24936 KB Output is correct
26 Correct 215 ms 25204 KB Output is correct
27 Correct 1716 ms 77960 KB Output is correct
28 Correct 1187 ms 65468 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 10 ms 508 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 1602 ms 70832 KB Output is correct
34 Correct 1522 ms 64372 KB Output is correct
35 Correct 1397 ms 57100 KB Output is correct
36 Correct 1160 ms 45840 KB Output is correct
37 Correct 363 ms 21808 KB Output is correct
38 Correct 272 ms 21068 KB Output is correct
39 Correct 365 ms 21796 KB Output is correct
40 Correct 274 ms 21072 KB Output is correct
41 Correct 368 ms 21840 KB Output is correct
42 Correct 268 ms 21068 KB Output is correct
43 Correct 1769 ms 78128 KB Output is correct
44 Correct 1245 ms 65236 KB Output is correct
45 Correct 122 ms 11284 KB Output is correct
46 Correct 200 ms 11756 KB Output is correct
47 Correct 826 ms 27224 KB Output is correct
48 Correct 1478 ms 59472 KB Output is correct
49 Correct 233 ms 24812 KB Output is correct
50 Correct 238 ms 24896 KB Output is correct
51 Correct 249 ms 25060 KB Output is correct
52 Correct 280 ms 26076 KB Output is correct
53 Correct 281 ms 26004 KB Output is correct
54 Correct 292 ms 26076 KB Output is correct