Submission #385671

#TimeUsernameProblemLanguageResultExecution timeMemory
385671valerikkStreet Lamps (APIO19_street_lamps)C++17
0 / 100
430 ms63036 KiB
#include <bits/stdc++.h>

using namespace std;

#define y1 y1111

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)a.size())
#define forn(i, n) for (int i = 0; i < (n); ++i)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1.1e9;
const ll INFL = 1.1e18;

const int N = 3e5 + 7;

struct Upd {
    int x, y, type;
};

int n, q;
string s, ss;
vector<Upd> upds[N];
pii qr[N];
set<int> st;
vector<int> xx;
vector<int> yy[N];
vector<ll> f[N];
vector<int> cnt[N];

void innerUpd(int pp, int y, int t, int tp) {
    y = lower_bound(all(yy[pp]), y + 1) - yy[pp].begin();
    for (int i = y; i <= sz(yy[pp]); i += i & -i) {
        f[pp][i] += tp * t;
        cnt[pp][i] += tp;
    }
}

void upd(int x, int y, int t, int tp) {
    x = lower_bound(all(xx), x + 1) - xx.begin();
    for (int i = x; i <= sz(xx); i += i & -i) innerUpd(i, y, t, tp);
}

ll innerGet(int pp, int y, int t) {
    y = lower_bound(all(yy[pp]), y + 1) - yy[pp].begin();
    ll res = 0;
    for (int i = y; i > 0; i -= i & -i) res += cnt[pp][i] * t - f[pp][i];
    return res;
}

ll get(int x, int y, int t) {
    x = lower_bound(all(xx), x + 1) - xx.begin();
    ll res = 0;
    for (int i = x; i > 0; i -= i & -i) res += innerGet(i, y, t);
    return res;
}

void adds(int t, int x1, int y1, int x2, int y2, int type) {
    ++x2, ++y2;
    upds[t].pb({x1, y1, type});
    upds[t].pb({x1, y2, -type});
    upds[t].pb({x2, y1, -type});
    upds[t].pb({x2, y2, type});
}

void toggle(int i, int t) {
    int prv = *prev(st.lower_bound(i)), nxt = *st.upper_bound(i);
    int type = s[i] == '0' ? 1 : -1;
    adds(t, prv, i + 1, i + 1, nxt, type);
    if (s[i] == '0') st.erase(i); else st.insert(i);
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> q >> ss;
    s = string(n, '0');
    for (int i = -1; i <= n; ++i) st.insert(i);
    forn(i, n) {
        if (ss[i] == '1') {
            toggle(i, 0);
        }
    }
    for (int t = 0; t <= q; ++t) qr[t] = {-1, -1};
    for (int t = 1; t <= q; ++t) {
        string type;
        cin >> type;
        if (type == "toggle") {
            int i;
            cin >> i;
            --i;
            toggle(i, t);
        } else {
            int a, b;
            cin >> a >> b;
            --a, --b;
            qr[t] = {a, b};
        }
    }
    
    for (int t = 0; t <= q; ++t) {
        for (auto u : upds[t]) xx.pb(u.x);
    }
    sort(all(xx));
    xx.erase(unique(all(xx)), xx.end());
    
    for (int t = 0; t <= q; ++t) {
        for (auto u : upds[t]) {
            for (int i = lower_bound(all(xx), u.x) - xx.begin() + 1; i <= sz(xx); i += i & -i) {
                yy[i].pb(u.y);
            }
        }
    }
    
    for (int i = 0; i <= sz(xx); ++i) {
        sort(all(yy[i]));
        yy[i].erase(unique(all(yy[i])), yy[i].end());
        f[i] = vector<ll>(sz(yy[i]) + 1, 0ll);
        cnt[i] = vector<int>(sz(yy[i]) + 1, 0);
    }
    
    for (int t = 0; t <= q; ++t) {
        for (auto u : upds[t]) upd(u.x, u.y, t, u.type);
        if (qr[t].F != -1) cout << get(qr[t].F, qr[t].S, t) << "\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...