Submission #124678

#TimeUsernameProblemLanguageResultExecution timeMemory
124678EntityITStreet Lamps (APIO19_street_lamps)C++14
100 / 100
3532 ms135828 KiB
/* check long long - int check N */ #include<bits/stdc++.h> using namespace std; #define pb push_back const int N = (int)3e5 + 5, Q = N; int n, q; string initSate; struct Query { int t, a, b; Query (int _t = 0, int _a = 0, int _b = 0) : t(_t), a(_a), b(_b) {} } query[Q]; struct Bit { static const int LOG = __lg(N) + 1; int a[1 << LOG]; void init () { memset(a, 0, sizeof a); } void update (int pos, int val) { for (; pos < (1 << LOG); pos += pos & -pos) a[pos] += val; } int get (int pos) { int ret = 0; for (; pos > 0; pos -= pos & -pos) ret += a[pos]; return ret; } int findPos (int val) { if (!val) return 0; int ret = 0; for (int i = LOG - 1; i >= 0; --i) if (a[ret | (1 << i)] < val) ret |= (1 << i), val -= a[ret]; return min(ret, n) + 1; } } bit; struct Bit2D { vector<int> cmprssNmbr[N]; vector<long long> a[N]; void init () { for (int i = 0; i < N; ++i) cmprssNmbr[i].assign(1, -1); } void add (int x, int y) { for (; x < N; x += x & -x) { cmprssNmbr[x].pb(y); } } void interPhase () { for (int i = 1; i < N; ++i) { sort(cmprssNmbr[i].begin(), cmprssNmbr[i].end() ); cmprssNmbr[i].erase(unique(cmprssNmbr[i].begin(), cmprssNmbr[i].end() ), cmprssNmbr[i].end() ); a[i].assign(cmprssNmbr[i].size(), 0); } } void update (int x, int y, int val) { for (; x < N; x += x & -x) { int _y = (int)(lower_bound(cmprssNmbr[x].begin(), cmprssNmbr[x].end(), y) - cmprssNmbr[x].begin() ); for (; _y < (int)a[x].size(); _y += _y & -_y) a[x][_y] += val; } } long long get (int x, int y) { long long ret = 0; for (; x > 0; x -= x & -x) { int _y = (int)(upper_bound(cmprssNmbr[x].begin(), cmprssNmbr[x].end(), y) - cmprssNmbr[x].begin() - 1); for (; _y > 0; _y -= _y & -_y) ret += a[x][_y]; } return ret; } void update (int x1, int y1, int x2, int y2, int val) { update(x1, y1, val); update(x2 + 1, y2 + 1, val); update(x1, y2 + 1, -val); update(x2 + 1, y1, -val); } } bit2D; int32_t main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("test.INP", "r", stdin); bit2D.init(); cin >> n >> q >> initSate; initSate = '!' + initSate; for (int i = 1; i <= q; ++i) { string str; cin >> str; if (str == "toggle") { int a; cin >> a; query[i] = Query(1, a, 0); } else { int a, b; cin >> a >> b; --b; query[i] = Query(2, a, b); } } string curState = initSate; for (int i = 1; i <= n; ++i) if (curState[i] == '0') bit.update(i, 1); for (int i = 0; i <= n; ++i) if (curState[i] != '1') { int j = bit.findPos(bit.get(i) + 1); if (i + 1 <= j - 1) { bit2D.add(i + 1, i + 1); bit2D.add(j, j); bit2D.add(i + 1, j); bit2D.add(j, i + 1); } } for (int i = 1; i <= q; ++i) if (query[i].t == 1) { if (curState[ query[i].a ] == '1') { curState[ query[i].a ] = '0'; int l = bit.findPos(bit.get(query[i].a) ) + 1, r = bit.findPos(bit.get(query[i].a) + 1) - 1; bit2D.add(l, query[i].a); bit2D.add(query[i].a + 1, r + 1); bit2D.add(l, r + 1); bit2D.add(query[i].a + 1, query[i].a); bit.update(query[i].a, 1); } else { curState[ query[i].a ] = '1'; int l = bit.findPos(bit.get(query[i].a) - 1 ) + 1, r = bit.findPos(bit.get(query[i].a) + 1) - 1; bit2D.add(l, query[i].a); bit2D.add(query[i].a + 1, r + 1); bit2D.add(l, r + 1); bit2D.add(query[i].a + 1, query[i].a); bit.update(query[i].a, -1); } } bit.init(); bit2D.interPhase(); curState = initSate; for (int i = 1; i <= n; ++i) if (curState[i] == '0') bit.update(i, 1); for (int i = 1; i <= q; ++i) { if (query[i].t == 1) { if (curState[ query[i].a ] == '1') { curState[ query[i].a ] = '0'; int l = bit.findPos(bit.get(query[i].a) ) + 1, r = bit.findPos(bit.get(query[i].a) + 1) - 1; bit2D.update(l, query[i].a, query[i].a, r, i); bit.update(query[i].a, 1); } else { curState[ query[i].a ] = '1'; int l = bit.findPos(bit.get(query[i].a) - 1 ) + 1, r = bit.findPos(bit.get(query[i].a) + 1) - 1; bit2D.update(l, query[i].a, query[i].a, r, -i); bit.update(query[i].a, -1); } } else { cout << bit2D.get(query[i].a, query[i].b) + (bit.get(query[i].b) == bit.get(query[i].a - 1) ? i : 0) << '\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...