Submission #172577

# Submission time Handle Problem Language Result Execution time Memory
172577 2020-01-02T04:53:22 Z rama_pang Street Lamps (APIO19_street_lamps) C++14
100 / 100
1785 ms 83948 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 and 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
// using Data = int;
    int N;
    vector<vector<int>> 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);
        }
    }

    /* Range Update and 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:62: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 376 KB Output is correct
4 Correct 2 ms 248 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 270 ms 23216 KB Output is correct
2 Correct 319 ms 23272 KB Output is correct
3 Correct 535 ms 25072 KB Output is correct
4 Correct 1560 ms 60600 KB Output is correct
5 Correct 1530 ms 66932 KB Output is correct
6 Correct 1678 ms 62364 KB Output is correct
7 Correct 1725 ms 82768 KB Output is correct
8 Correct 1516 ms 70064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 476 KB Output is correct
2 Correct 4 ms 556 KB Output is correct
3 Correct 4 ms 508 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 1298 ms 54420 KB Output is correct
6 Correct 1585 ms 62292 KB Output is correct
7 Correct 1489 ms 65784 KB Output is correct
8 Correct 1167 ms 71284 KB Output is correct
9 Correct 181 ms 17316 KB Output is correct
10 Correct 218 ms 27880 KB Output is correct
11 Correct 215 ms 28432 KB Output is correct
12 Correct 1681 ms 83856 KB Output is correct
13 Correct 1524 ms 71400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 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 4 ms 504 KB Output is correct
5 Correct 1765 ms 70676 KB Output is correct
6 Correct 1508 ms 64416 KB Output is correct
7 Correct 1471 ms 62068 KB Output is correct
8 Correct 1162 ms 50164 KB Output is correct
9 Correct 369 ms 24908 KB Output is correct
10 Correct 276 ms 23756 KB Output is correct
11 Correct 376 ms 24780 KB Output is correct
12 Correct 276 ms 23728 KB Output is correct
13 Correct 366 ms 24916 KB Output is correct
14 Correct 278 ms 23628 KB Output is correct
15 Correct 1785 ms 83948 KB Output is correct
16 Correct 1234 ms 71336 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 376 KB Output is correct
4 Correct 2 ms 248 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 270 ms 23216 KB Output is correct
9 Correct 319 ms 23272 KB Output is correct
10 Correct 535 ms 25072 KB Output is correct
11 Correct 1560 ms 60600 KB Output is correct
12 Correct 1530 ms 66932 KB Output is correct
13 Correct 1678 ms 62364 KB Output is correct
14 Correct 1725 ms 82768 KB Output is correct
15 Correct 1516 ms 70064 KB Output is correct
16 Correct 4 ms 476 KB Output is correct
17 Correct 4 ms 556 KB Output is correct
18 Correct 4 ms 508 KB Output is correct
19 Correct 4 ms 504 KB Output is correct
20 Correct 1298 ms 54420 KB Output is correct
21 Correct 1585 ms 62292 KB Output is correct
22 Correct 1489 ms 65784 KB Output is correct
23 Correct 1167 ms 71284 KB Output is correct
24 Correct 181 ms 17316 KB Output is correct
25 Correct 218 ms 27880 KB Output is correct
26 Correct 215 ms 28432 KB Output is correct
27 Correct 1681 ms 83856 KB Output is correct
28 Correct 1524 ms 71400 KB Output is correct
29 Correct 4 ms 504 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 4 ms 504 KB Output is correct
33 Correct 1765 ms 70676 KB Output is correct
34 Correct 1508 ms 64416 KB Output is correct
35 Correct 1471 ms 62068 KB Output is correct
36 Correct 1162 ms 50164 KB Output is correct
37 Correct 369 ms 24908 KB Output is correct
38 Correct 276 ms 23756 KB Output is correct
39 Correct 376 ms 24780 KB Output is correct
40 Correct 276 ms 23728 KB Output is correct
41 Correct 366 ms 24916 KB Output is correct
42 Correct 278 ms 23628 KB Output is correct
43 Correct 1785 ms 83948 KB Output is correct
44 Correct 1234 ms 71336 KB Output is correct
45 Correct 125 ms 12788 KB Output is correct
46 Correct 347 ms 13168 KB Output is correct
47 Correct 862 ms 30464 KB Output is correct
48 Correct 1594 ms 64656 KB Output is correct
49 Correct 235 ms 28012 KB Output is correct
50 Correct 239 ms 28012 KB Output is correct
51 Correct 260 ms 28388 KB Output is correct
52 Correct 305 ms 29788 KB Output is correct
53 Correct 306 ms 29632 KB Output is correct
54 Correct 285 ms 29660 KB Output is correct