1. 程式人生 > >FZU 2297 Number theory【線段樹/單點更新/思維】

FZU 2297 Number theory【線段樹/單點更新/思維】

list algo divide val 復雜 ger wing scrip ive

Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide.
Input
First line of the input file contains an integer T(0 < T ≤ 10) that indicates how many cases of inputs are there.

The description of each case is given below:

The first line contains two integers Q and M. The next Q lines contains the operations in ith line following form:

M yi: x = x * yi.

N di: x = x / ydi.

It’s ensure that di is different. That means you can divide yi only once after yi came up.

0 < yi ≤ 10^9, M ≤ 10^9

Output
For each operation, print an integer (one per line) x % M.

Sample Input
1
10 1000000000
M 2
D 1
M 2
M 10
D 3
D 4
M 6
M 7
M 12
D 7
Sample Output
2
1
2
20
10

1
6
42
504
84

【分析】
針對一個數組的操作,即對一個區間。可以用線段樹去進行維護。初始化建樹,葉子節點的值為1,維護每段區間上各個元素的乘積sum。M yi,將第i個元素的值改為yi。N di,將第di個元素的值改為1。輸出即查詢區間[1,Q]的sum值。也就是變成了單點更新、區間查詢問題。
時間復雜度為O(QlongQ)。

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<list>
#include<bitset>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5;
const double eps = 1e-8;

LL mod,val;
LL sum[maxn*4];

void update(int p,int val,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=val;
        return ;
    }
    int  m=(l+r)/2;
    if(p<=m)
        update(p,val,l,m,rt*2);
    else
        update(p,val,m+1,r,rt*2+1);
    sum[rt] = sum[rt*2] * sum[rt*2+1] % mod;
}
//char op[10];
int main()
{
    //ios::sync_with_stdio(false);
    int t,q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld",&q,&mod);
        for(int i=1;i<=4*maxn;i++) sum[i]=1;
        for(int i=1;i<=q;i++)
        {
            int x;char op[10];
            scanf("%s%d",op,&x);
            if(op[0]=='M')
            {
                update(i,x,1,maxn,1);
                printf("%lld\n",sum[1]);
            }
            else
            {
                update(x,1,1,maxn,1);
                printf("%lld\n",sum[1]);
            }
        }
    }
    return 0;
}
/*
1
10 1000000000
M 2
D 1
M 2
M 10
D 3
D 4
M 6
M 7
M 12
D 7

2
1
2
20
10
1
6
42
504
84
*/

FZU 2297 Number theory【線段樹/單點更新/思維】