Submission #308268

#TimeUsernameProblemLanguageResultExecution timeMemory
308268WLZStreet Lamps (APIO19_street_lamps)C++14
100 / 100
1681 ms68920 KiB
#include <bits/stdc++.h> using namespace std; struct comp { inline bool operator()(const pair<int, int> &a, const pair<int, int> &b) { return a.second < b.second; } }; struct event { int x, y_1, y_2, val, idx; }; template<typename T> class fenwick_tree { private: int n; vector<T> fenw; stack<int> st; public: fenwick_tree(int _n) : n(_n) { fenw.resize(n); } size_t size() { return n; } void update(int x, T val) { while (x < n) { fenw[x] += val; st.push(x); x |= (x + 1); } } T query(int x) { T ans = 0; while (x >= 0) { ans += fenw[x]; x = (x & (x + 1)) - 1; } return ans; } void update(int x, int y, T val) { update(x, val); if (y < n - 1) { update(y + 1, -val); } } void reset() { while (!st.empty()) { fenw[st.top()] = 0; st.pop(); } } }; vector<int> ans; vector<event> events, sorted_events; fenwick_tree<int> fenw(0); void add(int x_1, int y_1, int x_2, int y_2, int val, int idx) { events.push_back({x_1, y_1, y_2, val, idx}); events.push_back({x_2 + 1, y_1, y_2, -val, idx}); } void solve(int l, int r) { if (l + 1 == r) { return; } int mid = (l + r) / 2; solve(l, mid); solve(mid, r); int i = l, j = mid, cur = l; while (i < mid && j < r) { if (events[i].x <= events[j].x) { if (events[i].y_2 != -1) { fenw.update(events[i].y_1, events[i].y_2, events[i].val); } sorted_events[cur++] = events[i++]; } else { if (events[j].y_2 == -1) { ans[events[j].idx] += fenw.query(events[j].y_1); } sorted_events[cur++] = events[j++]; } } while (j < r) { if (events[j].y_2 == -1) { ans[events[j].idx] += fenw.query(events[j].y_1); } sorted_events[cur++] = events[j++]; } while (i < mid) { sorted_events[cur++] = events[i++]; } for (int t = l; t < r; t++) { events[t] = sorted_events[t]; } fenw.reset(); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; set< pair<int, int>, comp> st; for (int i = 1; i <= n + 1; i++) { st.insert({i, i}); } for (int i = 1; i <= n; i++) { if (s[i - 1] == '1') { auto it = st.lower_bound({0, i}); pair<int, int> tmp = {it->first, i + 1}; st.erase(it); st.erase({i + 1, i + 1}); st.insert(tmp); } } ans.assign(q + 1, -1); string type; for (int t = 1; t <= q; t++) { cin >> type; if (type == "toggle") { int i; cin >> i; if (s[i - 1] == '0') { s[i - 1] = '1'; auto it = st.lower_bound({0, i}); int x_1 = it->first, y_1 = it->second, x_2 = next(it)->first, y_2 = next(it)->second; pair<int, int> tmp = {x_1, y_2}; add(x_1, x_2, y_1, y_2, -t, t); st.erase({x_1, y_1}); st.erase({x_2, y_2}); st.insert(tmp); } else { s[i - 1] = '0'; auto it = st.lower_bound({0, i + 1}); int x = it->first, y = it->second; add(x, i + 1, i, y, t, t); st.erase({x, y}); st.insert({x, i}); st.insert({i + 1, y}); } } else { int a, b; cin >> a >> b; ans[t] = 0; if (st.lower_bound({0, a}) == st.lower_bound({0, b})) { ans[t] = t; } events.push_back({a, b, -1, -1, t}); } } fenw = fenwick_tree<int>(n + 2); sorted_events.resize((int) events.size()); solve(0, (int) events.size()); for (int i = 1; i <= q; i++) { if (ans[i] != -1) { cout << ans[i] << '\n'; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...