Submission #1199534

#TimeUsernameProblemLanguageResultExecution timeMemory
1199534t9unkubjStreet Lamps (APIO19_street_lamps)C++20
0 / 100
417 ms73608 KiB
#ifdef t9unkubj
#include"my_template.h"
//#include"my_template_no_debug.h"
#else
#define dbg(...) 199958
#pragma GCC optimize("O3")
#include"bits/stdc++.h"
using namespace std;
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using i128=__int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vc<vvc<T>>;
template<class T>using vvvvc=vc<vvvc<T>>;
template<class T>using smpq=priority_queue<T,vc<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
using vi=vc<int>;
using vvi=vvc<int>;
using vl=vc<ll>;
using vvl=vvc<ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=(ll(n)-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template<class T>
T sum(const vector<T>&v){
    return accumulate(all(v),T(0));
}
template<class T>
T min(const vector<T>&v){
    return *min_element(all(v));
}
template<class T>
T max(const vector<T>&v){
    return *max_element(all(v));
}
void YesNo(bool y){
    cout<<(y?"Yes":"No")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
template<class T>
void unique(vc<T>&a){
    a.erase(unique(all(a)),a.end());
}
vvi readgraph(int n,int m,int off = -1){
    vvi g(n);
    rep(i, m){
        int u,v;
        cin>>u>>v;
        u+=off,v+=off;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    return g;
}
vvi readtree(int n,int off=-1){
    return readgraph(n,n-1,off);
}
template<class T>
vc<T> presum(vc<T> &a){
    vc<T> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
ostream&operator<<(ostream&os,i128 num) {
    if(num==0){
        os<<"0";
        return os;
    }
    if(num<0){
        os<<"-";
        num=-num;
    }
    string res;
    while(num){
        res+='0'+static_cast<int>(num%10);
        num/=10;
    }
    reverse(all(res));
    os<<res;
    return os;
}

istream&operator>>(istream& is,i128&num) {
    string s;
    is>>s;
    bool neg=0;
    num=0;
    if(s[0]=='-'){
        neg=1;
        s=s.substr(1);
    }
    for(auto&c:s){
        if (c>='0'&&c<='9'){
            num=num*10+(c-'0');
        }else{
            is.setstate(ios::failbit);
            return is;
        }
    }
    if(neg){
        num=-num;
    }
    return is;
}

void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a; }
void scan(i128&a){ cin>> a; }
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__);
#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__);
#define LD(...) ld __VA_ARGS__; read(__VA_ARGS__);
#define I128(...) i128 __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);

void print(int a) { cout << a; }
void print(ll a) { cout << a; }
void print(string a) { cout << a; }
void print(char a) { cout << a; }
void print(uint a) { cout << a; }
void print(bool a) { cout << a; }
void print(ull a) { cout << a; }
void print(double a) { cout << a; }
void print(ld a){ cout<< a; }
void print(i128 a){ cout<< a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
struct ioset{
    ioset(){
        cin.tie(0)->sync_with_stdio(0);
        #ifdef t9unkubj
        cout<<fixed<<setprecision(6);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
}ioset_______;
struct MY_TIMER{
    double start_time;
    MY_TIMER(){
        start_time=clock();
    }
    double out(){
        return (clock()-start_time)/double(CLOCKS_PER_SEC);
    }
}TIMER;
#endif
//https://trap.jp/post/1489
//T:=座標型
//S:=点のvalue
//F:=作用素
//Apply:=作用
//Merge:=作用素をマージ
//init:=(pair<T,T>,idx)で初期化
//op:=集約
//e:=単位元

//id:=単位元
//Find を使うなら idx が S 内で定義されている必要がある
//template<class T,class S,class F,auto op,auto e,auto Apply,auto Merge,auto id,auto init>
template<class T,class S,class F,auto op,auto e,auto Apply,auto Merge,auto id,auto init>
struct kdtree{
    //葉ではvalueはそのものの値,それ以外では集約した値 
    //いい感じの演算じゃなければ葉以外の値は壊れてる
    struct Node{
        T xmin,xmax,ymin,ymax;
        S value;
        F lazy;
        int idx;
        int size;
        Node*l;
        Node*r;
        Node(vector<array<T,3>>&v,int L,int R,bool div){ 
            xmin=ymin=numeric_limits<T>::max();
            xmax=ymax=numeric_limits<T>::min();
            if constexpr(id){
                lazy=id();
            }else{
                lazy=F();
            }
            for(int i=L;i<R;i++){
                chmin(xmin,v[i][0]);
                chmax(xmax,v[i][0]);
                chmin(ymin,v[i][1]);
                chmax(ymax,v[i][1]);
            }
            
            size=R-L;
            if(size==0)return;
            if(size==1){
                if constexpr(init){
                    value=init(v[L][2]);
                }else{
                    value=S();
                }
                idx=v[L][2];
                l=r=nullptr;
                return;
            }else{
                idx=-1;
                int mid=size/2;
                nth_element(v.begin()+L,v.begin()+L+mid,v.begin()+R,[&](auto a,auto b){return a[div]<b[div];});
                l=new Node(v,L,L+mid,div^1);
                r=new Node(v,L+mid,R,div^1);
                this->update();
            }
        }
        void update(){
            if(l){
                if constexpr(op){
                    this->value=op(l->value,r->value);
                }
            }
        }
        void push(){
            if(l){
                if constexpr(Merge){
                    l->lazy=Merge(l->lazy,this->lazy);
                    r->lazy=Merge(r->lazy,this->lazy);
                }
                if constexpr(Apply){
                    l->value=Apply(l->value,this->lazy);
                    r->value=Apply(r->value,this->lazy);
                }
            }
            if constexpr(id){
                this->lazy=id();
            }else{
                this->lazy=F();
            }
        }
        S prod(T x1,T x2,T y1,T y2){
            if(x2<xmin||xmax<x1||y2<ymin||ymax<y1){
                if constexpr(e){
                    return e();
                }else{
                    return S();
                }
            }
            
            push();
            if(x1<=xmin&&xmax<=x2&&y1<=ymin&&ymax<=y2){
                return value;
            }
            if(l)return op(l->prod(x1,x2,y1,y2),r->prod(x1,x2,y1,y2));
            if constexpr(e){
                return e();
            }else{
                return S();
            }
        }
        void apply(T x1,T x2,T y1,T y2,F x){
            if(x2<xmin||xmax<x1||y2<ymin||ymax<y1){
                return;
            }
            push();
            if(x1<=xmin&&xmax<=x2&&y1<=ymin&&ymax<=y2){ 
                value=Apply(value,x);
                lazy=Merge(lazy,x);
                return;
            }
            l->apply(x1,x2,y1,y2,x);
            r->apply(x1,x2,y1,y2,x);
            update();
        }
        //点がuniqueであると仮定して (X,Y) の value を返す
        optional<S> get(T X,T Y){
            if(X<xmin||xmax<X||Y<ymin||ymax<Y)return nullopt;
            if(X==xmin&&xmax==X&&Y==ymin&&Y==ymax){
                return value;
            }
            push();
            if(l){
                auto res=l->get(X,Y);
                if(res.has_value())return res;
            }
            if(r){
                auto res=r->get(X,Y);
                if(res.has_value())return res;
            }
            return nullopt;
        }
        static bool inside(T dx,T dy,T K){
            return ll(dx)*dx+ll(dy)*dy<=ll(K)*K;
        }
        template<class Show> 
        bool find(T x,T y,T K,Show f,bool once){
            if(!inside(clamp(x,xmin,xmax)-x,clamp(y,ymin,ymax)-y,K))return 0;
            if(idx!=-1){
                f(idx);
                return 1;
            }
            if(l->find(x,y,K,f,once)&&once)return 1;
            return r->find(x,y,K,f,once);
        }
    };
    Node*root;
    kdtree(vector<pair<T,T>>&xy){
        vector<array<T,3>>X(xy.size());
        rep(i,xy.size())X[i]={xy[i].first,xy[i].second,(T)i};
        root=new Node(X,0,X.size(),0);
    }
    void apply(T x1,T x2,T y1,T y2,F x){
        static_assert(Merge);
        static_assert(Apply);
        root->apply(x1,x2,y1,y2,x);
    }
    S prod(T x1,T x2,T y1,T y2){    
        static_assert(op);
        return root->prod(x1,x2,y1,y2);
    }
    S get(T X,T Y){
        auto ret=root->get(X,Y);
        assert(ret.has_value());
        return ret.value();
    }
    template<class Show>
    void find(T x,T y,T K,Show f,bool once){
        root->find(x,y,K,f,once);
    }
};
//template<class T,class S,class F,auto op,auto e,auto Apply,auto Merge,auto id,auto init>
using NP=nullptr_t;
struct T{
    int able;
    int val,last;
    T():able(0),val(0),last(0){}
    int gain(int T){
        if(able)return val+T-last;
        else    return val;
    }
};

T e(){
    return T();
}
struct F{
    int is_able;
    int time;
    int offset;
    F():is_able(0),time(-1),offset(0){}
    F(int a,int b,int c):is_able(a),time(b),offset(c){}
};
T Apply(T a,F b){
    if(b.time==-1)return a;
    if(a.able==b.is_able)return a;
    a.val+=b.offset;
    if(a.able==0){
        assert(b.is_able==1);
        a.able=1;
        a.last=b.time;
        return a;
    }else{
        assert(b.is_able==0);
        a.able=0;
        a.val+=b.time-a.last;
        a.last=b.time;
        return a;
    }
}
F Merge(F a,F b){   
    if(b.time==-1)return a;
    if(a.time==-1)return b;
    if(a.is_able==b.is_able){
        if(a.is_able){
            a.offset+=b.time-a.time;
        }
        a.time=b.time;
        return a;
    }else if(a.is_able==1&&b.is_able==0){
        a.offset+=b.time-a.time;
        a.time=b.time;
        a.is_able=0;
        return a;
    }else{
        a.time=b.time;
        a.is_able=1;
        return a;
    }
    assert(0);
}
F id(){
    return F();
}
void solve(){
    INT(n,q);
    STR(s);
    vc<vc<int>>query(q);
    vc<pair<int,int>>xy;
    rep(t,q){
        string s;
        cin>>s;
        if(s=="toggle"){
            INT(i);
            query[t]={--i};
        }else{
            INT(a,b);
            --b;
            query[t]={--a,--b};
            xy.push_back({a,b});
        }
    }
    sort(all(xy));
    xy.erase(unique(all(xy)),xy.end());
    kdtree<int,T,F,0,e,Apply,Merge,id,0>kd(xy);
    set<int>off{-1,n};
    rep(i,n){
        if(s[i]=='0'){
            off.insert(i);
        }
    }
    rep(i,n){
        if(s[i]=='0'){
            auto it2=prev(off.lower_bound(i));
            if(*it2+1<=i-1)kd.apply(*it2+1,i-1,*it2+1,i-1,F(1,0,0));
        }
    }
    auto it=prev(off.end());
    if(*it+1<=n-1)kd.apply(*it+1,n-1,*it+1,n-1,F(1,0,0));
    rep(i,q){
        if(query[i].size()==1){
            int tar=query[i][0];
            if(off.count(tar)){
                auto it=off.upper_bound(tar);
                auto it2=prev(off.lower_bound(tar));
                //[L,R] が valid
                kd.apply(*it2+1,*it-1,*it2+1,*it-1,F(1,(int)i+1,0));
                off.erase(tar); 
            }else{
                auto it=off.upper_bound(tar);
                auto it2=prev(off.lower_bound(tar));
                kd.apply(*it2+1,tar,tar,*it-1,F(0,(int)i+1,0));
                off.insert(tar);
            }
        }else{
            cout<<kd.get(query[i][0],query[i][1]).gain(i+1)<<"\n";
        }
    }
}

signed main(){
    int t=1;
    //cin>>t;
    while(t--)solve();
}
/*
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 2 3
query 1 1
*/
#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...