Submission #172566

# Submission time Handle Problem Language Result Execution time Memory
172566 2020-01-02T03:26:59 Z rama_pang Street Lamps (APIO19_street_lamps) C++14
20 / 100
454 ms 524292 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;
        }
    }

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

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

    int query(int id) {
        return query_tree(id);
    }

};

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

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

    /* Standard Tree Updates */
    int resize(int n) {
        N = n;
        Tree.resize(n + 1);
        for (int i = 1; i <= N; i++) {
            Tree[i].resize(n + 1);
        }
    }

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

    Data query_tree(int r, int c) {
        Data res = 0;
        for (int i = r; i > 0; i -= i & -i) {
            res += Tree[i].query(c);
        }
        return res;
    }

    /* 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_tree(hi + 1, l, r, -x);
    }

    int query(int r, int c) { // Point query
        return query_tree(r, c);
    }

};

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

    int n, q;
    cin >> n >> q;
    
    string s;
    cin >> s;

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

    for (int i = 0; i < s.size(); i++) {
        if (s[i] == '1') {
            state[i + 1] = 1;
        }
    }

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

    for (int qi = 1; qi <= q; qi++) {
        string t;
        cin >> t;

        if (t == "toggle") {
            int i;
            cin >> i;

            if (state[i]) {
                int l = *prev(lamps.lower_bound(i)) + 1;
                int r = *lamps.upper_bound(i) - 1;
                BIT.update(l, i, i, r, qi); // Add ending time (ans = End - Start)

                lamps.emplace(i);
                state[i] = 0;
            } else {
                lamps.erase(i);
                state[i] = 1;

                int l = *prev(lamps.lower_bound(i)) + 1;
                int r = *lamps.upper_bound(i) - 1;
                BIT.update(l, i, i, r, -qi); // Add start time (ans = End - Start)
            }
        } else if (t == "query") {
            int a, b;
            cin >> a >> b;
            b--;

            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 qi 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:58:5: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:100:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < s.size(); i++) {
                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 248 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 4620 KB Output is correct
2 Correct 197 ms 5240 KB Output is correct
3 Correct 454 ms 103548 KB Output is correct
4 Runtime error 447 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4344 KB Output is correct
2 Correct 7 ms 4344 KB Output is correct
3 Correct 6 ms 4344 KB Output is correct
4 Correct 4 ms 4344 KB Output is correct
5 Runtime error 444 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 7 ms 4344 KB Output is correct
3 Correct 7 ms 4344 KB Output is correct
4 Correct 7 ms 4344 KB Output is correct
5 Runtime error 442 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 248 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 167 ms 4620 KB Output is correct
9 Correct 197 ms 5240 KB Output is correct
10 Correct 454 ms 103548 KB Output is correct
11 Runtime error 447 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Halted 0 ms 0 KB -