HDU 5475(2015 ICPC上海站网络赛)--- An easy problem(线段树点修改)

编程爱好者联盟 2016-10-25

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5475

Problem Description

One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first, X = 1. This calculator only supports two types of operation.1. multiply X with a number.2. divide X with a number which was multiplied before.After each operation, please output the number X modulo M.

Input

The first line is an integer T(1≤T≤10), indicating the number of test cases.For each test case, the first line are two integers Q and M. Q is the number of operations and M is described above. (1≤Q≤105,1≤M≤109)The next Q lines, each line starts with an integer x indicating the type of operation.if x is 1, an integer y is given, indicating the number to multiply. (0<y≤109)if x is 2, an integer n is given. The calculator will divide the number which is multiplied in the nth operation. (the nth operation must be a type 1 operation.)It's guaranteed that in type 2 operation, there won't be two same n.

Output

For each test case, the first line, please output "Case #x:" and x is the id of the test cases starting from 1.Then Q lines follow, each line please output an answer showed by the calculator.

Sample Input

1

10 1000000000

1 2

2 1

1 2

1 10

2 3

2 4

1 6

1 7

1 12

2 7

Sample Output

Case #1:

2

1

2

20

10

1

6

42

504

84

Source

2015 ACM/ICPC Asia Regional Shanghai Online

Recommend

hujie   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 

题意:输入Q和M,然后输入Q次操作,每次操作为一行数据op和s,定义一个数x=1。如果op=1表示x=x*s%mod 输出x,如果op=2 表示x除以第s次操作的s(第s次操作的op一定为1),输出x;

思路:线段树点修改,每次操作时,修改那个点,复杂度为log(n),如果op=1,那么修改为s,如果op=2,那么第s次操作对应的点修改为1;

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
LL mod;
LL tr[4*100005];
int p;
LL add;
void build(int l,int r,int i)
{
    if(l==r) {
        tr[i]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,i<<1);
    build(mid+1,r,i<<1|1);
    tr[i]=1;
}

void update(int l,int r,int i)
{
    if(l==r) { tr[i]=add; return ; }
    int mid=(l+r)>>1;
    if(p<=mid) update(l,mid,i<<1);
    else update(mid+1,r,i<<1|1);
    tr[i]=(tr[i<<1]*tr[i<<1|1])%mod;
}

int main()
{
    int T,Q,Case=1;
    cin>>T;
    while(T--)
    {
        scanf("%d%lld",&Q,&mod);
        printf("Case #%d:\n",Case++);
        build(1,Q,1);
        for(int i=1;i<=Q;i++)
        {
            int x;
            LL y;
            scanf("%d%lld",&x,&y);
            if(x==1) { p=i; add=y; }
            else { p=y; add=1; }
            update(1,Q,1);
            printf("%lld\n",tr[1]);
        }
    }
    return 0;
}

相关推荐