HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值。
解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数。那么每次查询[L,R]之间两数gcd的最大值即为查询[L,R]中值最大的线段,离线所有的查询数据,然后按右端点坐标从小到大排序,依次往右加入即可。
这里学到了树状数组维护最大值的写法。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 50007 int c[N];
struct node
{
int l,r,v;
}a[*N],Q[N];
int n,pos[N],num[N],ans[N]; int cmp(node ka,node kb) { return ka.r < kb.r; }
int lowbit(int x) { return x&-x; } void modify(int x,int val)
{
while(x > )
{
c[x] = max(c[x],val);
x -= lowbit(x);
}
} int getmax(int x)
{
int res = ;
while(x <= n)
{
res = max(res,c[x]);
x += lowbit(x);
}
return res;
} int main()
{
int t,i,j,x,q,tot;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&x);
pos[x] = i;
c[i] = ;
}
tot = ;
for(i=;i<=n/;i++)
{
int k = ;
for(j=i;j<=n;j+=i) //i的倍数
num[k++] = pos[j];
sort(num,num+k);
for(j=;j<k;j++)
{
a[tot].l = num[j-];
a[tot].r = num[j];
a[tot++].v = i;
}
}
scanf("%d",&q);
for(i=;i<q;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].v = i;
}
sort(a,a+tot,cmp);
sort(Q,Q+q,cmp);
j = ;
for(i=;i<q;i++)
{
if(Q[i].l == Q[i].r)
{
ans[Q[i].v] = ;
continue;
}
while(j < tot && a[j].r <= Q[i].r)
modify(a[j].l,a[j].v),j++;
ans[Q[i].v] = getmax(Q[i].l);
}
for(i=;i<q;i++)
printf("%d\n",ans[i]);
}
return ;
}
HDU 4630 No Pain No Game 树状数组+离线操作的更多相关文章
- HDU 4630 No Pain No Game(树状数组)
题目链接 看的别人的题解,离线之后,按r排序,枚举1-n,利用pre[j],存上次j的倍数出现的位置,树状数组里统计的当前位置到最后的最大值,树状数组是求区间最值其实应该很麻烦的,但是此题用法只是求到 ...
- HDU 4630 No Pain No Game 树状数组+离线查询
思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...
- 4630 no pain no game 树状数组
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你N个数,然后给你M个询问,每个询问包含一个l 一个r,问你lr 这个区间中任意两个数最 ...
- hdu 5869 区间不同GCD个数(树状数组)
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
- hdu 1394 Minimum Inversion Number(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- HDU 5773 The All-purpose Zero(树状数组)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5773 [题目大意] 给出一个非负整数序列,其中的0可以替换成任意整数,问替换后的最长严格上升序列长 ...
- POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
Ping pong Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- 【特别推荐】几款极好的 JavaScript 下拉列表插件
表单元素让人爱恨交加.作为网页最重要的组成部分,表单几乎无处不在,从简单的邮件订阅.登陆注册到复杂的需要多页填写的信息提交功能,表单都让开发者花费了大量的时间和精力去处理,以期实现好用又漂亮的表单功能 ...
- eclipse提示:This tag and its children can be replaced by one <TextView/> and a compound drawable
今天在学习android开发的时候,写了这样的一段代码: <?xml version="1.0" encoding="utf-8"?> <Li ...
- 闭包和this
一.闭包 最开始理解闭包是在一个函数内部定义一个函数,可以在外面的环境里进行调用.现在对于闭包的理解是利用函数来保存作用域内的对象. 理解闭包首先要理解执行上下文,变量对象,活动对象,作用域链.因为执 ...
- 学习笔记——关于HTML(含HTML5)的块级元素和行级(内联)元素总结
1.首先我们要知道什么是块级元素和行级(内联)元素? 块级(block)元素的特点: ①总是在新行上开始: ②高度,行高以及外边距和内边距都可控制: ③宽度缺省是它的容器的100%,除非设定一个宽度: ...
- LayoutInflater的infalte()
其中: resource:是布局文件ID root:是父ViewGroup对象, attachToRoot:是是否将“翻译”出来的View添加到上面的root中 root和attachToRoot是共 ...
- Object-c字符串操作
字符串操作: -(void) testString{ NSString *str1 = @"some string"; NSLog(@"%@", str1); ...
- iOS 关于手势
不同视图不能同时添加相同手势!
- iOS---UISearchBar限制输入字数
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText // called when text ...
- response设置输出文件编码
在java后台的Action代码或者Servlet代码中用response的方法来设置输出内容的编码方式,有以下三个方法: 1.response.setCharacterEncoding(" ...
- nodejs events模块
var EventEmitter = require('events').EventEmitter; var emitter = new EventEmitter(); console.log(emi ...