Master of GCD

时间限制: 1 Sec  内存限制: 128 MB

提交: 670  解决: 112

[提交] [状态] [命题人: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.

只需要求出【1,n】中乘2 和 乘3的最少次数

比赛的时候写的线段树    结束了学弟说用差分可以很快解决,给跪了orz

线段树代码:

#include<iostream>
#include<cstdio>     //EOF,NULL
#include<cstring>    //memset
#include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm>  //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h>     //INT_MAX
#include<bitset> // bitset<?> n
using namespace std;

#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long LL;
typedef long long ll;
const double eps=1e-8;
const double PI = acos(1.0);
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9+7;
const ll mod = 998244353;
const int MAXN = 1e6+7;
const int maxm = 1;
const int maxn = 100000+10;
int T;
int n,m;
struct node {
  ll l, r;
  int lazy2,lazy3;
  ll t;
  ll cnt2,cnt3;
}tree[maxn << 2];
int a,b,x;

void pushup(int k){
  tree[k].cnt2 = min(tree[k<<1].cnt2,tree[k<<1|1].cnt2);
  tree[k].cnt3 = min(tree[k<<1].cnt3,tree[k<<1|1].cnt3);
}
void pushdown(int k)
{
    if(tree[k].lazy2)
    {
        tree[k<<1].cnt2 += tree[k].lazy2;
        tree[k<<1|1].cnt2 += tree[k].lazy2;
        tree[k<<1].lazy2 += tree[k].lazy2;
        tree[k<<1|1].lazy2 += tree[k].lazy2;
        tree[k].lazy2 = 0;
    }
    if(tree[k].lazy3)
    {
        tree[k<<1].cnt3 += tree[k].lazy3;
        tree[k<<1|1].cnt3 += tree[k].lazy3;;
        tree[k<<1].lazy3 += tree[k].lazy3;
        tree[k<<1|1].lazy3 += tree[k].lazy3;
        tree[k].lazy3 = 0;
    }
}

void build(int l,int r,int k){
  tree[k].l = l;
  tree[k].r = r;
  tree[k].lazy2 = 0;
  tree[k].lazy3 = 0;
  tree[k].cnt2 = 0;
  tree[k].cnt3 = 0;
  if(l == r){
//    tree[k].t = 1 ;
    return ;
  }
  int mid =  (r + l) >> 1 ;
  build(l, mid, k << 1);
  build(mid + 1,r , k << 1|1);
  pushup(k);
}

void updata(int a,int b,int k,int x){
  if(tree[k].l == tree[k].r)
  {
    if(x == 2)
      tree[k].cnt2 ++;
    if(x == 3)
      tree[k].cnt3 ++;
     return ;
  }
  if(a <= tree[k].l && b >= tree[k].r )
  {
    if(x == 2){
      tree[k].cnt2 ++;
      tree[k].lazy2 ++;
    }
    if(x == 3){
      tree[k].cnt3 ++;
      tree[k].lazy3 ++;
    }
    return ;
  }
  pushdown(k);
  int mid = (tree[k].l + tree[k].r) >> 1;
  if(a <= mid){
    updata(a,b,k<<1,x);
  }
  if(b > mid){
    updata(a,b,k<<1|1,x);
  }
  pushup(k);
}

int main(){
  read(T);
  while(T--){
    read2(n,m);
    build(1,n,1);
    while(m--){
      read3(a,b,x);
      updata(a,b,1,x);
    }
    ll ans = 1;
    for(int i = 0 ; i < tree[1].cnt2 ;i ++){
      ans = ans * 2 % mod;
    }
    for(int i = 0 ; i < tree[1].cnt3 ;i ++){
      ans = ans * 3 % mod;
    }
    printf("%lld\n",ans % mod);
  }
}

差分代码

#include<iostream>
#include<cstdio>     //EOF,NULL
#include<cstring>    //memset
#include<cstdlib>    //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath>           //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm>  //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip>             //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h>     //INT_MAX
#include<bitset> // bitset<?> n
using namespace std;

#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> P;
typedef long long LL;
typedef long long ll;
const double eps=1e-8;
const double PI = acos(1.0);
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9+7;
const ll mod = 998244353;
const int MAXN = 1e6+7;
const int maxm = 1;
const int maxn = 100000+10;
int T;
int n,m;
int a,b,x;
int cnt2[maxn],cnt3[maxn];
int tot2[maxn],tot3[maxn];
int main(){
  read(T);
  while(T--){
    memset(cnt2,0);
    memset(cnt3,0);
    memset(tot2,0);
    memset(tot3,0);
    read2(n,m) ;
    while(m--){
      read3(a,b,x);
      if(x == 2) {
        cnt2[a]++,cnt2[b+1]--;
      }
      else {
        cnt3[a]++,cnt3[b+1]--;
      }
    }
    int min2,min3;
    min2 = min3 = inf;
    for(int i = 1; i<= n ;i ++){
        tot2[i] = tot2[i-1] + cnt2[i];
        tot3[i] = tot3[i-1] + cnt3[i];
        min2 = min(min2,tot2[i]);
        min3 = min(min3,tot3[i]);
    }
    ll  ans = 1;
    for(int i = 0 ; i < min2 ;i ++){
      ans = ans * 2 % mod;
    }
    for(int i = 0 ; i < min3 ;i ++){
      ans = ans * 3 % mod;
    }
    cout << ans <<endl;
  }
}

Master of GCD 【线段树区间更新 || 差分】的更多相关文章

  1. upc组队赛2 Master of GCD 【线段树区间更新 || 差分】

    Master of GCD 题目描述 Hakase has n numbers in a line. At fi rst, they are all equal to 1. Besides, Haka ...

  2. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  3. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  4. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  5. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  6. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  7. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  8. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  9. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

随机推荐

  1. [5]windows内核情景分析---进程线程

    本篇主要讲述进程的启动过程.线程的调度与切换.进程挂靠 进程的启动过程: BOOL CreateProcess ( LPCTSTR lpApplicationName,                 ...

  2. ArrayList与List性能测试

    理论:由于ArrayList存储数据存在装箱(读取数据存在拆箱),而泛型List<T>直接对T类型数据进行存储,不存在装箱与拆箱拆箱操作,理论上速度应该快一些. 废话少说,上代码. pub ...

  3. First Wainberg-2018-Deep learning in biomedicine Experience

    ppt+paper 链接:https://pan.baidu.com/s/14toqjcSJti5ZXT3ff4rwIA 提取码:xgkt

  4. python XML文件解析:用xml.dom.minidom来解析xml文件

    python解析XML常见的有三种方法: 一是xml.dom.*模块,是W3C DOM API的实现,若需要处理DOM API则该模块很合适, 二是xml.sax.*模块,它是SAX API的实现,这 ...

  5. 华为手机安装 charles 证书

  6. redis安装集群的2种方式

    redis主从只是数据的备份,当主宕机后不会自动切换从为主,需要手动切换从为主. 哨兵就可以自动切换从为主, 当主数据库遇到异常中断服务后,开发者可以通过手动的方式选择一个从数据库来升格为主数据库,以 ...

  7. python smtplib 发送邮件简单介绍

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式python的smtplib提供了一种很 ...

  8. 大数据学习路线:Zookeeper集群管理与选举

    大数据技术的学习,逐渐成为很多程序员的必修课,因为趋势也是因为自己的职业生涯.在各个技术社区分享交流成为很多人学习的方式,今天很荣幸给我们分享一些大数据基础知识,大家可以一起学习! 1.集群机器监控 ...

  9. 清明 DAY 1

    数学基础   Part 1.  高精度计算     Part 2.  模意义下的运算                     mod  对一个数取模,其实就是取余数   注意: •   无除法运算 • ...

  10. 已知宽高和未知宽高的div块的水平垂直居中

    //已知宽高的情况 .div1_container{     border:1px solid #00ee00;     height:300px;     position:relative; } ...