题目描述:

F. Ant colony
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.

In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r(1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si).

After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.

In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.
Input

The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.

The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.

The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.

Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.
Output

Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
Sample test(s)
input

5
1 3 2 4 2
4
1 5
2 5
3 5
4 5

output

4
4
1
1

Note

In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.

In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.

In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.

In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.

思路:

题目的意思是说,给一个数列,看里面有多少个数,这样的数可以被数列中的其他所有数整除。显然这个数就是数列的gcd啦!为什么呢?首先gcd可以满足条件,然后如果不是gcd,那就是gcd的因数,可是数列中的数如果有一个是gcd的因数那它小于等于gcd,而它又不可能比gcd小,只能相等。(为什么,它要是比gcd小,那它才会是gcd)。我怎么会有这么奇怪的想法怀疑它不是gcd (キ`゚Д゚´)!!

又因为是区间查询问题,整一个线段树来维护区间gcd,和等于gcd的数目。注意的是build函数里这么pushup,还有查询函数怎么统计结果。

pushup就是对一个节点求左右两个节点的gcd,如果左边的节点的gcd与这个gcd相等,统计数目加左边的相等数目,如果右边的等就再加右边的数目,不等就是零。

query函数求答案的时候要看一下当前区间答案来自哪里,是左区间,还是右区间,还是两边都有?分别处理一下就好。

这道题竟然连懒标记都没用,就是静态查询√

代码:

 #include <iostream>
#define max_n 100005
using namespace std;
int n;
int t;
struct node
{
int num;
int gcd;
int id;
}tree[max_n<<];
int a[max_n]; int GCD(int a,int b)
{
if(a<b) swap(a,b);
int r = a%b;
if(r==)
{
return b;
}
return GCD(b,r);
}
void build(int id,int l,int r)
{
if(l==r)
{
tree[id].gcd = a[l];
tree[id].num = ;
return;
}
int mid = (l+r)>>;
build(id<<,l,mid);
build(id<<|,mid+,r);
int gcd = GCD(tree[id<<].gcd,tree[id<<|].gcd);
tree[id].num = ;
tree[id].gcd = gcd;
if(tree[id<<].gcd==gcd)
{
tree[id].num += tree[id<<].num;
}
if(tree[id<<|].gcd==gcd)
{
tree[id].num += tree[id<<|].num;
}
}
pair<int,int> query(int id,int L,int R,int l,int r)
{
//cout << "l " << l << " r " << r << endl;
if(L<=l&&r<=R)
{
int gcd = tree[id].gcd;
int num = tree[id].num;
//cout << "gcd " << gcd << "num " << num << endl;
return pair<int,int>(gcd,num);
}
int mid = (l+r)>>;
int ans = ;
pair<int,int> res1,res2;
if(L<=mid){ res1 = query(id<<,L,R,l,mid); }
if(mid<R) {res2 = query(id<<|,L,R,mid+,r);}
int gcd;
if(L<=mid)
{
if(mid<R)
{
gcd = GCD(res1.first,res2.first);
//cout << "gcd " << gcd << endl;
if(res1.first==gcd)
ans+=res1.second;
if(res2.first==gcd)
ans+=res2.second;
}
else
{
gcd = res1.first;
ans += res1.second;
}
}
else
{
gcd = res2.first;
ans += res2.second;
}
//cout << "gcd " << gcd << " ans " << ans << endl;
return pair<int,int>(gcd,ans);
}
int main()
{
//cout << GCD(1,3) << endl;
cin >> n;
for(int i = ;i<=n;i++)
{
cin >> a[i];
}
build(,,n);
cin >> t;
for(int q = ;q<t;q++)
{
int L,R;
cin >> L >> R;
cout << R-L+-query(,L,R,,n).second << endl;;
}
return ;
}

Codeforces G. Ant colony的更多相关文章

  1. CodeForces 474F Ant colony ST+二分

    Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...

  2. Codeforces 474F - Ant colony

    注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...

  3. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  4. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  6. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

  7. bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分

    3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entranc ...

  8. 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

  9. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

随机推荐

  1. node框架那些事儿

    一.简单介绍 express:适合初学者,模版引擎,路由,中间件 koa2:核心中间件 eggjs:企业级应用框架 二.express 服务端框架,提供路由功能,异常处理.路由系统+中间件构成web开 ...

  2. vue-loader会缓存node_modules中依赖模块

    问题如下: https://github.com/vuejs/vue-cli/issues/3635 截止到vue cli3还没有解决 手动删除node_modules下的.cache文件夹可以解决这 ...

  3. SignalR 填坑记

    1.发送文字消息没有问题,如何发送文件消息 SignalR可以将参数序列化和反序列化. 这些参数被序列化的格式叫做Hub 协议, 所以Hub协议就是一种用来序列化和反序列化的格式. Hub协议的默认协 ...

  4. Win10 专业版 Hyper-V 主机计算服务无法启动

    Windows 10升级1809版本后,发现Hyper-V不能用了,管理器里是一片空白,看服务Hyper-V 主机计算服务没有启动,手动启动的话失败,报错,代码1053. 自己尝试修复,也百度了很久, ...

  5. (CSDN迁移)JAVA多线程实现-继承Thread

    继承Thread方法: extends Thread 重写覆盖run()方法: @Override public void run() 通过start()方法启动线程. threadDemo01.st ...

  6. mysql报错 常见 1045 10061

    报错1045: 远程没有设置用户远程访问的权限 解决方案: 进行授权(红色是你的密码) 如果想root用户使用password从任何主机连接到mysql服务器的话. GRANT ALL PRIVILE ...

  7. Golang修改json文件的两种方法

    第三方包 go get -u github.com/tidwall/sjson bytes, _ := ioutil.ReadFile(jsonFile) value1, _ := sjson.Set ...

  8. ZYNQ笔记(3):GPIO的使用(MIO、EMIO)——led灯

    一.GPIO原理 1.GPIO介绍 程序员通过软件代码可以独立和动态地对每个 GPIO 进行控制,使其作为输入.输出或中断. (1)通过一个加载指令,软件可以读取一个 GPIO 组内所有 GPIO 的 ...

  9. Python与MogoDB交互

    睡了大半天,终于有时间整理下拖欠的MongoDB的封装啦. 首先我们先进行下数据库的连接: conn = MongoClient('localhost',27017) # 建立连接 result = ...

  10. 【题解】Luogu P5405 [CTS2019]氪金手游

    原题传送门 我们珂以先考虑一条链的情况,设\(sum\)为所有\(w_i\)的总和,\(Sw_i\)表示\(\sum_{j=i}^nw_i\) \[1 \rightarrow 2 \rightarro ...