hdu 4630 No Pain No Game 线段树离线处理
求出一个区间内任意两个数的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 线段树离线处理的更多相关文章
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...
- HDU 4630 No Pain No Game (线段树+离线)
题目大意:给你一个无序的1~n的排列a,每次询问[l,r]之间任取两个数得到的最大gcd是多少 先对所有询问离线,然后把问题挂在区间的左端点上(右端点也行) 在预处理完质数,再处理一个next数组,表 ...
- hdu 4630 No Pain No Game(线段树+离线操作)
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- E - No Pain No Game 线段树 离线处理 区间排序
E - No Pain No Game HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- 线段树+离线 hdu5654 xiaoxin and his watermelon candy
传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...
随机推荐
- notepad++中的zencoding的快捷键修改[转]
在notepad++自己的”设置-->管理快捷键“中,找不到zen coding的快捷键,我又不想改掉已经用习惯了的ctrl+/,结果就用了一种比较偏门的修改快捷键的解决方案,希望可以帮到有同样 ...
- 20151225--easyUI
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- PHP 提交checkbox表单时 判断复选框是否被选中
function GetTitleImgPath(){ $titleImgPath = ""; if (isset($_POST["titlecheckbox" ...
- Windows 去掉启动时的放大镜
控制面板-轻松访问中心-使计算机更易于显示不勾选 启用放大镜
- background:url 的使用方法
#pingfen li{ width:27px; float:left; height:28px; cursor:pointer; background:url( ; list-style:none; ...
- echarts 地图与时间轴混搭
//常量定义public class Constant { public static Integer PM_YEAR_NO = 5; } //action public class ZhiBiaoP ...
- 【翻译】MVC Music Store 教程-概述(三)
Controller 与传统的Web框架,将传入的URL通常映射到磁盘上的文件.例如:一个URL请求“/Products.aspx" 或"/Products.php”是处理一个Pr ...
- C# 常用函数和方法集
1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System. ...
- MiniSD卡是什么
Mini SD卡比目前主流的普通SD卡(如DC或DV上使用的SD卡),在外形上更加小巧,重量仅有3克左右,体积只有21.5x20x1.4mm,比普通SD卡足足节省了60%的空间.别小看这么小的外形,它 ...
- SQLServer2012 和 MariaDB 10.0.3 分页效率的对比
1. 实验环境 R910服务器, 16G内存 SqlServer 2012 64bit MariaDB 10.0.3 64bit (InnoDB) 2. 实验表情况 rtlBill ...