1. 程式人生 > >Master of GCD HDU

Master of GCD HDU

Hakase has n numbers in a line. At first, they are all equal to 1. Besides, Hakase is interested inprimes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and forevery l ≤ i ≤ r, she will change aiinto ai ∗ x. To simplify the problem, x will be 2 or 3. After moperations, Hakase wants to know what is the greatest common divisor of all the numbers.InputThe first line contains an integer T (1 ≤ T ≤ 10) representing the number of test cases.For each test case, the first line contains two integers n (1 ≤ n ≤ 100000) and m (1 ≤ m ≤ 100000),where n refers to the length of the whole sequence and m means there are m operations.The following m lines, each line contains three integers li (1 ≤ li ≤ n), ri (1 ≤ ri ≤ n), xi (xi ∈ {2, 3}),which are referred above.OutputFor each test case, print an integer in one line, representing the greatest common divisor of thesequence. Due to the answer might be very large, print the answer modulo 998244353.


題意:輸入t,為有t組資料,每組資料先輸入 n,m表示有n個點,m個操作,下面有m行,每行有三個數,前兩個為一個區間,把這個區間中的 每個數都乘以最後一個數,最後一個數只能是 2,3;求,最後這個n個數的最大公倍數,剛開始這n個數都是1

思路:由於資料問題,所以要用到線段樹,求n個數的最大公倍數,就是看看有幾個2和幾個3 全部在 n個數中;

一定要記住:當這個區間有延遲標記時,同時這個區間中的值也被更新了,同時性;只剩下向下傳延遲標記; 

程式碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define Max 100010
#define mod 998244353
struct node
{
	int x;    //
	int y;
	int nnode2;	
	int nnode3;
}stu[Max*4];
int n,m;


void build(int root,int star,int end)
{
	stu[root].nnode2 = 0;
	stu[root].nnode3 = 0;
	stu[root].x = 0;
	stu[root].y = 0;
	if(star==end)
		return ;
	int mid = (star+end)/2;
	build(2*root,star,mid);
	build(2*root+1,mid+1,end);
}

void updet(int root,int star,int end,int l,int r,int val)
{
	
	if(l>end||r<star)
		return;
	if(l<=star&&end<=r)
	{
		if(val==2)
		{
			//printf("root==%d\n",root);
			stu[root].nnode2++;     // 延遲標記,一定要記住這個區間有延遲標記時,同時這個區間的值也被更新過了 
			stu[root].x++;
		}
		else
		{
			stu[root].y++;
			stu[root].nnode3++;
		} 
		return ;
	}
	
	if(stu[root].nnode2)
	{
		stu[root*2].nnode2 += stu[root].nnode2;
		stu[root*2+1].nnode2 += stu[root].nnode2;
		stu[root*2].x += stu[root].nnode2;
		stu[root*2+1].x +=stu[root].nnode2;
		stu[root].nnode2 = 0;		
	}
	if(stu[root].nnode3)
	{
		stu[root*2].nnode3 += stu[root].nnode3;
		stu[root*2+1].nnode3 += stu[root].nnode3;
		stu[root*2].y += stu[root].nnode3;
		stu[root*2+1].y +=stu[root].nnode3;
		stu[root].nnode3 = 0;
	}
	int mid = (star+end)/2;
	if(l<=mid) updet(root*2,star,mid,l,r,val);
	if(r>mid) updet(root*2+1,mid+1,end,l,r,val);
	
	stu[root].x = min(stu[root*2].x,stu[root*2+1].x);
	stu[root].y = min(stu[root*2].y,stu[root*2+1].y);
}
int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		build(1,1,n);
		//printf("n==%d\n",n);
		int x,y,val;
		for(i = 0;i<m;i++)
		{
			scanf("%d%d%d",&x,&y,&val);
			updet(1,1,n,x,y,val);			
		}
		long long sum = 1;
		for(i = 0;i<stu[1].x;i++)
		{
			sum= (sum*2)%mod;
		}
		for(i = 0;i<stu[1].y;i++)
		{
			sum = sum*3%mod;
		}
		printf("%lld\n",sum);
	}
	return 0;
}