P1890 gcd区间(线段树)

传送门

线段树裸题

注意CSP考场上不要使用以下划线开头的函数和命名空间

code:

#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1100
#define Mid ((l+r)>>1)
#define lson rt<<1,l,Mid
#define rson rt<<1|1,Mid+1,r
using namespace std;
inline void read(int &x)
{
    x=0;
    int p=1;
    char c=getchar();
    while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<1)+(x<<3)+(c^'0');c=getchar();}
    x*=p;
}
int n,m;
int a[N<<2];
void build(int rt,int l,int r)
{
    if(l==r)
    {
        read(a[rt]);
        return;
    }
    else
    {
        build(lson);
        build(rson);
        a[rt]=__gcd(a[rt<<1],a[rt<<1|1]);
    }
}
int query(int rt,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        return a[rt];
    }
    else
    {
        int tmp=0;
        if(L<=Mid)tmp=query(lson,L,R);
        if(R>Mid)tmp=__gcd(tmp,query(rson,L,R));
        return tmp;
    }
}
int main()
{
    read(n);read(m);
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        read(x);read(y);
        printf("%d\n",query(1,1,n,x,y));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值