HDU 4630 No Pain No Game 树状数组+离线操作

题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值。

解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数。那么每次查询[L,R]之间两数gcd的最大值即为查询[L,R]中值最大的线段,离线所有的查询数据,然后按右端点坐标从小到大排序,依次往右加入即可。

这里学到了树状数组维护最大值的写法。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 50007

int c[N];
struct node
{
    int l,r,v;
}a[10*N],Q[N];
int n,pos[N],num[N],ans[N];

int cmp(node ka,node kb) { return ka.r < kb.r; }
int lowbit(int x) { return x&-x; }

void modify(int x,int val)
{
    while(x > 0)
    {
        c[x] = max(c[x],val);
        x -= lowbit(x);
    }
}

int getmax(int x)
{
    int res = 0;
    while(x <= n)
    {
        res = max(res,c[x]);
        x += lowbit(x);
    }
    return res;
}

int main()
{
    int t,i,j,x,q,tot;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            pos[x] = i;
            c[i] = 1;
        }
        tot = 0;
        for(i=2;i<=n/2;i++)
        {
            int k = 0;
            for(j=i;j<=n;j+=i)  //i的倍数
                num[k++] = pos[j];
            sort(num,num+k);
            for(j=1;j<k;j++)
            {
                 a[tot].l = num[j-1];
                 a[tot].r = num[j];
                 a[tot++].v = i;
            }
        }
        scanf("%d",&q);
        for(i=0;i<q;i++)
        {
            scanf("%d%d",&Q[i].l,&Q[i].r);
            Q[i].v = i;
        }
        sort(a,a+tot,cmp);
        sort(Q,Q+q,cmp);
        j = 0;
        for(i=0;i<q;i++)
        {
            if(Q[i].l == Q[i].r)
            {
                ans[Q[i].v] = 0;
                continue;
            }
            while(j < tot && a[j].r <= Q[i].r)
                modify(a[j].l,a[j].v),j++;
            ans[Q[i].v] = getmax(Q[i].l);
        }
        for(i=0;i<q;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

时间: 2024-10-24 22:02:16

HDU 4630 No Pain No Game 树状数组+离线操作的相关文章

hdu 4630 树状数组+离线操作+GCD

http://acm.hdu.edu.cn/showproblem.php?pid=4630 重新认识了树状数组. 首先要记住那个树形的图,然后+或-lowbit(i)是自己根据具体问题设定的,不要死于+或者-, 树状数组的特点: 1.+lowbit(i)可以到达包含结点i的上一层父节点    所以用于值的更改 2.-lowbit(i)可以到达不包含i所代表区间的上一层父节点  所以用于值的求和---每个不相交的段加起来 3.C[i]的含义也是根据具体问题去做设定的,但是c[i]覆盖了a[i-2

hdu 1166:敌兵布阵(树状数组,练习题)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37773    Accepted Submission(s): 15923 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

HDU 1892 See you~ (二维树状数组)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 See you~ Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say so

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 481    Accepted Submission(s): 245 Problem Description You were driving along a highway when you got caught by the road p

hdu 5869 区间不同GCD个数(树状数组)

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 58 Problem Description This is a simple problem. The teacher gives Bob a list of probl

hdu 1556 Color the ball(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气

HDU 2852 KiKi&#39;s K-Number【 树状数组 二分 】

题意:给出m个操作,0:是增加一个数,add(x,1)1:是删除一个指定的数,这个是看sum(x) - sum(x-1)是否为0,为0的话则不存在,不为0的话,则add(x,-1)2:是查询比x大的数中第k大的数,先求出比x小的个数s,假设比x大的数中第k大的数为y,那么比y小的个数有s+k个二分y的值来找 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath&g