Submission #837986

#TimeUsernameProblemLanguageResultExecution timeMemory
837986CookieStreet Lamps (APIO19_street_lamps)C++14
100 / 100
2763 ms116468 KiB
#include<bits/stdc++.h>
#include<fstream>
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx2")
using namespace std;
ifstream fin("newbarn.in");
ofstream fout("newbarn.out");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int base = 74;
const int mxn = 3e5 + 5, mod = 1e9 + 9, mxq = 3e5 + 5;
const ll inf = 1e17;
int n, q;
struct BIT2D{
    vt<int>bit[mxn + 1];
    vt<int>node[mxn + 1];
    void fakeupd(int x, int y){
        for(;x <= n + 1; x += x & (-x)){

            node[x].pb(y);

        }
    }
    void fakeget(int x, int y){
        for(;x; x -= x & (-x)){

            node[x].pb(y);

        }
    }
    int get(int x, int y){
        int ans = 0;
        for(;x; x -= x & (-x)){
            for(int idy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; idy; idy -= idy & (-idy)){
                ans += bit[x][idy];
            }
        }
        return(ans);
    }
    void upd(int x, int y, int v){
        for(;x <= n + 1; x += x & (-x)){
            for(int idy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; idy <= sz(node[x]); idy += idy & (-idy)){
                bit[x][idy] += v;
            }
        }
    }
    void init(){
        for(int i = 1; i <= n + 1; i++){
            sort(node[i].begin(), node[i].end());
            node[i].resize(unique(node[i].begin(), node[i].end()) - node[i].begin());
            bit[i].resize(sz(node[i]) + 3);
        }

    }
    void recupd(int i, int j, int x, int y, bool fake, int v = 0){
        if(fake){
            fakeupd(i, j); fakeupd(i, y + 1); fakeupd(x + 1, j); fakeupd(x + 1, y + 1);
        }else{
            upd(i, j, v); upd(i, y + 1, -v); upd(x + 1, j, -v); upd(x + 1, y + 1, v);
        }
    }
};
set<int>pos;
BIT2D cool;
pii queries[mxq + 1];
bool ok[mxn + 1];
int findlast(int x){
    auto it = pos.lower_bound(x);
    if(it == pos.begin())return(0);
    return(*prev(it));
}
int findnext(int x){
    auto it = pos.upper_bound(x);
    if(it == pos.end())return(n + 1);
    return(*it);
}
char c[mxn + 1];
signed main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        cin >> c[i];
        ok[i] = (c[i] == '0');
        if(ok[i]){
            pos.insert(i);
        }
    }
    for(int i = 0; i < q; i++){
        string s; cin >> s;
        if(s[0] == 't'){
            int id; cin >> id;
            if(!ok[id]){
                int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
                cool.recupd(topx, topy, botx, boty, 1);
                pos.insert(id);
            }else{

                pos.erase(id);
                int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
                cool.recupd(topx, topy, botx, boty, 1);
            }
            ok[id] = !ok[id];
            queries[i] = make_pair(id, -1);
        }else{
            int x, y; cin >> x >> y;
            cool.fakeget(x, y);
            queries[i] = make_pair(x, y);
        }

    }
    cool.init();
    pos.clear();
    for(int i = 1; i <= n; i++){
        ok[i] = (c[i] == '0');
        if(ok[i])pos.insert(i);
    }
    for(int i = 0; i < q; i++){
        if(queries[i].second == -1){
            int id = queries[i].first;
            if(!ok[id]){
                int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
                cool.recupd(topx, topy, botx, boty, 0, i + 1);
                pos.insert(id);
            }else{

                pos.erase(id);
                int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
                cool.recupd(topx, topy, botx, boty, 0, -(i + 1));
            }
            ok[id] = !ok[id];
        }else{
            int x = queries[i].first, y = queries[i].second;
            int ans = cool.get(x, y);
            if(findnext(x) >= y && findlast(y) < x){
                ans += (i + 1); 
            }
            cout << ans << "\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...