Submission #966247

#TimeUsernameProblemLanguageResultExecution timeMemory
966247steveonalexStreet Lamps (APIO19_street_lamps)C++17
100 / 100
2217 ms111164 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define ALL(v) (v).begin(), (v).end() #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng(1); ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;} ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return mask & (-mask);} ll pop_cnt(ll mask){return __builtin_popcountll(mask);} ll ctz(ll mask){return __builtin_ctzll(mask);} ll clz(ll mask){return __builtin_clzll(mask);} ll logOf(ll mask){return 63 - clz(mask);} template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b){a = b; return true;} return false; } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b){a = b; return true;} return false; } template <class T> void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){ for(auto i: a) out << i << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } struct FenwickTree{ int n; vector<vector<int>> a, val; FenwickTree(int _n){ n = _n; a.resize(n+1); val.resize(n+1); } void fake_update(int i, int j){ while(i <= n){ val[i].push_back(j); i += LASTBIT(i); } } void build_tree(){ for(int i = 1; i<=n; ++i){ val[i].push_back(0); remove_dup(val[i]); a[i].resize(val[i].size()); } } void update(int i, int j, int v){ while(i <= n){ int idx = lower_bound(ALL(val[i]), j) - val[i].begin(); while(idx < val[i].size()){ a[i][idx] += v; idx += LASTBIT(idx); } i += LASTBIT(i); } } int get(int i, int j){ int ans = 0; while(i > 0){ int idx = upper_bound(ALL(val[i]), j) - val[i].begin() - 1; while(idx > 0){ ans += a[i][idx]; idx -= LASTBIT(idx); } i -= LASTBIT(i); } return ans; } }; const int N = 3e5 + 69; int a[N], b[N]; void fake_add(int i, set<int> &S, vector<pair<int, int>> &range){ int l = -1, r = -1; auto it = S.upper_bound(i); if (it != S.end()) r = *it; if (it != S.begin()){ it--; l = *it; } S.insert(i); if (l > -1) range.push_back({l, i}); if (r > -1) range.push_back({i, r}); } void fake_del(int i, set<int> &S, vector<pair<int, int>> &range){ S.erase(i); int l = -1, r = -1; auto it = S.upper_bound(i); if (it != S.end()) r = *it; if (it != S.begin()){ it--; l = *it; } if (l > -1 && r > -1) range.push_back({l, r}); } void add_sigma(int i, int j, set<int> &S, FenwickTree &bit){ int l = -1, r = -1; auto it = S.upper_bound(j); if (it != S.end()) r = *it; if (it != S.begin()){ it--; l = *it; } S.insert(j); if (l > -1 && r > -1) bit.update(r, l, i); if (l > -1) bit.update(j, l, -i); if (r > -1) bit.update(r, j, -i); } void del_sigma(int i, int j, set<int> &S, FenwickTree &bit){ S.erase(j); int l = -1, r = -1; auto it = S.upper_bound(j); if (it != S.end()) r = *it; if (it != S.begin()){ it--; l = *it; } cout.flush(); if (l > -1) bit.update(j, l, i); if (r > -1) bit.update(r, j, i); if (l > -1 && r > -1) bit.update(r, l, -i); } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.inp", "r", stdin); int n, q; cin >> n >> q; string s; cin >> s; for(int i= 1; i<=n; ++i) a[i+1] = (s[i-1] == '1'); n++; vector<vector<int>> vt; for(int i = 1; i<=q; ++i){ string type; cin >> type; if (type == "query"){ int l, r; cin >> l >> r; l++; vt.push_back({2, l, r}); } else{ int j; cin >> j; j++; vt.push_back({1, j}); } } set<int> S; vector<pair<int, int>> range; for(int i = 1; i<=n; ++i) if (a[i] == 0) fake_add(i, S, range); for(int i = 1; i<=n; ++i) b[i] = a[i]; for(int i = 0; i<q; ++i){ if (vt[i][0] == 1){ int j = vt[i][1]; if (b[j] == 1) fake_add(j, S, range); else fake_del(j, S, range); b[j] ^= 1; } } FenwickTree bit(n); for(pair<int, int> i: range) bit.fake_update(i.second, i.first); bit.build_tree(); S.clear(); for(int i = 1; i<=n; ++i) if (a[i] == 0) S.insert(i); for(int i = 1; i<=n; ++i) b[i] = a[i]; for(int i= 0; i<q; ++i){ if (vt[i][0] == 1){ int j = vt[i][1]; if (b[j] == 1){ add_sigma(i+1, j, S, bit); } else{ del_sigma(i+1, j, S, bit); } b[j] ^= 1; } else{ int l = vt[i][1], r = vt[i][2]; int ans = bit.get(r, l-1) - bit.get(l-1, l-1); auto it = S.lower_bound(l); if (it != S.end() && (*it) <= r) ans += i+1; ans = (i + 1) - ans; cout << ans << "\n"; } } return 0; }

Compilation message (stderr)

street_lamps.cpp: In member function 'void FenwickTree::update(int, int, int)':
street_lamps.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |             while(idx < val[i].size()){
      |                   ~~~~^~~~~~~~~~~~~~~
#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...