Submission #673606

#TimeUsernameProblemLanguageResultExecution timeMemory
673606ghostwriterStreet Lamps (APIO19_street_lamps)C++17
100 / 100
2708 ms149636 KiB
// #pragma GCC optimize ("Ofast") // #pragma GCC target ("avx2") #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #else #define debug(...) #endif #define ft front #define bk back #define st first #define nd second #define ins insert #define ers erase #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define bg begin #define ed end #define all(x) (x).bg(), (x).ed() #define sz(x) (int)(x).size() // #define int long long typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; #define FOR(i, l, r) for (int i = (l); i <= (r); ++i) #define FOS(i, r, l) for (int i = (r); i >= (l); --i) #define FRN(i, n) for (int i = 0; i < (n); ++i) #define FSN(i, n) for (int i = (n) - 1; i >= 0; --i) #define EACH(i, x) for (auto &i : (x)) #define WHILE while template<typename T> T gcd(T a, T b) { T d2 = (a | b) & -(a | b); a /= d2; b /= d2; WHILE(b) { a = a % b; swap(a, b); } return a * d2; } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } void _assert(bool statement) { if (statement) return; cerr << "\n>> Assertion failed!\n"; exit(0); } void _assert(bool statement, const str &message) { if (statement) return; cerr << "\n>> Assertion failed: " << message << '\n'; exit(0); } void _error(const str &message) { cerr << "\n>> Error: " << message << '\n'; exit(0); } #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Tran The Bao - ghostwriter Training for VOI23 gold medal ---------------------------------------------------------------- GOAT ---------------------------------------------------------------- */ struct Event { str type; int i, a, b; Event() {} }; struct Toggle { int lm, rm, v; Toggle() {} Toggle(int lm, int rm, int v) : lm(lm), rm(rm), v(v) {} }; const int oo = 1e9 + 5; const int N = 3e5 + 5; int n, q, additionalValue[N]; char s[N]; Event event[N]; Toggle toggleUpdate[N]; vi f[N], f1[N]; void input(int test_id) { cin >> n >> q; FOR(i, 1, n) cin >> s[i]; FOR(i, 1, q) { cin >> event[i].type; if (event[i].type == "toggle") cin >> event[i].i; else { cin >> event[i].a >> event[i].b; --event[i].b; } } } void preUpdate(int x, int y) { for (int i = x; i <= n; i += (i & -i)) f1[i].pb(y); } void update(int x, int y, int v) { for (int i = x; i <= n; i += (i & -i)) { int pos = lb(all(f1[i]), y) - f1[i].bg(); for (int j = pos; j < sz(f1[i]); j += ((j + 1) & -(j + 1))) f[i][j] += v; } } void preUpdate(int xl, int xr, int yl, int yr) { preUpdate(xl, yl); if (yr < n) preUpdate(xl, yr + 1); if (xr < n) preUpdate(xr + 1, yl); if (xr < n && yr < n) preUpdate(xr + 1, yr + 1); } void update(int xl, int xr, int yl, int yr, int v) { update(xl, yl, v); if (yr < n) update(xl, yr + 1, -v); if (xr < n) update(xr + 1, yl, -v); if (xr < n && yr < n) update(xr + 1, yr + 1, v); } void preGetSum(int x, int y) { for (int i = x; i > 0; i -= (i & -i)) f1[i].pb(y); } int getSum(int x, int y) { int rs = 0; for (int i = x; i > 0; i -= (i & -i)) { int pos = lb(all(f1[i]), y) - f1[i].bg(); for (int j = pos; j >= 0; j -= ((j + 1) & -(j + 1))) rs += f[i][j]; } return rs; } void initFenwickTree() { FOR(i, 1, n) { if (f1[i].empty()) continue; sort(all(f1[i])); f[i].resize(sz(f1[i]), 0); } } void preToggle(multiset<pi> &segment, int i, int j) { s[i] ^= 1; if (s[i] == '1') { int lm = i, rm = i; if (s[i - 1] == '1') lm = (*--segment.ub({i, oo})).st; if (s[i + 1] == '1') rm = (*segment.ub({i, oo})).nd; if (lm == -oo) lm = i; if (rm == oo) rm = i; preUpdate(lm, i, i, rm); toggleUpdate[j] = Toggle(lm, rm, -j); if (lm < i) segment.ers(--segment.ub({i, oo})); if (rm > i) segment.ers(segment.ub({i, oo})); segment.ins({lm, rm}); } else { pi tmp = *--segment.ub({i, oo}); int lm = tmp.st, rm = tmp.nd; preUpdate(lm, i, i, rm); toggleUpdate[j] = Toggle(lm, rm, j); segment.ers(--segment.ub({i, oo})); if (lm < i) segment.ins({lm, i - 1}); if (rm > i) segment.ins({i + 1, rm}); } } void preGetAns(multiset<pi> &segment, int a, int b, int j) { preGetSum(a, b); pi tmp = *--segment.ub({a, oo}); if (tmp.st <= a && b <= tmp.nd) additionalValue[j] += j; } int getAns(int a, int b, int j) { return getSum(a, b) + additionalValue[j]; } void prepareEvent() { multiset<pi> segment; segment.ins({-oo, -oo}); segment.ins({oo, oo}); int pre = 0; FOR(i, 1, n + 1) if (i == n + 1 || s[i] == '0') { if (pre + 1 < i) segment.ins({pre + 1, i - 1}); pre = i; } FOR(j, 1, q) if (event[j].type == "toggle") { int i = event[j].i; preToggle(segment, i, j); } else { int a = event[j].a, b = event[j].b; preGetAns(segment, a, b, j); } initFenwickTree(); } void solve(int test_id) { prepareEvent(); FOR(j, 1, q) { if (event[j].type == "toggle") { int i = event[j].i, lm = toggleUpdate[j].lm, rm = toggleUpdate[j].rm, v = toggleUpdate[j].v; update(lm, i, i, rm, v); } else { int a = event[j].a, b = event[j].b; cout << getAns(a, b, j) << '\n'; } } } void reinit(int test_id) { } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); int test_num = 1; // cin >> test_num; // comment if the problem does not requires multitest FOR(i, 1, test_num) { input(i); // input in noninteractive problems for case #i solve(i); // main function to solve case #i reinit(i); // reinit global data to default used in case #i } #ifdef LOCAL cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms.\n"; #endif return 0; } /* ---------------------------------------------------------------- From Benq: stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ---------------------------------------------------------------- */
#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...