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=1;i<=N;i++){int tot=a[i],pos=i;for(int j=0;j<v[i-1].size();j++){int r=__gcd(a[i],v[i-1][j].first);if(tot!=r){v[i].push_back(make_pair(tot,pos));tot=r; pos=v[i-1][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[100005]; int c[1000005]; int vis[1000005]; int sum[100005]; struct Node {int l,r;int id; }node[100005]; bool cmp(const Node s1,const Node s2) {return s1.r<s2.r; } vector<pair<int,int> > v[100005];int __gcd(int x,int y) {int r=x%y;x=y;y=r;if(r==0) return x;return __gcd(x,y); } int Lowbit(int t) {return t&(t^(t-1)); } int Sum(int x) {int sum = 0;while(x > 0){sum += c[x];x -= Lowbit(x);}return sum; } void add(int li,int t) {while(li<=1000005){c[li]+=t;li=li+Lowbit(li);} } int main() {int N,Q;while(scanf("%d%d",&N,&Q)!=EOF){for(int i=1;i<=N;i++) scanf("%d",&a[i]);for(int i=1;i<=N;i++){int tot=a[i],pos=i;for(int j=0;j<v[i-1].size();j++){int r=__gcd(a[i],v[i-1][j].first);if(tot!=r){v[i].push_back(make_pair(tot,pos));tot=r; pos=v[i-1][j].second;}}v[i].push_back(make_pair(tot,pos));}for(int i=0;i<Q;i++)scanf("%d%d",&node[i].l,&node[i].r),node[i].id=i;sort(node,node+Q,cmp);memset(c,0,sizeof(c));memset(vis,0,sizeof(vis));int len=0;for(int i=1;i<=N;i++){for(int j=0;j<v[i].size();j++){int s1=v[i][j].first;int s2=v[i][j].second;if(vis[s1]){add(vis[s1],-1);}vis[s1]=s2;add(s2,1);}while(node[len].r==i){sum[node[len].id]=Sum(i)-Sum(node[len].l-1);len++;}}for(int i=0;i<Q;i++)printf("%d\n",sum[i]);for(int i=0;i<=N;i++)v[i].clear();}return 0; }
转载于:https://www.cnblogs.com/chen9510/p/5867587.html
2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)相关推荐
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- 2019南昌网络赛 I. Yukino With Subinterval 树状数组套线段树
I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...
- 2019 南京 网络赛 B (二维偏序,树状数组离线)
题意: 给出一N*N的蛇形矩阵,具体位置元素值不给你,自己找规律,然后给你M个 有效位置,P次查询,每次查询一个子矩阵中有效元素的权值和,该权值和等于对于 每个有效元素,模10拆分后相加得到的和.(注 ...
- 2018蓝桥模拟赛·天上的星星 暴力|二维树状数组
在一个星光摧残的夜晚,蒜头君一颗一颗的数这天上的星星. 蒜头君给在天上巧妙的画了一个直角坐标系,让所有的星星都分布在第一象.天上有 nn 颗星星,他能知道每一颗星星的坐标和亮度. 现在,蒜头君问自己 ...
- 2016 UESTC Training for Data Structures O - 卿学姐种美丽的花 树状数组+等差数列
O - 卿学姐种美丽的花 Time Limit: 8000/4000MS (Java/Others) Memory Limit: 125535/65535KB (Java/Others) Su ...
- HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)...
树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/6 ...
- HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景
http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...
- 【HDU - 5869】Different GCD Subarray Query(思维,数学,gcd,离线处理,查询区间不同数,树状数组 或 二分RMQ)
题干: This is a simple problem. The teacher gives Bob a list of problems about GCD (Greatest Common Di ...
- HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化
题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...
最新文章
- SpringBoot------添加保存时自动编译插件
- ubuntu16.04系统下创建python隔离环境
- 在长沙怎么挑选装饰公司
- System.BadImageFormatException”类型的未经处理的异常在 xx.exe 中发生
- EdgeX Foundry理论篇
- 学习笔记——XSLT转换器的使用(Xalan和Saxon) .(转)
- 彩色图如何转换成灰度
- springBoot ajax 报错 Circular view path [xx: would dispatch...
- Incorrect line ending: found carriage return (\r) without corresponding newline (\n)错误的解决方案...
- php 获取数据库中的信息,php获取数据库中数据的实现方法
- java sql.setInt_java – PreparedStatement的setInt()无法在PostgreSQL上运行
- 【原创】大叔案例分享(4)定位分析--见证scala的强大
- 使用mouse without borders无界键盘鼠标工具实现一套键盘鼠标控制两台电脑(非常的奈斯)
- 优科无线并购Wi-Fi入网软件提供商Cloudpath Networks
- 使用Python实现excel项目清单自动生成word文档
- Jmeter-使用http proxy代理录制脚本
- Arduino超声波模块原理
- FI财务会计全局设置
- VLC模拟TS直播流
- 【C语言】童年的扫雷游戏(递归展开)你也可以做出来,将他发给你的网瘾室友玩吧 ——含详细注释及解析
热门文章
- 百度工程师控制公司服务器“挖矿”:4个月赚10万 判刑3年
- ajax请求get方法的封装,使用jQuery中Ajax的封装函数——$.get()
- Linux solr 启动命令,linux – 重启Solr的正确方法是什么
- TZOJ上的C语言作业答案,C语言编程练习
- 059_arguments.callee和arguments.callee.caller
- phpstudy卸载mysql_PHPstudy卸载和phpstudy卸载详解
- 玩客云如何设置文件存储到副盘_如何存储自己的资料:低成本的小型存储方案...
- 图书商城:购物车模块
- Android应用开发:动画和Fragment
- oracle11g中rman基本使用方法