Hdu5381-The sum of gcd(莫队)
- #include<cstdio>
- #include<vector>
- #include<cstring>
- #include<string>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- const int maxn=;
- typedef __int64 LL;
- int N,M,A[maxn],block;
- struct Ques
- {
- int x,y,id;
- Ques(int x=,int y=,int id=):x(x),y(y),id(id){}
- bool operator < (const Ques& t) const
- {
- int a=x/block,b=t.x/block; //排序
- if(a!=b) return a<b;
- return y<t.y;
- }
- }ques[maxn];
- int gcd(int a,int b){ return b==?a:gcd(b,a%b); }
- struct node
- {
- int id,g;
- node(int id=,int g=):id(id),g(g){}
- };
- vector<node> vl[maxn],vr[maxn];
- void GetLe()
- {
- for(int i=;i<=N;i++) //得到以i为右端点连续区间的gcd值
- {
- if(i==){ vl[i].push_back(node(i,A[i])); continue; }
- int g=A[i],id=i;
- int Size=vl[i-].size();
- for(int j=;j<Size;j++)
- {
- node& t=vl[i-][j];
- int ng=gcd(t.g,g);
- if(ng!=g) vl[i].push_back(node(id,g));
- g=ng; id=t.id;
- }
- vl[i].push_back(node(id,g));
- }
- }
- void GetRi() //同理
- {
- for(int i=N;i>=;i--)
- {
- if(i==N){ vr[i].push_back(node(i,A[i])); continue; }
- int g=A[i],id=i;
- int Size=vr[i+].size();
- for(int j=;j<Size;j++)
- {
- node& t=vr[i+][j];
- int ng=gcd(t.g,g);
- if(ng!=g) vr[i].push_back(node(id,g));
- g=ng; id=t.id;
- }
- vr[i].push_back(node(id,g));
- }
- }
- LL WorkLe(int x,int y)
- {
- int Size=vl[y].size();
- int ny=y;
- LL ret=;
- for(int i=;i<Size;i++)
- {
- node& t=vl[y][i];
- if(t.id>=x)
- {
- ret+=(LL)t.g*(ny-t.id+);
- ny=t.id-; //跳过去
- }
- else{ ret+=(LL)t.g*(ny-x+); break; }
- }
- return ret;
- }
- LL WorkRi(int x,int y)
- {
- int nx=x;
- LL ret=;
- int Size=vr[x].size();
- for(int i=;i<Size;i++)
- {
- node& t=vr[x][i];
- if(t.id<=y)
- {
- ret+=(LL)t.g*(t.id-nx+);
- nx=t.id+;
- }
- else { ret+=(LL)t.g*(y-nx+); break; }
- }
- return ret;
- }
- LL ans[maxn];
- void solve()
- {
- for(int i=;i<=N;i++) vl[i].clear(),vr[i].clear();
- block=(int)sqrt(N+0.5); //分块
- sort(ques,ques+M); //排序
- GetLe(); //得到左边连续相同的gcd区间
- GetRi(); //得到右边连续相同的gcd区间
- int x=,y=;
- LL ret=;
- for(int i=;i<M;i++) //莫队的主要实现部分
- {
- Ques& q=ques[i];
- while(y<q.y){ y++; ret+=WorkLe(x,y); }
- while(y>q.y){ ret-=WorkLe(x,y); y--; }
- while(x<q.x){ ret-=WorkRi(x,y); x++; }
- while(x>q.x){ x--; ret+=WorkRi(x,y); }
- ans[q.id]=ret; //保存答案
- }
- for(int i=;i<M;i++) printf("%lld\n",ans[i]); //输出
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d",&N);
- for(int i=;i<=N;i++) scanf("%d",&A[i]);//输入
- scanf("%d",&M);
- int x,y;
- for(int i=;i<M;i++)
- {
- scanf("%d%d",&x,&y);
- ques[i]=Ques(x,y,i); //离线保存查询
- }
- solve();
- }
- return ;
- }
Hdu5381-The sum of gcd(莫队)的更多相关文章
- hdu5381 The sum of gcd]莫队算法
题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...
- hdu 5381 The sum of gcd 莫队+预处理
The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- hdu 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
- HDU-4676 Sum Of Gcd 莫队+欧拉函数
题意:给定一个11~nn的全排列AA,若干个询问,每次询问给出一个区间[l,r][l,r],要求得出∑l≤i<j≤r gcd(Ai,Aj)的值. 解法:这题似乎做的人不是很多,蒟蒻当然不会做只 ...
- hdu 4676 Sum Of Gcd 莫队+数论
题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...
- HDOJ 5381 The sum of gcd 莫队算法
大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...
- hdu5381 The sum of gcd
莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n)). 代码 #i ...
- [CSP-S模拟测试]:sum(数学+莫队)
题目传送门(内部题63) 输入格式 第一行有一个整数$id$,表示测试点编号.第一行有一个整数$q$,表示询问组数.然后有$q$行,每行有两个整数$n_i,m_i$. 输出格式 一共有$q$行,每行一 ...
- HDU 4676 Sum Of Gcd 【莫队 + 欧拉】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...
随机推荐
- x2engine
x2engine 各版本下载 https://bitnami.com/stack/x2crm/installer https://bitnami.com/redirect/to/36211/bitna ...
- ext3中xtype属性汇总
基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...
- J2EE学习路线
第一部分: JAVA语言基础知识.包括异常.IO流.多线程.集合类.数据库.(切记基础知识一定要时时刻刻巩固,注意,如果你是想以最快速度学习J2EE,关于Java中的Swing知识点,就只做了解) ...
- eclipse java 配置
1.eclipse菜单 - Window - Preferences- Java - Installed JREs 2.eclipse菜单 - Window - Preferences- Java - ...
- Javascript:作用域 学习总结
作用域(scope): 变量与函数的可访问范围,控制着变量与函数的可见性和生命周期 作用域分类: javascript中,变量的作用域分为:全局作用域,局部作用域 局部变量的优先级大于全局变量,或 ...
- Oracle11g环境设置-windows环境
新建环境变量(系统变量),变量名:ORACLE_HOME 变量值:E:\app\Administrator\product\11.2.0\dbhome_1 新建环境变量(系统变量),变量名:ORACL ...
- sqlserver2005重新安装(安装汇编错误,安装程序无法连接到数据库服务进行服务配置)
2014-01-09 16:41 1687人阅读 评论(1) 收藏 举报 分类: 数据库(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. sqlserver2005重新安装(安装汇编错误, ...
- jquery cookie 删除不了的处理办法
$.cookie(name, null);$.cookie(name, null, {path : "/"}); Jquery Cookie的值直接设置null,并不能直接删除Co ...
- 关于(x&y)+((x^y)>>1)的探究
今天在程序员面试宝典上看到 int f(int x int y ) { return (x&y)+((x^y)>>1) } f(729,271) 结果为500 从式子中可以看出分为 ...
- C#中KeyDown和KeyPress区别
1.比如说TexBox 输入'a' 按下->触发KeyDown事件,然后去处理 ->将a显示输入到文本框后 ->触发KeyPress事件