[TJOI2018]数学计算

题目链接


  如果把这个问题看作是区间问题,那么会简单的多了,一开始整个区间都是1,所有的值都是1,然后就像是分治的做法,我们将他们分开来,算每个小子集的乘积取模,然后修改的时候其实就是置1或者是置当前数值。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
//#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N;
ll mod;
ll tree[maxN << 2];
inline void pushup(int rt) { tree[rt] = tree[lsn] * tree[rsn] % mod; }
void update(int rt, int l, int r, int qx, ll val)
{
    if(l == r) { tree[rt] = val; return; }
    int mid = HalF;
    if(qx <= mid) update(Lson, qx, val);
    else update(Rson, qx, val);
    pushup(rt);
}
int main()
{
    int T; scanf("%d", &T);
    while(T--)
    {
        scanf("%d%lld", &N, &mod);
        fill(tree + 1, tree + (N << 2) + 1, 1);
        for(int i=1, op, x; i<=N; i++)
        {
            scanf("%d%d", &op, &x);
            if(op == 1) update(1, 1, N, i, x);
            else update(1, 1, N, x, 1);
            printf("%lld\n", tree[1]);
        }
    }
    return 0;
}

 

你可能感兴趣的:(线段树)