题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224

hdu5869

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e6 + ;
int a[N/ + ], bit[N]; //_pos存的是gcd上一次出现的位置
vector <P> ans[N/ + ]; //存的是以i为右端点的gcd
map <int, int> _pos;
struct query {
int l, r, pos;
bool operator <(const query& cmp) const {
return r < cmp.r;
}
}q[N/ + ];
int res[N/ + ]; //答案 void init(int n) {
_pos.clear();
memset(bit, , sizeof(bit));
for(int i = ; i <= n; ++i) {
ans[i].clear();
}
} int GCD(int a, int b) {
return b ? GCD(b, a % b): a;
} void add(int i, int x) {
for( ; i <= N; i += (i&-i))
bit[i] += x;
} int sum(int i) {
int s = ;
for( ; i >= ; i -= (i&-i))
s += bit[i];
return s;
} int main()
{
int n, m, t;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
init(n);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
}
for(int i = ; i <= m; ++i) {
scanf("%d %d", &q[i].l, &q[i].r);
q[i].pos = i;
}
for(int i = ; i <= n; ++i) {
int x = a[i], y = i;
for(int j = ; j < ans[i - ].size(); ++j) {
int gcd = GCD(x, ans[i - ][j].first);
if(gcd != x) {
ans[i].push_back(make_pair(x, y));
x = gcd, y = ans[i - ][j].second;
}
}
ans[i].push_back(make_pair(x, y));
}
sort(q + , q + m + );
int p = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j < ans[i].size(); ++j) {
if(!_pos[ans[i][j].first]) {
add(ans[i][j].second, );
_pos[ans[i][j].first] = ans[i][j].second;
} else {
add(_pos[ans[i][j].first], -);
_pos[ans[i][j].first] = ans[i][j].second;
add(ans[i][j].second, );
}
}
while(i == q[p].r && p <= m) {
res[q[p].pos] = sum(q[p].r) - sum(q[p].l - );
++p;
}
}
for(int i = ; i <= m; ++i) {
printf("%d\n", res[i]);
}
}
return ;
}

FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869的更多相关文章

  1. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

  2. 区间gcd问题 HDU 5869 离线+树状数组

    题目大意:长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种. 子段就是表示是要连续的a[] 思路:固定右端点,预处理出所有的gcd,每次都和i-1的gcd比较,然后 ...

  3. FZU2224 An exciting GCD problem 区间gcd预处理+树状数组

    分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...

  4. 【HDU4947】GCD Array(莫比乌斯反演+树状数组)

    点此看题面 大致题意: 一个长度为\(n\)的数组,实现两种操作:将满足\(gcd(i,k)=d\)的\(a_i\)加上\(v\),询问\(\sum_{i=1}^xa_i\). 对于修改操作的推式子 ...

  5. A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. poj_3468: A Simple Problem with Integers (树状数组区间更新)

    题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. 这道题也可以用线段树解,本文不做描述,下面分析如何用树状数组来解决这道题. 先把问 ...

  7. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   ...

  8. POJ 3468 A Simple Problem with Integers 【树状数组】

    题目链接:id=3468">http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b.要求输出v[a]到v[b]之 ...

  9. poj 3468: A Simple Problem with Integers (树状数组区间更新)

    题目链接: http://poj.org/problem?id=3468 题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. 这道题也 ...

随机推荐

  1. sublime text3 安装ctags实现函数跟踪跳转

    来源:http://blog.csdn.net/menglongfc/article/details/51141084 本人试用平台如下:sublime text3,和谐版 在source insig ...

  2. DP刷题记录(长期更新)

    bzoj 2748 一个吉他手,有一个初始音量,有一个音量最大值max. 给定n个音量变化量,从第一个变化量开始,可以选择加上或者减去变化量.途中音量不能低于0,不能超过max. 求最后能达到的最大音 ...

  3. alidoing --使用JS实现多语言框架、喜欢的请进、、瓦特平台!

    大家好! 多语言实现的案例:http://alidoing.com/或者http://www.alidoing.com/ 图:切换语言界面 JS代码实现: 1.首先新建一个对象langobj,当然对象 ...

  4. 解决前端工程师与UI设计协同工作的问题

    前端工程师与UI设计协同工作主要环节在于设计图与前端界面是否一致.(还原度) 不得不说,设计图与前端界面实现不一致的问题时有发生.(好吧,我经验有限)所以经常写完的前端页面都需要去修改.(特别是做移动 ...

  5. custom post types 404 Page Error

    问题: 注册新的文章类型后,用新的类型写文章,打开后报 404 错误 原因: 因为虽然注册了新的帖子类型,但WordPress还不知道如何处理它 解决: 到设置 -> 固定链接,重新点击保存,再 ...

  6. jQuery效果show()方法

    $("button").click(function(){ $("p").show(); }); Syntax $(selector).show(speed,e ...

  7. Leetcode 430.扁平化多级双向链表

    扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示. 扁 ...

  8. [python IO学习篇] [打开包含中文路径的文件]

    https://my.oschina.net/mcyang000/blog/289460   打开路径含有中文的文件时,要注意: 1 在windows对文件名编码是采用gbk等进行编码保存,所以要将文 ...

  9. [Android Studio篇][1] AS开发中遇到问题汇总

    1 在android新建文件,提示权限不够,增加权限 修改工程下 main/AndroidMainfest.xml增加 <uses-permission android:name="a ...

  10. java.net.ConnectException: Connection timed out: no further information

    ping IP 地址 检查是否连上 重启虚拟机 检查主机