Luo Gu problem solution [P4588] [TJOI2018] math

Title Description

There are a number of adzuki \ (X \) , the initial value \ (1 \) adzuki beans with a. \ (Q \) operations, there are two types of operations:

\ (. 1 \; m \) : \ (X = X \ Times m \) output \ (X \% MOD \) ;

\ (2 \; POS \) : \ (X = X / \) of \ (POS \) number of times of operation multiplied (Guarantee \ (POS \) operations to a certain type \ (1 \) , for each a type \ (1 \) operation will be up to one other) output \ (X \% MOD \) ;

Input Format

A total of \ (T \) set of input ( \ (T \ leq5 \) );

For each set of input, the first line is two digits \ (Q, MOD \) ( \ (Q \ leq100000, MOD \ leq100000000 \) );

Next \ (Q \) rows, each act operation type \ (OP \) , the operation or multiplied numbered \ (m \) (to ensure that all inputs are valid).

Output Format

For each operation, the output line, comprising the operation execution \ (x \% mod \) values

Sample input and output

Input # 1

1
10 1000000000
1 2
2 1
1 2
1 10
2 3
2 4
1 6
1 7
1 12
2 7

Output # 1

2
1
2
20
10
1
6
42
504
84

Description / Tips

For \ (20 \% \) data, \ (. 1 \ Leq Q \ leq500 \)

For \ (100 \% \) data, \ (. 1 \ Leq Q \ leq100000 \)

answer

This question is difficult in thinking, think how difficult the tree line.

Violence simulation is easy to think, however, in this question, the violence simulation is wrong! ! !

A set of \ (\ texttt {hack} \ ) Data:

1
2 10
1 99
2 99

Output should be:

9
1

Looks like you can with precision, but the spatial complexity touching ...... ~~

Directly speak here of positive solutions.

Maintenance operations with a product section segment tree, if \ (1 \) value of the current node operation will be changed to \ (m \) , \ (2 \) values of nodes where the number of operations in addition to put instead \ (1 \) .

The output value can then direct the output line ...... root node.

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define int long long//注意long long
#define itn int
#define gI gi

using namespace std;

inline int gi()//快读
{
    int f = 1, x = 0; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return f * x;
}

int t, q, mod, tr[400003], ans;

inline int ls(int p) {return p << 1;}//左儿子
inline int rs(itn p) {return p << 1 | 1;}//右儿子

void modify(int ql, int qr, int z, int l, int r, int p)//修改节点
{
    if (l == r && l == ql) {tr[p] = z; return;}//到了叶子节点进行修改
    int mid = (l + r) >> 1;
    if (ql <= mid) modify(ql, qr, z, l, mid, ls(p));//递归左子树
    if (qr > mid) modify(ql, qr, z, mid + 1, r, rs(p));//递归右子树
    tr[p] = (tr[ls(p)] % mod * tr[rs(p)] % mod) % mod;//上传节点
    return;
}

signed main()
{
    t = gi();
    while (t--)
    {
        q = gi(), mod = gi();
        for (int i = 1; i <= 400001; i+=1) tr[i] = 1;//注意,本题中不需要建树,只需要把所有节点的值设为1
        for (int i = 1; i <= q; i+=1)
        {
            int op = gi(), m = gi();
            if (op == 1) modify(i, i, m, 1, q, 1);//操作1,将第i个点的值修改为m
            else modify(m, m, 1, 1, q, 1);//操作2,将第m个点的值修改为1
            printf("%lld\n", tr[1]);//输出
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xsl19/p/11617062.html