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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值