hdu 4630 线段树+离线处理

No Pain No Game

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


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 a 1, a 2, ..., a n.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 a 1, a 2, ..., a n.
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
 


题意是给出50000个区间,求出每个区间任意两个数的gcd中的最大值。

这是比较典型的一类题。这类题是典型的就是直接枚举区间两个端点会直接N^2复杂度爆掉,往往都需要换个角度解决,就是考虑以某个数x为中心,并求出x这个数对答案的贡献范围。

具体到这一题上,一段区间内直接求gcd显然TLE。而每个数a[i]对于答案的贡献在于他的因数,假如的他的因数对最大的gcd值有贡献,那么这个因数之前必然出现过至少次。现在考虑啊a[i]的因数x,假如前面某个数a[j]也有一个因数x, 那么可以知道区间[k,i]  ( 1<=k<=j)的最大gcd至少是x。所以解题思路就是,用线段树维护前面任意位置到当前位置pos的区间最大值,数组a从左往右扫,记录每个因数上一次出现的位置vis[x],每次对于a[pos]的因数x,更新区间[1,vis[x]]最大值。这样就能得到[1,pos]的任意子区间的答案。所以对于题目的询问我们要先存起来,按照右端点排序。


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") //交c++要手动扩栈,不然会tle
#define getc int m=(l+r)>>1, lc=rt<<1, rc=rt<<1|1;
const int maxn=50010;

vector<int> fac[maxn];
int vis[maxn];
int a[maxn];
int maxi[4*maxn],lazy[4*maxn];


Seg q[maxn];
int ans[maxn];
int n;

void update(int rt, int l, int r, int x)
{
    maxi[rt]=max(maxi[rt], x);
    lazy[rt]=max(lazy[rt], x);
}
void push_down(int rt, int l, int r)
{
    getc;
    if(lazy[rt]){
        update(lc, l, m, lazy[rt]);
        update(rc, m+1, r, lazy[rt]);
        lazy[rt]=0;
    }
}

void modify(int rt, int l, int r, int ll, int rr, int x)
{
    if(rr<ll) return;
    if(ll<= l && r<=rr){
        update(rt, l, r);
        return;
    }
    else push_down(rt, l, r);

    getc;
    if(ll<=m)
        modify(lc, l, m ,ll ,rr, x);
    if(rr>m)
        modify(rc, m+1, r, ll, rr, x);
    maxi[rt]=max(maxi[lc], maxi[rc]);
}

int query(int rt, int l, int r, int ll, int rr)
{
    if(ll<=l && r<=rr){
        return maxi[rt];
    }
    else push_down(rt, l, r);

    getc;
    int ret=0;
    if(ll<=m)
        ret=max(ret, query(lc, l, m, ll, rr));
    if(rr>m)
        ret=max(ret, query(rc, m+1,r, ll, rr))
    return ret;
}


int main()
{
    for(int i=1; i<=50000; i++){
        fac[i].push_back(1);
        fac[i].push_back(i);
        for(int j=2; j*j<=i; j++){
            if(i%j==0){
                fac[i].push_back(j);
                if(j*j!=i)
                    fac[i].push_back(i/j);
            }
        }
    }



    int t;
    cin>>t;
    while(t--){
        int Q;
        cin>>n;
        for(int i=1; i<=n; i++)
            scanf("%d", a+i);

        memset(maxi, 0, sizeof(maxi));
        memset(lazy, 0, sizeof(lazy));
        memset(vis, 0, sizeof(vis));
        cin>>Q;
        for(int i=0; i<Q; i++){
            scanf("%d%d", &q[i].l, &q[i].r);
            q[i].p=i;
        }
        sort(q, q+Q);
        int cur=0;
        int pos=1;
        while(pos<=n){
            for(int i=0; i<fac[a[i]].size(); i++){
                int x=fac[a[i]][i];
                modify(1, 1, n, 1, vis[x]));
                vis[x]=pos;
            }

            while(q[cur].r==pos){
                ans[q[cur].p]=query(1, 1, n, q[cur].l, q[cur].r);
                cur++;
            }
            pos++;
        }


        for(int i=0; i<Q; i++)
            printf("%d\n", ans[i]);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值