Submission #859494

#TimeUsernameProblemLanguageResultExecution timeMemory
859494normankr07Street Lamps (APIO19_street_lamps)C++17
40 / 100
1749 ms228320 KiB
#include <iostream> #include <map> #include <set> #include <vector> using namespace std; const int maxn = 3e5 + 5; int n, q; char c[maxn]; int nodes[maxn * 4]; int f[maxn]; set<int> st; void build(int id, int l, int r) { if (l == r) { if (c[l] == '1') nodes[id] = 0; else nodes[id] = q + 1; return; } int mid = (l + r) >> 1; build(id * 2, l, mid); build(id * 2 + 1, mid + 1, r); nodes[id] = max(nodes[id * 2], nodes[id * 2 + 1]); } void update(int id, int l, int r, int i, int val) { if (i < l || i > r) return; if (l == r) { nodes[id] = val; return; } int mid = (l + r) >> 1; if (i <= mid) update(id * 2, l, mid, i, val); else update(id * 2 + 1, mid + 1, r, i, val); nodes[id] = max(nodes[id * 2], nodes[id * 2 + 1]); } int get(int id, int l, int r, int u, int v) { if (v < l || r < u) return 0; if (u <= l && r <= v) return nodes[id]; int mid = (l + r) >> 1; return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v)); } struct node { int l, r, val; }; vector<node> bit[2][maxn]; int cnt[2][maxn]; void createl(int x, int id1, int id) { if (bit[x][id1][id].l == 0) { bit[x][id1][id].l = ++cnt[x][id1]; bit[x][id1].push_back({0, 0}); } } void creater(int x, int id1, int id) { if (bit[x][id1][id].r == 0) { bit[x][id1][id].r = ++cnt[x][id1]; bit[x][id1].push_back({0, 0}); } } void update1(int id1, int id, int l, int r, int i, int val, int x) { if (i < l || i > r) return; int mid = (l + r) >> 1; if (i <= mid) createl(x, id1, id); else creater(x, id1, id); bit[x][id1][id].val += val; if (l == r) return; if (i <= mid) update1(id1, bit[x][id1][id].l, l, mid, i, val, x); else update1(id1, bit[x][id1][id].r, mid + 1, r, i, val, x); } int get1(int id1, int id, int l, int r, int i, int x) { if (i < l || i > r) return 0; int mid = (l + r) >> 1; if (l == r) return bit[x][id1][id].val; if (i <= mid && bit[x][id1][id].l) return get1(id1, bit[x][id1][id].l, l, mid, i, x); else if (i > mid && bit[x][id1][id].r) { if (bit[x][id1][id].l) return bit[x][id1][bit[x][id1][id].l].val + get1(id1, bit[x][id1][id].r, mid + 1, r, i, x); return get1(id1, bit[x][id1][id].r, mid + 1, r, i, x); } else if (i > mid && bit[x][id1][id].l) return bit[x][id1][bit[x][id1][id].l].val; return 0; } void updatebt(int id, int i, int j, int val) { for (int i1 = i; i1 <= n + 1; i1 += (i1 & -i1)) update1(i1, 0, 1, n + 1, j, val, id); } int getbt(int id, int i, int j) { int ans = 0; for (int i1 = i; i1 >= 1; i1 -= (i1 & -i1)) ans += get1(i1, 0, 1, n + 1, j, id); return ans; } int main() { ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> q; for (int i = 1; i <= n; ++i) cin >> c[i]; for (int i = 1; i <= n + 1; ++i) { bit[0][i].push_back({0, 0, 0}); } st.insert(0); st.insert(n + 1); for (int i = 1; i <= n; ++i) { if (c[i] == '1') f[i] = 0; else f[i] = q + 1; if (c[i] == '0') st.insert(i); } build(1, 1, n); for (int id = 1; id <= q; ++id) { int d, x, y; string s; cin >> s; if (s == "toggle") d = 2; else d = 1; if (d == 1) { cin >> x >> y; if (x > y) swap(x, y); int ans = get(1, 1, n, x, y - 1); if (ans == q + 1) cout << getbt(0, x, y) << '\n'; else cout << getbt(0, x, y) + id - ans << '\n'; continue; } cin >> x; if (c[x] == '0') { c[x] = '1'; f[x] = id; update(1, 1, n, x, id); st.erase(x); } else { c[x] = '0'; auto it = st.lower_bound(x); int dy = *it; int dx = *(--it); int tmp = id - f[x]; updatebt(0, dx + 1, x + 1, tmp); updatebt(0, x + 1, x + 1, -tmp); updatebt(0, dx + 1, dy + 1, -tmp); updatebt(0, x + 1, dy + 1, tmp); f[x] = q + 1; update(1, 1, n, x, q + 1); st.insert(x); } } 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...