hdu 6273 Master of GCD(线段树区间维护两个最小值)
题目:点击打开链接
ppt:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf
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.
题意:
给定区间内乘2或乘3,求1到n的最大公约数。
只要想到是求1到n都有的2或3的个数,就好做了,用线段树维护这两个值即可。
#include <bits/stdc++.h>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define ll long long int
using namespace std;
const int maxn=100010;
const int mod=998244353;
int n,m;
struct node{
int l,r;
ll sum2,sum3;//维护区间内2,3数量的最小值
}t[maxn<<2];
ll lazy2[maxn<<2],lazy3[maxn<<2];
void down(int rt){
if(lazy2[rt]){
ll tmp=lazy2[rt];
lazy2[rt<<1]+=tmp,lazy2[rt<<1|1]+=tmp;
t[rt<<1].sum2+=tmp,t[rt<<1|1].sum2+=tmp;
lazy2[rt]=0;
}
if(lazy3[rt]){
ll tmp=lazy3[rt];
lazy3[rt<<1]+=tmp,lazy3[rt<<1|1]+=tmp;
t[rt<<1].sum3+=tmp,t[rt<<1|1].sum3+=tmp;
lazy3[rt]=0;
}
}
void build(int rt,int l,int r){
t[rt].l=l,t[rt].r=r;
t[rt].sum2=0,lazy2[rt]=0;
t[rt].sum3=0,lazy3[rt]=0;
if(l==r){
return ;
}
int mid=(l+r)>>1;
build(lson),build(rson);
}
void update(int rt,int l,int r,int k){
if(l<=t[rt].l&&r>=t[rt].r){
if(k==2) lazy2[rt]++,t[rt].sum2++;
if(k==3) lazy3[rt]++,t[rt].sum3++;
return ;
}
down(rt);
int mid=(t[rt].l+t[rt].r)>>1;
if(l<=mid) update(rt<<1,l,r,k);
if(r>mid) update(rt<<1|1,l,r,k);
t[rt].sum2=min(t[rt<<1].sum2,t[rt<<1|1].sum2);
t[rt].sum3=min(t[rt<<1].sum3,t[rt<<1|1].sum3);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
update(1,l,r,k);
}
ll ans=1; //。。。忘了中间爆int
for(ll i=0;i<t[1].sum2;i++)
ans=ans*2%mod;
for(ll i=0;i<t[1].sum3;i++)
ans=ans*3%mod;
printf("%lld\n",ans);
}
return 0;
}
上一篇: 基础实验4-2.1-树的同构-编程题