hdu5475 An easy problem 線段樹

HDU 5475 An easy problem

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N=100005;
ll dat[4*N];
int f[N];
void build(int rt,int l,int r)
{
    dat[rt]=1;
    if(l==r)
    {
        f[l]=rt;
        return;
    }
    int mid=(l+r)/2;
    build(rt*2,l,mid);
    build(rt*2+1,mid+1,r);
}
void update(int rt,int m)
{
    if(rt==1) return;
    int ft=rt/2;
    dat[ft]=(dat[ft*2]*dat[ft*2+1])%m;
    update(ft,m);
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        //memset(dat,,sizeof dat);
        for(int i=0;i<4*N;i++)
            dat[i]=1;
        ll res=1;
        int c=0;
        build(1,1,n);
        printf("Case #%d:\n",cas++);
        for(int i=1;i<=n;i++)
        {
            c=i;
            int x,y;
            scanf("%d%d",&x,&y);
           if(x==1)
           {
               y%=m;
               dat[f[c]]=(dat[f[c]]*1ll*y)%m;
               update(f[c],m);
               res=dat[1]%m;
               printf("%I64d\n",res);
           }
           else if(x==2)
           {
               int rt=f[y];
               dat[rt]=1;
               update(rt,m);
               res=dat[1]%m;
               printf("%I64d\n",res);
           }
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章