题目链接: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. python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)

    什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: 一 from threading import Thread ...

  2. Linux异常体系之stubs_offset

    转自 http://www.xuebuyuan.com/2208550.html 在ARM V4及V4T以后的大部分处理器中,中断向量表的位置可以有两个位置:一个是0x00000000,另一个是0xf ...

  3. HDU - 1251 统计难题(Trie树)

    有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...

  4. 水题:HDU1716-排列2

    排列2 Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代 ...

  5. Persona5

    65536K   Persona5 is a famous video game. In the game, you are going to build relationship with your ...

  6. Python虚拟机函数机制之扩展位置参数和扩展键参数(六)

    扩展位置参数和扩展键参数 在Python虚拟机函数机制之参数类别(三)的例3和例4中,我们看到了使用扩展位置参数和扩展键参数时指示参数个数的变量的值.在那里,我们发现在函数内部没有使用局部变量时,co ...

  7. Mybatis中接口和对应的mapper文件位置配置详解

    Mybatis中接口和对应的mapper文件位置配置详解 原链接为:https://blog.csdn.net/fanfanzk1314/article/details/71480954 今天遇到一个 ...

  8. luogu1233 木棍加工

    先排个序然后做最长上升子序列就行了. #include <algorithm> #include <iostream> #include <cstdio> usin ...

  9. python - 文件处理/open

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_文件处理.py@ide: PyCharm Community E ...

  10. custom post types 404 Page Error

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