题意:给你n个数和mod,X=1;接下来的n个数a,b,如果a=1,表示X=X*b;如果a=2,表示X=X/(第b个数的b值),线段树做之;if (a==2)将第b个数表示成1;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const LL maxm=1e5+10;
LL num[maxm<<2];
LL add[maxm<<2];
LL type[maxm<<2];
LL val[maxm<<2];
LL sum[maxm<<2];
LL n,ans;
LL pow(LL a,LL b)
{
LL s=1;
for(LL i=1;i<=b;i++)
{
s*=a;
}
return s;
}
void pushup(LL rt)
{
sum[rt]=(sum[rt<<1]*sum[rt<<1|1])%ans;
}
void pushdown(LL rt,LL m)
{
if(add[rt])
{
add[rt<<1]=add[rt];
add[rt<<1|1]=add[rt];
sum[rt<<1]=pow(add[rt],(m-(m>>1)));
sum[rt<<1|1]=pow(add[rt],(m>>1));
add[rt]=0;
}
}
void build(LL l,LL r,LL rt)
{
if(l==r)
{
sum[rt]=num[l]%ans;
add[rt]=sum[rt];
return;
}
LL m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void updata(LL L,LL R,LL c,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
add[rt]=c;
sum[rt]=pow(c,r-l+1);
return;
}
pushdown(rt,r-l+1);
LL m=(l+r)>>1;
if(L<=m)
updata(L,R,c,lson);
if(R>m)
updata(L,R,c,rson);
pushup(rt);
}
LL querty(LL L,LL R,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+1);
LL m=(l+r)>>1;
LL cnt=1;
if(L<=m)
cnt=(cnt*querty(L,R,lson))%ans;
if(R>m)
cnt=(cnt*querty(L,R,rson))%ans;
return cnt%ans;
}
int main()
{
LL t;
LL k=1;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&ans);
printf("Case #%lld:\n",k++);
for(LL i=1; i<=n; i++)
{
scanf("%lld%lld",&type[i],&val[i]);
if(type[i]==1)
{
num[i]=val[i];
}
if(type[i]==2)
{
num[i]=1;
}
}
build(1,n,1);
for(LL i=1; i<=n; i++)
{
if(type[i]==1)
{
printf("%lld\n",querty(1,i,1,n,1)%ans);
}
if(type[i]==2)
{
updata(val[i],val[i],1,1,n,1);
printf("%lld\n",querty(1,i,1,n,1)%ans);
}
}
}
return 0;
}
//这样更方便一点,直接取sum[1],就是现在所求的X值
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const LL maxm=1e5+10;
LL type[maxm<<2];
LL val[maxm<<2];
LL sum[maxm<<2];
LL n,ans;
void pushup(LL rt)
{
sum[rt]=(sum[rt<<1]*sum[rt<<1|1])%ans;
}
void build(LL l,LL r,LL rt)
{
if(l==r)
{
sum[rt]=1;
return;
}
LL m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void updata(LL L,LL R,LL c,LL l,LL r,LL rt)
{
if(L<=l&&R>=r)
{
sum[rt]=c;
return;
}
LL m=(l+r)>>1;
if(L<=m)
updata(L,R,c,lson);
if(R>m)
updata(L,R,c,rson);
pushup(rt);
}
int main()
{
LL t;
LL k=1;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&ans);
build(1,n,1);
printf("Case #%lld:\n",k++);
for(LL i=1; i<=n; i++)
{
scanf("%lld%lld",&type[i],&val[i]);
if(type[i]==1)
{
updata(i,i,val[i],1,n,1);
printf("%lld\n",sum[1]%ans);
}
if(type[i]==2)
{
updata(val[i],val[i],1,1,n,1);
printf("%lld\n",sum[1]%ans);
}
}
}
return 0;
}