TJOI 2018 数学计算

题链

 对时间建线段树,当然,你要写中国剩余定理我也是很资瓷。

#include<bits/stdc++.h>
#define LL long long
#define N 300007
#define sight(x) ('0'<=x&&x<='9')
#define Mid (l+r>>1)
#define Ls No<<1,l,Mid
#define Rs No<<1|1,Mid+1,r
using namespace std;
LL p[N];
int mo,T,x,op,q;
inline void read(int &x){
    static char c;
    for (c=getchar();!sight(c);c=getchar());
    for (x=0;sight(c);c=getchar())x=x*10+c-48;
}
void write(LL x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);}
inline void writeln(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); }
inline void writel(LL x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); }
void change(int No,int l,int r,int id,int xx){
    if (l==r) {
        assert(id==l);
        p[No]=xx; return;
    }
    if (id<=Mid) change(Ls,id,xx);
    else change(Rs,id,xx);
    p[No]=p[No<<1]*p[No<<1|1]%mo;
}
void build(int No,int l,int r) {
    if (l==r) {
        p[No]=1; return;
    }
    build(Ls); build(Rs);
    p[No]=p[No<<1]*p[No<<1|1]%mo;
}
signed main () {
//    freopen("b.in","r",stdin);
    read(T);
    while (T--) {
        read(q); read(mo);
        build(1,1,q);
        for (int i=1;i<=q;i++) {
            read(op); read(x);
            if (op==1) change(1,1,q,i,x);
            else change(1,1,q,x,1);
            writeln(p[1]);
        }
    } return 0;
}

 

posted @ 2018-05-18 21:29  泪寒之雪  阅读(190)  评论(0编辑  收藏  举报