题目链接

求出一个区间内任意两个数的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-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 5e4+;
int maxx[maxn<<], ans[maxn], a[maxn], b[maxn][], 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] = ;
if(l == r)
return ;
int m = l+r>>;
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>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
maxx[rt] = max(maxx[rt<<], maxx[rt<<|]);
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return maxx[rt];
}
int m = l+r>>, ret = ;
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(, n, );
for(int i = ; i<=n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for(int i = ; 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 = ; i<=n; i++) {
for(int j = i; j<=n; j+=i) {
b[j][num[j]++] = i;
}
}
mem1(pre);
int pos = ;
for(int i = ; i<=n&&pos<m; i++) {
for(int j = ; j<num[a[i]]; j++) {
int tmp = b[a[i]][j];
if(~pre[tmp]) {
update(pre[tmp], tmp, , n, );
}
pre[tmp] = i;
}
while(q[pos].r == i) {
if(q[pos].r == q[pos].l)
ans[q[pos].id] = ;
else
ans[q[pos].id] = query(q[pos].l, q[pos].r, , n, );
pos++;
}
}
for(int i = ; i<m; i++) {
printf("%d\n", ans[i]);
}
}
return ;
}

hdu 4630 No Pain No Game 线段树离线处理的更多相关文章

  1. HDU - 4630 No Pain No Game (线段树 + 离线处理)

    id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...

  2. HDU 4630 No Pain No Game (线段树+离线)

    题目大意:给你一个无序的1~n的排列a,每次询问[l,r]之间任取两个数得到的最大gcd是多少 先对所有询问离线,然后把问题挂在区间的左端点上(右端点也行) 在预处理完质数,再处理一个next数组,表 ...

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

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. E - No Pain No Game 线段树 离线处理 区间排序

    E - No Pain No Game  HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...

  6. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  9. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

随机推荐

  1. JAVA设计原则

      1.单一职责规则(SRP):有且仅有一个原因引起类的变化. 2.依赖倒置规则(DIP): 高层模块不应该依赖底层模块,两者都用依赖接口: 接口不依赖细节: 细节依赖接口. 3.接口隔离规则:类间的 ...

  2. C++ Primer 读书笔记: 第9章 顺序容器

    第9章 顺序容器 引: 顺序容器: vector 支持快速随机访问 list 支持快速插入/删除 deque 双端队列 顺序容器适配器: stack 后进先出栈 queue 先进先出队列 priori ...

  3. zabbix中文配置及乱码问题

    1.切换成中文 2.发现有乱码出现 由于zabbix的web端没有中文字库,所以我们使用windows中的字体. 找到简体字的存储位置——copy到桌面或其他位置——上传到zabbix服务器——web ...

  4. JQuery easyui (3) Resizable(调整大小)组件

    Resizable  动态调整元素大小  不依赖其他组件 Resizable的加载方法 <div class="easyui-resizable"></div&g ...

  5. javascript 学习笔记(权威指南)

    1.数组的sort()方法默认是按照字母排序的,下面举个栗子说明: 1)全是字母: var arr =["zu","fan","an",&q ...

  6. 你的阅读造就了你 You are what you read

    在豆瓣上看到的一篇很有思想和正能量的文章,在这里请允许我用原创的方式来呈现给大家.如果你是在校的大学生或者研究生博士生,这篇文章会让你有很多的共鸣.如果你已真正的踏入这个社会,也将受益匪浅.   电脑 ...

  7. fputcsv 和 fgetcsv

    public function putcsv(){ $list = M("ad")->limit(0,10)->select(); $fp = fopen('./fil ...

  8. [原创]obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用

    原文链接:obj-c编程15[Cocoa实例02]:KVC和KVO的实际运用 我们在第16和第17篇中分别介绍了obj-c的KVC与KVO特性,当时举的例子比较fun,太抽象,貌似和实际不沾边哦.那么 ...

  9. sql查询数据库表中重复记录方法

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people where peopleId in (select peopleId ...

  10. Day4_代码重用与函数

    知识点速记: 重用代码的方法:脚本包含require().include(); 全局配置文件php.ini(auto_prepend_file/auto_append_file); 目录配置文件.ht ...