• hdu 4630 No Pain No Game(线段树+离线操作)


     

    No Pain No Game

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1769    Accepted Submission(s): 748


    Problem Description
    Life is a game,and you lose it,so you suicide.
    But you can not kill yourself before you solve this problem:
    Given you a sequence of number a1, a2, ..., an.They are also a permutation of 1...n.
    You need to answer some queries,each with the following format:
    If we chose two number a,b (shouldn't be the same) from interval [l, r],what is the maximum gcd(a, b)? If there's no way to choose two distinct number(l=r) then the answer is zero.
     

    Input
    First line contains a number T(T <= 5),denote the number of test cases.
    Then follow T test cases.
    For each test cases,the first line contains a number n(1 <= n <= 50000).
    The second line contains n number a1, a2, ..., an.
    The third line contains a number Q(1 <= Q <= 50000) denoting the number of queries.
    Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n),denote a query.
     

    Output
    For each test cases,for each query print the answer in one line.
     

    Sample Input
    1 10 8 2 4 9 5 7 10 6 1 3 5 2 10 2 4 6 9 1 4 7 10
     

    Sample Output
    5 2 2 4 3
     

    Author
    WJMZBMR
     

    Source
     

    Recommend
    zhuyuanchen520   |   We have carefully selected several similar problems for you:  5283 5282 5280 5279 5278 
     

    题意: 有N个数。 是 1~N的一个排列。有M个询问, 每次询问一个区间, 问从这个区间中,取两个数的最大的最大公约数。

    题解:先把查询按右区间升序排序。在将数组按顺序插入,记录当前这个数的因子出现的位置,假设之前有出现则代表这两个因子出现的

             位置之间有两个数的公共约数是它,用线段树维护区间约数最大值就可以。

    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<vector>
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define N 50050
    #define lc idx<<1
    #define rc idx<<1|1
    
    using namespace std;
    
    int n,q,flag;
    int a[N],tree[N*4];
    int L[N],R[N];
    int first[N],ans[N];
    vector<int>vec;
    
    struct node {
        int id;
        int l,r;
    } Q[N];
    
    bool cmp(node a,node b) {
        if(a.r==b.r)
            return a.l<b.l;
        return a.r<b.r;
    }
    
    ///求全部因子
    void FJ(int x) {
        vec.clear();
        for(int i=1; i*i<=x; i++) {
            if(x%i==0) {
                vec.push_back(i);
                if(x/i!=i)
                    vec.push_back(x/i);
            }
        }
    }
    
    void push_up(int idx) {
        tree[idx]=max(tree[lc],tree[rc]);
    }
    
    void build(int idx,int l,int r) {
        tree[idx]=0;
        if(l==r) {
            return;
        }
        int mid=(l+r)>>1;
        build(lson);
        build(rson);
    }
    
    void update(int idx,int l,int r,int x,int v) { //x处的值改为v
        if(l==r) {
            if(tree[idx]<v)
                tree[idx]=v;
            return;
        }
        int mid=(l+r)>>1;
        if(x<=mid)update(lson,x,v);
        else      update(rson,x,v);
        push_up(idx);
    }
    
    int query(int idx,int l,int r,int x,int y) {
        if(l>=x&&y>=r) {
            return tree[idx];
        }
        int ans=0;
        int mid=(l+r)>>1;
        if(x<=mid) {
            ans=max(ans,query(lson,x,y));
        }
        if(y>mid) {
            ans=max(ans,query(rson,x,y));
        }
        return ans;
    }
    
    void debug() {
        for(int i=0; i<vec.size(); i++) {
            printf("%d ",vec[i]);
        }
        cout<<endl;
    }
    
    int main() {
        //freopen("test.in","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--) {
            scanf("%d",&n);
            for(int i=1; i<=n; i++) {
                scanf("%d",&a[i]);
            }
            scanf("%d",&q);
            for(int i=1; i<=q; i++) {
                scanf("%d%d",&Q[i].l,&Q[i].r);
                Q[i].id=i;
            }
            sort(Q+1,Q+1+q,cmp);
            memset(first,0,sizeof first);
            memset(L,0,sizeof L);
            memset(R,0,sizeof R);
            build(1,1,n);
            ///预处理同样有区间的左右区间
            int f=1;
            L[Q[f].r]=f;
            R[Q[f].r]=f;
            for(int i=1; i<=q;) {
                while(Q[i].r==Q[f].r&&i<=q) {
                    i++;
                }
                L[Q[f].r]=f;
                R[Q[f].r]=i-1;
                f=i;
            }
            for(int i=1; i<=n; i++) {
                //FJ(a[i]);
                //debug();
                int xx=a[i];
                for(int k=1; k*k<=xx; k++) {
                    if(xx%k==0) {
                        if(!first[k]) {
                            first[k]=i;
                        } else {
                            update(1,1,n,first[k],k);
                            first[k]=i;
                        }
                        int kk=xx/k;
                        if(k!=kk) {
                            if(!first[kk]) {
                                first[kk]=i;
                            } else {
                                update(1,1,n,first[kk],kk);
                                first[kk]=i;
                            }
                        }
                    }
                }
                int x=L[i],y=R[i];
                if(x==0||y==0)continue;
                for(int j=x; j<=y; j++) {
                    int k=Q[j].l;
                    if(k==i) {
                        ans[Q[j].id]=0;
                    } else {
                        ans[Q[j].id]=query(1,1,n,k,i);
                    }
                }
                if(y==q)break;
            }
            for(int i=1; i<=q; i++) {
                printf("%d
    ",ans[i]);
            }
        }
        return 0;
    }


  • 相关阅读:
    小菜读书---《程序员修炼之道–从小工到专家》
    小菜读书--《大话设计模式》
    darknet-mini:带注释的darknet简化版,助你深入理解YOLO
    如何使用VLC进行视频录像
    海康相机打开的方法
    YOLOv5训练自己的数据集(超详细完整版)
    用GANs来做数据增强
    torch ----------->>>>rknn
    Linux下的tar压缩解压缩命令详解
    小缺陷目标检测网络--PCB缺陷检测—TDD-net
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6813232.html
Copyright © 2020-2023  润新知