zoukankan      html  css  js  c++  java
  • 【TJOI2018】数学计算(线段树水题)

    描述
    小豆现在有一个数 xx,初始值为 11 。 小豆有 QQ次操作,操作有两种类型:
    11 mmx=x×mx=x×m ,输出 xmodMx mod M
    22 posposx=x/x=x/pospos次操作所乘的数(保证第 pospos 次操作一定为类型11,对于每一个类型11的操作至多会被除一次),输出xmodMx mod M
    输入
    一共有 tt 组输入。
    对于每一组输入,第一行是两个数字 Q,MQ,M
    接下来 QQ 行,每一行为操作类型 opop ,操作编号或所乘的数字mm(保证所有的输入都是合法的)。
    输出
    对于每一个操作,输出一行,包含操作执行后的xmodMx mod M的值
    样例输入
    1
    10 1000000000
    1 2
    2 1
    1 2
    1 10
    2 3
    2 4
    1 6
    1 7
    1 12
    2 7
    样例输出
    2
    1
    2
    20
    10
    1
    6
    42
    504
    84
    提示
    对于20%的数据,1Q5001≤Q≤500
    对于100%的数据, 1Q105,t5,M1091≤Q≤10^5, t≤5,M≤10^9

    第一眼还以为高精度乘除取膜

    瞬间放弃

    第二眼线段树水题

    维护一下区间积,每次除把一个点改回来就是了

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    inline int read(){
    	char ch=getchar();
    	int res=0,f=1;
    	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    	while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
    	return res*f;
    }
    const int N=100005;
    ll tr[N<<2],a[N],mod;
    int T,n;
    #define lc (u<<1)
    #define rc ((u<<1)|1)
    #define mid ((l+r)>>1)
    inline void pushup(int u){
    	tr[u]=tr[lc]*tr[rc]%mod;
    }
    inline void buildtree(int u,int l,int r){
    	tr[u]=1;
    	if(l==r)return;
    	buildtree(lc,l,mid);
    	buildtree(rc,mid+1,r);
    	pushup(u);
    }
    inline void update(int u,int l,int r,int pos,ll k){
    	if(l==r){
    		tr[u]=k;return;
    	}
    	if(pos<=mid)update(lc,l,mid,pos,k);
    	else update(rc,mid+1,r,pos,k);
    	pushup(u);
    }
    inline void change(int u,int l,int r,int pos){
    	if(l==r){
    		tr[u]=1;return;
    	}
    	if(pos<=mid)change(lc,l,mid,pos);
    	else change(rc,mid+1,r,pos);
    	pushup(u);
    }
    int main(){
    	int T=read();
    	while(T--){
    		n=read(),mod=read();
    		buildtree(1,1,n);
    		for(int i=1;i<=n;i++){
    			int op=read(),k=read();
    			if(op==1){
    				update(1,1,n,i,k);
    				cout<<tr[1]<<'
    ';
    			}
    			else {
    				change(1,1,n,k);
    				cout<<tr[1]<<'
    ';
    			}
    		}
    	}
    }
    
  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/10366363.html
Copyright © 2011-2022 走看看