Master of GCD HDU - 6273 (interval update, segment tree)

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.


The meaning of the question: input t, there are t groups of data, each group of data first input n, m means there are n points, m operations, there are m lines below, each line has three numbers, the first two are an interval, put this Each number in the interval is multiplied by the last number, and the last number can only be 2, 3; find, the greatest common multiple of the last n numbers, at the beginning, the n numbers are all 1

Idea: Due to data problems, we need to use a line segment tree to find the greatest common multiple of n numbers, just to see how many 2s and how many 3s are all in the n numbers;

It must be remembered: when there is a delay marker in this interval, the value in this interval is also updated at the same time, simultaneity; only the downlink delay marker is left; 

Code:

#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++; // Delay marker, be sure to remember that when there is a delay marker in this interval, the value of this interval has also been updated
			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);
}
intmain()
{
	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);
			update(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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324838489&siteId=291194637