2017杭州CCPC J - Master of GCD(線段樹)

題意:初始序列爲1,兩種操作,一種區間所有數 * 2, 一種是區間所有數 * 3 ,問最後所有數的最大公約數

題解:區間乘+區間GCD肯定不對,因爲有取模操作,對乘積結果取模再求GCD肯定不對了

          然後就想到維護每個結點乘2的次數和乘3的次數,分別取最小值就好了 ,ans=2^{x}*3^{y},x,y分別是區間最小2的個數和3的個數

          線段樹區間修改+最小值

細節:快速冪記得開 LL,中間會爆int

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod =  998244353;
int sum2[maxn*4],lazy2[maxn*4];
int sum3[maxn*4],lazy3[maxn*4];
void pushdown(int l,int r,int p,int sum[],int lazy[]){
	int mid=(l+r)/2;
	int pp=p;
	if(lazy[p]){
		sum[p<<1]+=lazy[p];
		sum[p<<1|1]+=lazy[p];
		lazy[p<<1]+=lazy[p];
		lazy[p<<1|1]+=lazy[p];
		lazy[p]=0;
	}
}
void build(int l, int r,int p){
	sum2[p]=sum3[p]=0;
	lazy2[p]=lazy3[p]=0;
	if(l==r){
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,p<<1);
	build(mid+1,r,p<<1|1);
	
}
void update(int l,int r,int L,int R,int p,int sum[],int lazy[]){
	if(L<=l&&r<=R){
		sum[p]+=1;
		lazy[p]+=1;
		return ;
	}
	pushdown(l,r,p,sum,lazy);
	int mid=(l+r)/2;
	if(L<=mid) update(l,mid,L,R,p<<1,sum,lazy);
	if(R>mid) update(mid+1,r,L,R,p<<1|1,sum,lazy);
	sum[p]=min(sum[p<<1],sum[p<<1|1]);
}
int query(int l,int r,int L,int R,int p,int sum[],int lazy[]){
	if(L<=l&&r<=R){
		return sum[p]; 
	}
	pushdown(l,r,p,sum,lazy);
	int mid=(l+r)/2;
	int ans=INF;
	if(L<=mid) ans=min(ans,query(l,mid,L,R,p<<1,sum,lazy));
	if(R>mid) ans=min(ans,query(mid+1,r,L,R,p<<1|1,sum,lazy));
	return ans;
}
ll pow(ll a,int b){
   ll ans=1;
   while(b){
   	   if(b&1) ans=ans*a%mod;
   	   a=a*a%mod;
   	   b=b/2;
   }
   return ans%mod;
}
int main(){
	int t; 
	scanf("%d",&t);
	while(t--){
		int n; scanf("%d",&n);
		int m; scanf("%d",&m);
        build(1,n,1);
		while(m--){
			int l,r,x;
			scanf("%d%d%d",&l,&r,&x);
			if(x==2)
			update(1,n,l,r,1,sum2,lazy2);
			else update(1,n,l,r,1,sum3,lazy3);
		}
		int xx=query(1,n,1,n,1,sum2,lazy2);
		int yy=query(1,n,1,n,1,sum3,lazy3);
		ll ans=pow(2*1ll,xx)*pow(3*1ll,yy)%mod;
		printf("%lld\n",ans);
		
	}
	return 0;
} 
/*
2
5 3
1 3 2 
3 5 2
1 5 3
6 3
1 2 2
5 6 2
1 6 2




*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章