码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4630 No Pain No Game 线段树离线处理

时间:2016-03-04 16:00:10      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

 

求出一个区间内任意两个数的gcd的最大值。

 

先将询问读进来然后按r值排序。 将每一个数因数分解, 对每一个因子x, 如果pre[x]!=-1, 那么就更新update(pre[x], x, 1, n, 1), 当前的坐标i和某一个询问的r相同时进行查询。

具体看代码。注意l和r相同时答案是0。 初始的时候将maxx数组都弄成1.

 

谜之WA了n次才发现是数组开小了。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 5e4+5;
int maxx[maxn<<2], ans[maxn], a[maxn], b[maxn][100], num[maxn], pre[maxn], c[maxn];
struct node
{
    int l, r, id;
    bool operator < (node a) const
    {
        return r<a.r;
    }
}q[maxn];
void build(int l, int r, int rt) {
    maxx[rt] = 1;
    if(l == r)
        return ;
    int m = l+r>>1;
    build(lson);
    build(rson);
}
void update(int p, int val, int l, int r, int rt) {
    if(l == r) {
        maxx[rt] = max(val, maxx[rt]);
        return ;
    }
    int m = l+r>>1;
    if(p<=m)
        update(p, val, lson);
    else
        update(p, val, rson);
    maxx[rt] = max(maxx[rt<<1], maxx[rt<<1|1]);
}
int query(int L, int R, int l, int r, int rt) {
    if(L<=l&&R>=r) {
        return maxx[rt];
    }
    int m = l+r>>1, ret = 0;
    if(L<=m)
        ret = max(ret, query(L, R, lson));
    if(R>m)
        ret = max(ret, query(L, R, rson));
    return ret;
}
int main()
{
    int t, n, m;
    cin>>t;
    while(t--) {
        mem(maxx);
        scanf("%d", &n);
        build(1, n, 1);
        for(int i = 1; i<=n; i++)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        for(int i = 0; i<m; i++) {
            scanf("%d%d", &q[i].l, &q[i].r);
            q[i].id = i;
        }
        sort(q, q+m);
        mem(num);
        mem(b);
        for(int i = 1; i<=n; i++) {
            for(int j = i; j<=n; j+=i) {
                b[j][num[j]++] = i;
            }
        }
        mem1(pre);
        int pos = 0;
        for(int i = 1; i<=n&&pos<m; i++) {
            for(int j = 0; j<num[a[i]]; j++) {
                int tmp = b[a[i]][j];
                if(~pre[tmp]) {
                    update(pre[tmp], tmp, 1, n, 1);
                }
                pre[tmp] = i;
            }
            while(q[pos].r == i) {
                if(q[pos].r == q[pos].l)
                    ans[q[pos].id] = 0;
                else
                    ans[q[pos].id] = query(q[pos].l, q[pos].r, 1, n, 1);
                pos++;
            }
        }
        for(int i = 0; i<m; i++) {
            printf("%d\n", ans[i]);
        }
    }
    return 0;
}

 

hdu 4630 No Pain No Game 线段树离线处理

标签:

原文地址:http://www.cnblogs.com/yohaha/p/5242182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!