HDU 5475 An easy problem 线段树

An easy problem

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

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

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

HINT

 

题意

一开始ans = 1

有两个操作

1.乘以x

2.除以第y个加入的数

然后需要mod

题解:

显然,不用mod的话,就是傻逼题了

但是有一个mod,那么我求逆元就好了

但是很蛋疼的是,有些数并没有逆元怎么办?

那就线段树咯……

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1)

using namespace std;
const int maxn = 1e5 + 500;
int Q;
long long MOD,X,C[maxn];

typedef long long SgTreeDataType;
struct treenode
{
  int L , R  ;
  SgTreeDataType sum ;
  void updata(SgTreeDataType v)
  {
      sum = v;
  }
};

treenode tree[maxn*4];
inline void push_up(int o)
{
    tree[o].sum = (tree[2*o].sum * tree[2*o+1].sum)%MOD;
}

inline void build_tree(int L , int R , int o)
{
    tree[o].L = L , tree[o].R = R,tree[o].sum = 1LL;
    if (R > L)
    {
        int mid = (L+R) >> 1;
        build_tree(L,mid,o*2);
        build_tree(mid+1,R,o*2+1);
    }
}

inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
    int L = tree[o].L , R = tree[o].R;
    if (QL <= L && R <= QR) tree[o].updata(v);
    else
    {
        int mid = (L+R)>>1;
        if (QL <= mid) updata(QL,QR,v,o*2);
        if (QR >  mid) updata(QL,QR,v,o*2+1);
        push_up(o); 
    }
}

inline SgTreeDataType query(int QL,int QR,int o)
{
    int L = tree[o].L , R = tree[o].R;
    if (QL <= L && R <= QR) return tree[o].sum;
    else
    {
        int mid = (L+R)>>1;
        SgTreeDataType res = 1LL;
        if (QL <= mid) res *= query(QL,QR,2*o);
        if(res >= MOD) res %= MOD;
        if (QR > mid) res *= query(QL,QR,2*o+1);
        if(res >= MOD) res %= MOD;
        return res;
    }
}


void initiaiton()
{
    X=1;
    scanf("%d%I64d",&Q,&MOD);
    build_tree(1,Q,1);
}

void solve()
{
    for(int i = 1 ; i <= Q ; ++ i)
    {
        int type ;
        long long y;
        scanf("%d%I64d",&type,&y);
        if(type == 1)
        {
            updata(i , i , y , 1);
            printf("%I64d\n",query(1,Q,1));
        }
        else
        {
            updata(y , y , 1 , 1);
            long long cx = query(1,Q,1);
            printf("%I64d\n",cx);
            X = cx;
        }
    }
}


int main(int argc,char *argv[])
{
    int Case;
    scanf("%d",&Case);
    int cas=1;
    while(Case--)
    {
        initiaiton();
        printf("Case #%d:\n",cas++);
        solve();
    }
    return 0;
}

 

    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/4841359.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞