2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)
题目链接
http://acm.split.hdu.edu.cn/showproblem.php?pid=5869
Given an array a of N positive integers a1,a2,⋯aN−1,aN; a subarray of a is defined as a continuous interval between a1 and aN. In other words, ai,ai+1,⋯,aj−1,aj is a subarray of a, for 1≤i≤j≤N. For a query in the form (L,R), tell the number of different GCDs contributed by all subarrays of the interval [L,R].
For each test, the first line consists of two integers N and Q, denoting the length of the array and the number of queries, respectively. N positive integers are listed in the second line, followed by Q lines each containing two integers L,R for a query.
You can assume that
1≤N,Q≤100000
1≤ai≤1000000
for(int i=;i<=N;i++)
{
int tot=a[i],pos=i;
for(int j=;j<v[i-].size();j++)
{
int r=__gcd(a[i],v[i-][j].first);
if(tot!=r)
{
v[i].push_back(make_pair(tot,pos));
tot=r; pos=v[i-][j].second;
}
}
v[i].push_back(make_pair(tot,pos));
}
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
int a[];
int c[];
int vis[];
int sum[];
struct Node
{
int l,r;
int id;
}node[];
bool cmp(const Node s1,const Node s2)
{
return s1.r<s2.r;
}
vector<pair<int,int> > v[]; int __gcd(int x,int y)
{
int r=x%y;
x=y;
y=r;
if(r==) return x;
return __gcd(x,y);
}
int Lowbit(int t)
{
return t&(t^(t-));
}
int Sum(int x)
{
int sum = ;
while(x > )
{
sum += c[x];
x -= Lowbit(x);
}
return sum;
}
void add(int li,int t)
{
while(li<=)
{
c[li]+=t;
li=li+Lowbit(li);
}
}
int main()
{
int N,Q;
while(scanf("%d%d",&N,&Q)!=EOF)
{
for(int i=;i<=N;i++) scanf("%d",&a[i]);
for(int i=;i<=N;i++)
{
int tot=a[i],pos=i;
for(int j=;j<v[i-].size();j++)
{
int r=__gcd(a[i],v[i-][j].first);
if(tot!=r)
{
v[i].push_back(make_pair(tot,pos));
tot=r; pos=v[i-][j].second;
}
}
v[i].push_back(make_pair(tot,pos));
} for(int i=;i<Q;i++)
scanf("%d%d",&node[i].l,&node[i].r),node[i].id=i;
sort(node,node+Q,cmp);
memset(c,,sizeof(c));
memset(vis,,sizeof(vis));
int len=;
for(int i=;i<=N;i++)
{
for(int j=;j<v[i].size();j++)
{
int s1=v[i][j].first;
int s2=v[i][j].second;
if(vis[s1]){
add(vis[s1],-);
}
vis[s1]=s2;
add(s2,);
}
while(node[len].r==i)
{
sum[node[len].id]=Sum(i)-Sum(node[len].l-);
len++;
}
}
for(int i=;i<Q;i++)
printf("%d\n",sum[i]);
for(int i=;i<=N;i++)
v[i].clear();
}
return ;
}
2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)的更多相关文章
- HDU 5869 Different GCD Subarray Query rmq+离线+数状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...
- 区间gcd问题 HDU 5869 离线+树状数组
题目大意:长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种. 子段就是表示是要连续的a[] 思路:固定右端点,预处理出所有的gcd,每次都和i-1的gcd比较,然后 ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...
- 2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树
I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...
- 沈阳网络赛J-Ka Chang【分块】【树状数组】【dfs序】
Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, ...
- 北邮校赛 F. Gabriel's Pocket Money(树状数组)
F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...
- 【HDU4947】GCD Array(莫比乌斯反演+树状数组)
点此看题面 大致题意: 一个长度为\(n\)的数组,实现两种操作:将满足\(gcd(i,k)=d\)的\(a_i\)加上\(v\),询问\(\sum_{i=1}^xa_i\). 对于修改操作的推式子 ...
- 2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)
https://nanti.jisuanke.com/t/41422 题目大意: 给出n和b,求1到n,各数在b进制下各位数之和的总和. 直接暴力模拟,TLE.. 没想到是要打表...还是太菜了. # ...
随机推荐
- 知方可补不足~Sqlserver中的几把锁和.net中的事务级别
回到目录 当数据表被事务锁定后,我们再进行select查询时,需要为with(锁选项)来查询信息,如果不加,select将会被阻塞,直到锁被释放,下面介绍几种SQL的锁选项 SQL的几把锁 NOLOC ...
- 品味FastDFS~第三回 项目中的FastDFS
回到目录 包括京东,淘宝在内的几个互联网老大,在实现分布式文件存储这块都使用了FastDFS,它是一个轻量级的东西,安装与使用都很方便,服务器间通过socket进行数据通讯,无论在安全和效率上都是可以 ...
- lua的io操作文档
2014-09-16~15:26:35 I/O库提供两种不同的方式进行文件处理1.io表调用方式:使用io表,io.open将返回指定文件的描述,并且所有的操作将围绕这个文件描述 io表同样提供三种预 ...
- angularjs with node service
- Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...
- ASP.NET MVC删除数据
Index.cshtml @model IEnumerable<MvcExample.Models.Category> <script type="text/javascr ...
- 【.net】从比较两个字节数组谈起
上午,有位初学者朋友问:如何比较两个字节数组中各字节是否相等? 不许笑,我一向反对嘲笑初学者,初学者不认真学习时你可以批评,但不能讥嘲.你不妨想想,你自己开始学习编程的时候又是什么个光景? 好,于是, ...
- 浏览器端获取局域网IP地址,本机的MAC,以及机器名
原文链接:http://www.orlion.ga/59/ 只针对IE且客户端的IE允许AcitiveX运行 code: <html> <head> <title> ...
- PHP的学习--可变变量
可变变量 有时候使用可变变量名是很方便的.就是说,一个变量的变量名可以动态的设置和使用.一个普通的变量通过声明来设置,例如: <?php $a = 'hello'; ?> 一个可变变量获取 ...
- oracle表数据类型number对应java中BIgDecimal转int
oracle中id为number类型,在java获取id时用getBigDecimal 相匹配, 如果想转换成int,重写model中的getInt方法: public Integer getInt( ...