[TJOI2018] math
Title Description
There are a number of adzuki x, Q has an initial value of 1. adzuki operations, there are two types of operations:
1 m: x = x × m output x% mod;
2 pos: x = x / number of times of operation multiplied pos (pos operations must ensure that the first type 1, type 1 for the operation of each other will be at most once) output x% mod;
Input Format
There are a set of input t (t≤5);
For each set of input, the first line is two numbers Q, mod (Q≤100000, mod≤100000000);
Next Q lines, each operation type behavior op, the operation number or the number m is multiplied (to ensure that all inputs are valid).
Output Format
For each operation, the output row contains the value x% mod operation performed after
At first glance I thought it was a road simulation title, a closer look will find that burst longlong, then thought it was a math problem;
In fact, you can carry out his segment tree, interval tree line maintenance product line, when pos = 2, a single point on the line modification;
In many cases like this, thinking is more important than the algorithm;
Code:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100100;
const int M=2000100;
LL mod,m;
struct Node{
int l,r;
LL w;
}tr[N*4];
void build(int l,int r,int k){
tr[k].l=l,tr[k].r=r;
if(l==r){
tr[k].w=1;
return;
}
int d=(l+r)>>1;
build(l,d,ls);
build(d+1,r,rs);
(tr[k].w=tr[ls].w*tr[rs].w)%=mod;
}
void update(int pos,LL val,int k){
if(tr[k].l==tr[k].r){
tr[k].w=val;
return;
}
int d=(tr[k].l+tr[k].r)>>1;
if(pos<=d) update(pos,val,ls);
else update(pos,val,rs);
(tr[k].w=tr[ls].w*tr[rs].w)%=mod;
}
int main(){
int t;
cin>>t;
while(t--){
int q;
cin>>q>>mod;
build(1,q,1);
for(int i=1;i<=q;i++){
int op;
scanf("%d%lld",&op,&m);
if(op==1){
update(i,m,1);
printf("%lld\n",tr[1].w);
}
else{
update(m,1,1);
printf("%lld\n",tr[1].w);
}
}
}
return 0;
}