Submission #172576

# Submission time Handle Problem Language Result Execution time Memory
172576 2020-01-02T04:17:25 Z rama_pang Street Lamps (APIO19_street_lamps) C++14
20 / 100
5000 ms 85976 KB
#include <bits/stdc++.h>
using namespace std;
 
template<typename Data>
struct SparseFenwickTree { // Range Update Point Query
    int N;
    unordered_map<int, Data> Tree;
 
    SparseFenwickTree() { N = 0; Tree.clear(); }
 
    /* Standard Tree Updates */
    int resize(int n) {
        N = n;
    }
 
    void update_tree(int p, Data x) {
        for (int i = p; i <= N; i += i & -i) {
            Tree[i] += x;
            if (Tree[i] == 0) {
                Tree.erase(i);
            }
        }
    }
 
    Data query_tree(int p) {
        Data res = 0;
        for (int i = p; i > 0; i -= i & -i) {
            res += Tree[i];
            if (Tree[i] == 0) {
                Tree.erase(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<SparseFenwickTree<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 current time if range doesn't have ending time yet
 
            cout << ans << "\n";
        }
    }
 
    return 0;
}

Compilation message

street_lamps.cpp: In member function 'int SparseFenwickTree<Data>::resize(int)':
street_lamps.cpp:14: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:63:5: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:105: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 252 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 779 ms 1528 KB Output is correct
2 Correct 1387 ms 1764 KB Output is correct
3 Correct 3604 ms 4944 KB Output is correct
4 Execution timed out 5094 ms 76808 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 760 KB Output is correct
2 Correct 10 ms 632 KB Output is correct
3 Correct 9 ms 632 KB Output is correct
4 Correct 5 ms 508 KB Output is correct
5 Execution timed out 5102 ms 85976 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 8 ms 636 KB Output is correct
3 Correct 10 ms 632 KB Output is correct
4 Correct 12 ms 760 KB Output is correct
5 Correct 3441 ms 37468 KB Output is correct
6 Execution timed out 5100 ms 74276 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 252 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 779 ms 1528 KB Output is correct
9 Correct 1387 ms 1764 KB Output is correct
10 Correct 3604 ms 4944 KB Output is correct
11 Execution timed out 5094 ms 76808 KB Time limit exceeded
12 Halted 0 ms 0 KB -