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],问区间里有多少个三元组满足 ...
随机推荐
- WinForm DataGridView看似刷新的问题
昨天同事winform遇到一个问题, 窗体上有一个时间控件,和一堆文本,下拉控件,时间控件是每秒都在动态走的 窗体下发一个DataGridView 控件显示保存后的数据 保存的数据库是在另一台机器B上 ...
- 详解VB.net编写DLL(动态链接库、应用程序扩展)文件
首先,我们启动VS(Visual-Studio简称),我使用的是VS2008版本. 新建一个项目-选择内裤(额...不好意思)→类库 ,名称就默认吧. 编写类库没有窗体设计,因此我们不能使用工具箱中的 ...
- CentOS6.4中安装Python-Pip 以及Phyton gevent
一.安装Phyton-pip 首先要安装 Setuptools wget --no-check-certificate https://pypi.python.org/packages/2.6/s/s ...
- leetcode Binary Tree Right Side View python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- iOS定位与地图
定位: 手机上定位的实现主要有三种方式:基站(附近基站的位置),wifi(所连接路由器的位置),卫星(最准确,也最耗能). iOS的定位功能主要是由CLLocationManager类来完成的.这个类 ...
- ecshop开发日志之手机端虚拟商品自动发货
在ecshop官方模版收,web端的虚拟商品购买后不能像pc端那般直接在付款后出现虚拟商品的卡号,密码,截止日期一下为让手机购买也可以在付款后自动显示发货并能显示卡号密码截止日期首 先找到pc端的fl ...
- echarts的使用总结;
题外话:好久没来博客园了,这几个月自己的工作经历可以算是相当丰富,其实一直不知道自己做web前端能做到什么时候,但是想说既然现在还在做着这个职位,就好好的学习.之前很少写js代码,来了新公司大多数都是 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)
目的: 1. 通过网页读取watchdog的信息 2. 通过网页设置watchdog 准备工作: 1. 选择一个web框架,选用 cherrypy $ sudo apt-get install pyt ...
- CF 293 E Close Vertices (树的分治+树状数组)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题目:给出一棵树,问有多少条路径权值和不大于w,长 ...
- python-操作exel(xlrd,xlwt)
1.使用第三方库 python中处理excel表格,常用的库有xlrd(读excel)表.xlwt(写excel)表.openpyxl(可读写excel表)等. xlrd读数据较大的excel表时效率 ...