Master of GCD线段树

问题 J: Master of GCD
时间限制: 1 Sec 内存限制: 128 MB
提交: 888 解决: 195
[提交] [状态] [命题人:admin]
题目描述
Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Hakase is interested in primes. She will choose a continuous subsequence [l, r] and a prime parameter x each time and for every l≤i≤r, she will change ai into ai*x. To simplify the problem, x will be 2 or 3. After m operations, Hakase wants to know what is the greatest common divisor of all the numbers.

输入
The first line contains an integer T (1≤T≤10) representing the number of test cases.
For each test case, the fi rst 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.

输出
For each test case, print an integer in one line, representing the greatest common divisor of the sequence. Due to the answer might be very large, print the answer modulo 998244353.

样例输入
复制样例数据
2
5 3
1 3 2
3 5 2
1 5 3
6 3
1 2 2
5 6 2
1 6 2
样例输出
6
2

提示
For the first test case, after all operations, the numbers will be [6,6,12,6,6]. So the greatest common divisor is 6.

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const long long mod = 1e9 + 7;
const int maxn = 100000 + 100;
using namespace std;
typedef long long ll;

const int modif=998244353;
ll sum2[maxn<<2],lazy2[maxn<<2],sum3[maxn<<2],lazy3[maxn<<2];

void PushUp2(ll rt)
{
    sum2[rt]=min(sum2[rt<<1],sum2[rt<<1|1]);
    lazy2[rt]=0;
}

void PushUp3(ll rt)
{
    sum3[rt]=min(sum3[rt<<1],sum3[rt<<1|1]);
    lazy3[rt]=0;
}

void PushDown2(ll rt,ll m)
{
    if (lazy2[rt])
    {
        lazy2[rt<<1] += lazy2[rt];
        lazy2[rt<<1|1]+=lazy2[rt];
        sum2[rt<<1] += (m - (m >> 1)) * lazy2[rt];
        sum2[rt<<1|1] += (m >> 1) * lazy2[rt];
        lazy2[rt] = 0;
    }

}

void PushDown3(ll rt,ll m){
    if (lazy3[rt])
    {
        lazy3[rt<<1] += lazy3[rt];
        lazy3[rt<<1|1]+=lazy3[rt];
        sum3[rt<<1] += (m - (m >> 1)) * lazy3[rt];
        sum3[rt<<1|1] += (m >> 1) * lazy3[rt];
        lazy3[rt] = 0;
    }
}

void update2(ll L,ll R,ll c,ll l,ll r,ll rt)
{
    if (L <= l && r <= R)
    {
        lazy2[rt] += c;
        sum2[rt] += c ;
        return ;
    }
    PushDown2(rt, r - l + 1);
    ll m = (l + r) >> 1;
    if (L <= m) update2(L, R, c, lson);
    if (m < R) update2(L, R, c, rson);
    PushUp2(rt);
}

void update3(ll L,ll R,ll c,ll l,ll r,ll rt)
{
    if (L <= l && r <= R)
    {
        lazy3[rt] += c;
        sum3[rt] += c ;
        return ;
    }
    PushDown3(rt, r - l + 1);
    ll m = (l + r) >> 1;
    if (L <= m) update3(L, R, c, lson);
    if (m < R) update3(L, R, c, rson);
    PushUp3(rt);
}
ll query2(ll L,ll R,ll l,ll r,ll rt)
{
    ll ans=inf;
    if (L <= l && r <= R)
    {
        return sum2[rt];
    }
    PushDown2(rt, r - l + 1);
    ll m = (l + r) >> 1;
    if (L <= m) ans=min(ans,query2(L, R, lson));
    if (m < R) ans=min(ans,query2(L, R, lson));
    return ans;
}
ll query3(ll L,ll R,ll l,ll r,ll rt)
{
    ll ans=inf;
    if (L <= l && r <= R)
    {
        return sum3[rt];
    }
    PushDown3(rt, r - l + 1);
    ll m = (l + r) >> 1;
    if (L <= m) ans=min(ans,query3(L, R, lson));
    if (m < R) ans=min(ans,query3(L, R, lson));
    return ans;
}

int main()
{
    int t,n,m,aa,bb,cc;
    scanf("%d",&t);
    while(t--)
    {
        memset(sum2,0,sizeof(sum2));
        memset(sum3,0,sizeof(sum3));
        memset(lazy2,0,sizeof(lazy2));
        memset(lazy3,0,sizeof(lazy3));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&aa,&bb,&cc);
            if(cc==2)
            {
                update2(aa,bb,1,1,n,1);
            }
            else
            {
                update3(aa,bb,1,1,n,1);
            }
        }
        ll s2=query2(1,n,1,n,1);
        ll s3=query3(1,n,1,n,1);
        ll res=1;
        if(s2>0){
            for(int i=0;i<s2;i++){
                res=(res*2)%modif;
            }
        }
        if(s3>0){
            for(int i=0;i<s3;i++){
                res=(res*3)%modif;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值