Submission #1299687

#TimeUsernameProblemLanguageResultExecution timeMemory
1299687anarch_yStreet Lamps (APIO19_street_lamps)C++20
0 / 100
1409 ms98220 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define all(x) begin(x), end(x) #define sz(x) (int)x.size() #define pb push_back template<class T> struct SegmentTree{ T def = 0; vector<T> tree; int len; SegmentTree(int N){ int p = ceil(log2(N)); len = (1<<p); tree.resize(2*len, def); } T f(T a, T b){ return a+b; } void build(int k, int x, int y){ if(x == y) return; int d = (x+y)/2; build(2*k, x, d); build(2*k+1, d+1, y); tree[k] = f(tree[2*k], tree[2*k+1]); } void begin(){ build(1, 0, len-1); } void add(int id, T val, int k, int x, int y){ if(id < x or id > y) return; if(x == y and x == id){ tree[x+len] += val; return; } int d = (x+y)/2; if(id <= d) add(id, val, 2*k, x, d); else add(id, val, 2*k+1, d+1, y); tree[k] = f(tree[2*k], tree[2*k+1]); } void add(int id, T val){ add(id, val, 1, 0, len-1); } T query(int a, int b, int k, int x, int y){ if(b < x or a > y) return def; if(a <= x and y <= b) return tree[k]; int d = (x+y)/2; T s1 = query(a, b, 2*k, x, d); T s2 = query(a, b, 2*k+1, d+1, y); return f(s1, s2); } T query(int a, int b){ return query(a, b, 1, 0, len-1); } }; struct Pt{ int t, x, y, v, id; }; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; s = '0' + s + '0'; set<pair<int, int>> st; map<pair<int, int>, int> mp; int x = 0; for(int i=1; i<=n; i++){ if(s[i] == '1'){ if(x == 0) x = i; else if(s[i+1] == '0'){ st.insert({x, i}); x = 0; } } } for(auto [x, y]: st){ mp[{x, y}] = 0; } vector<Pt> v; int t = 1; auto upd = [&](int x, int y, int a){ v.pb({t, x, x, a, 0}); v.pb({t, x, y+1, -a, 0}); v.pb({t, y+1, x, -a, 0}); v.pb({t, y+1, y+1, a, 0}); t++; }; for(auto [x, y]: st){ upd(x, y, 1); } vector<int> ans(q+1); int cur = 1; for(int c=1; c<=q; c++){ string tp; cin >> tp; if(tp == "toggle"){ int i; cin >> i; if(s[i] == '1'){ auto it = st.upper_bound({i, 0}); it--; int x = it->first, y = it->second; int c1 = mp[{x, y}]; if(c-c1-1 > 0){ upd(x, y, c-c1-1); } st.erase({x, y}); mp.erase({x, y}); if(x <= i-1){ st.insert({x, i-1}); mp[{x, i-1}] = c; upd(x, i-1, 1); } if(i+1 <= y){ st.insert({i+1, y}); mp[{i+1, y}] = c; upd(i+1, y, 1); } s[i] = '0'; } else{ if(s[i-1] == '1' and s[i+1] == '1'){ auto it = st.lower_bound({i+1, 0}); int x2 = it->first, y2 = it->second; it--; int x1 = it->first, y1 = it->second; int c1 = mp[{x1, y1}]; if(c-c1-1 > 0){ upd(x1, y1, c-c1-1); } c1 = mp[{x2, y2}]; if(c-c1-1 > 0){ upd(x2, y2, c-c1-1); } st.erase({x1, y1}); st.erase({x2, y2}); mp.erase({x1, y1}); mp.erase({x2, y2}); st.insert({x1, y2}); mp[{x1, y2}] = c; upd(x1, y2, 1); } else if(s[i+1] == '1'){ auto it = st.lower_bound({i+1, 0}); int x = it->first, y = it->second; int c1 = mp[{x, y}]; if(c-c1-1 > 0){ upd(x, y, c-c1-1); } st.erase({x, y}); mp.erase({x, y}); st.insert({i, y}); mp[{i, y}] = c; upd(i, y, 1); } else if(s[i-1] == '1'){ auto it = st.lower_bound({i, 0}); it--; int x = it->first, y = it->second; int c1 = mp[{x, y}]; if(c-c1-1 > 0){ upd(x, y, c-c1-1); } st.erase({x, y}); mp.erase({x, y}); st.insert({x, i}); mp[{x, i}] = c; upd(x, i, 1); } else{ st.insert({i, i}); mp[{i, i}] = c; upd(i, i, 1); } s[i] = '1'; } } else{ int a, b; cin >> a >> b; b--; if(s[a] == '0'){ v.pb({t, a, b, 0, cur}); t++; cur++; } else{ auto it = st.upper_bound({a, 0}); it--; int x = it->first, y = it->second; if(b <= y){ int c1 = mp[{x, y}]; if(c-c1-1 > 0){ ans[cur] = c-c1-1; } v.pb({t, a, b, 0, cur}); t++; cur++; } else{ v.pb({t, a, b, 0, cur}); t++; cur++; } } } } SegmentTree<int> seg(n+2); seg.begin(); auto solve = [&](auto solve, int l, int r){ if(l == r) return; int m = (l+r)/2; solve(solve, l, m); solve(solve, m+1, r); vector<Pt> w; vector<pair<int, int>> h; int i = l, j = m+1; while(i <= m and j <= r){ if(v[i].x <= v[j].x){ seg.add(v[i].y, v[i].v); h.pb({v[i].y, -v[i].v}); w.pb(v[i]); i++; } else{ if(v[j].id != 0){ ans[v[j].id] += seg.query(1, v[j].y); } w.pb(v[j]); j++; } } while(i <= m){ w.pb(v[i]); i++; } while(j <= r){ if(v[j].id != 0){ ans[v[j].id] += seg.query(1, v[j].y); } w.pb(v[j]); j++; } for(int k=l; k<=r; k++){ v[k] = w[k-l]; } for(auto [y, s]: h){ seg.add(y, s); } }; solve(solve, 0, sz(v)-1); for(int i=1; i<cur; i++) cout << ans[i] << "\n"; }
#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...