# # Luo segment tree valley 4588 [TJOI2018] math

Topic Portal


analysis

Since the original title have done
so directly say, because each number up to be removed once
it is possible to take a segment tree maintenance intervals, it is very simple, just delete modify a single point on the line


Code

#include <cstdio>
#include <cctype>
#define rr register
using namespace std;
int Q,mod,w[400011];
inline signed iut(){
    rr int ans=0; rr char c=getchar();
    while (!isdigit(c)) c=getchar();
    while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
    return ans;
}
inline void print(int ans){
    if (ans>9) print(ans/10);
    putchar(ans%10+48);
}
inline void build(int k,int l,int r){
    if (l==r) {w[k]=1; return;}
    rr int mid=(l+r)>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
    w[k]=1ll*w[k<<1]*w[k<<1|1]%mod;
}
inline void update(int k,int l,int r,int x,int z){
    if (l==r) {w[k]=z+(!z); return;}
    rr int mid=(l+r)>>1;
    if (x<=mid) update(k<<1,l,mid,x,z);
        else update(k<<1|1,mid+1,r,x,z);
    w[k]=1ll*w[k<<1]*w[k<<1|1]%mod;
}
signed main(){
    for (rr int T=iut();T;--T){
        Q=iut(),mod=iut();
        build(1,1,Q);
        for (rr int i=1;i<=Q;++i){
            rr int z=iut(),x=iut();
            if (z==1) update(1,1,Q,i,x);
                 else update(1,1,Q,x,0);
            print(w[1]),putchar(10);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Spare-No-Effort/p/12355156.html