HDU 4630 No Pain No Game(树状数组)
看的别人的题解,离线之后,按r排序,枚举1-n,利用pre[j],存上次j的倍数出现的位置,树状数组里统计的当前位置到最后的最大值,树状数组是求区间最值其实应该很麻烦的,但是此题用法只是求到最后的最大值,插入的时候,往前更新就好了,类似求和。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num[];
int p[];
int n;
struct node
{
int l,r,id;
} que[];
bool cmp(node a,node b)
{
return a.r < b.r;
}
bool cmp1(node a,node b)
{
return a.id < b.id;
}
int pre[];
int ans[];
int lowbit(int t)
{
return t&(-t);
}
void insert(int t,int d)
{
while(t)
{
p[t] = max(p[t],d);
t -= lowbit(t);
}
}
int getmax(int t)
{
int maxz = ;
while(t <= n)
{
maxz = max(maxz,p[t]);
t += lowbit(t);
}
return maxz;
}
int main()
{
int i,j,t,m,tnum;
scanf("%d",&t);
while(t--)
{
memset(p,,sizeof(p));
memset(pre,,sizeof(pre));
scanf("%d",&n);
for(i = ; i <= n; i ++)
scanf("%d",&num[i]);
scanf("%d",&m);
for(i = ; i < m; i ++)
{
scanf("%d%d",&que[i].l,&que[i].r);
que[i].id = i;
}
sort(que,que+m,cmp);
tnum = ;
for(i = ; i <= n; i ++)
{
if(tnum == m) break;
for(j = ; j*j <= num[i]; j ++)
{
if(num[i]%j == )
{
if(pre[j] != )
insert(pre[j],j);
pre[j] = i;
if (j * j== num[i]) continue ;
if(pre[num[i]/j] != )
insert(pre[num[i]/j],num[i]/j);
pre[num[i]/j] = i;
}
}
while(tnum < m&&que[tnum].r == i)
{
ans[que[tnum].id] = getmax(que[tnum].l);
tnum ++;
}
}
sort(que,que+m,cmp1);
for(i = ;i < m;i ++)
{
if(que[i].l == que[i].r)
printf("0\n");
else
printf("%d\n",ans[i]);
}
}
return ;
}
HDU 4630 No Pain No Game(树状数组)的更多相关文章
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- 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) ...
随机推荐
- map遍历
Set<Map.Entry<String,String>> ss = params.entrySet(); for(Map.Entry<String,String> ...
- 查看一些特定sql需求的书写
user表,5个人abcde, content表10篇文章,一个人对应两篇文章,有 time字段,查询出五个人的最新文章. select a.id,a.SName,a.ClsNo,a.Scorefr ...
- spring事物传播属性
PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists. 支持一个当前事务;如果不存在 ...
- Mysql数据库中设置root密码的命令及方法
我们都知道通常PHP连接 Mysql都是通过root用户名和密码连接,默认情况下在Mysql安装时root初始密码为空,在安装使用PHP开源系统时,都需要填写连接Mysql数据库的用户名和密码,此时当 ...
- for循环,pydev提示未使用的变量,解决办法
对于如下代码,pvdev会产生未使用变量的警告 for i in range(5): func() 解决办法: 把变量替换成下划线_,就不会生产告警了.改变后如下: for _ in range(5) ...
- 排序稳定性stable
stable排序 O(n^2): InsertionSort,BubbleSort O(nlgn): MergeSort O(n+k): CountSort, RadixSort,BucketSort ...
- Java for LeetCode 039 Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- MFC dfs遍历文件
//如果涉及到大文件的遍历(大于4GB),可以将以下代码_finddata_t换成__finddata64_t,_findfirst换成_findfirst64,_findnext换成_findnex ...
- vector data() [c++11]
Example 12345678910111213141516171819202122 // vector::data #include <iostream> #include <v ...
- C# Window Form播放音乐的4种方式
C#播放背景音乐通常有四种方式: 1.播放系统事件声音 2.使用System.Media.SoundPlayer播放wav------------------------仅仅是对波形音乐 3.使用MC ...