Codeforces Round #271 (Div. 2) F. Ant colony 线段树
1 second
256 megabytes
standard input
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.
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.
Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
5
1 3 2 4 2
4
1 5
2 5
3 5
4 5
4
4
1
1
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,区间最小值。区间最小值个数
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+,MOD=1e9+;
const LL INF=1e18+,mod=1e9+; struct is
{
int gcd,minn,num;
};
struct SGT
{
int minn[N<<],gcd[N<<],num[N<<];
void pushup(int pos)
{
if(minn[pos<<]==minn[pos<<|])
minn[pos]=minn[pos<<],num[pos]=num[pos<<]+num[pos<<|];
else if(minn[pos<<]<minn[pos<<|])
minn[pos]=minn[pos<<],num[pos]=num[pos<<];
else minn[pos]=minn[pos<<|],num[pos]=num[pos<<|];
gcd[pos]=__gcd(gcd[pos<<],gcd[pos<<|]);
}
void build(int l,int r,int pos)
{
if(l==r)
{
scanf("%d",&minn[pos]);
gcd[pos]=minn[pos];
num[pos]=;
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
is query(int L,int R,int l,int r,int pos)
{
if(L==l&&r==R)return (is){gcd[pos],minn[pos],num[pos]};
int mid=(l+r)>>;
if(L>mid)return query(L,R,mid+,r,pos<<|);
else if(R<=mid)return query(L,R,l,mid,pos<<);
else
{
is x=query(L,mid,l,mid,pos<<);
is z=query(mid+,R,mid+,r,pos<<|);
int gcd=__gcd(x.gcd,z.gcd);
int num,minn=;
if(x.minn==z.minn)
minn=x.minn,num=x.num+z.num;
else if(x.minn<z.minn)
minn=x.minn,num=x.num;
else minn=z.minn,num=z.num;
return (is){gcd,minn,num};
}
}
}tree;
int main()
{
int n;
scanf("%d",&n);
tree.build(,n,);
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
is ans=tree.query(l,r,,n,);
//cout<<ans.minn<<" "<<ans.gcd<<endl;
if(ans.minn&&ans.gcd%ans.minn==)printf("%d\n",r-l+-ans.num);
else printf("%d\n",r-l+);
}
return ;
}
Codeforces Round #271 (Div. 2) F. Ant colony 线段树的更多相关文章
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)
题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #271 (Div. 2) F ,E, D, C, B, A
前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #546 (Div. 2) E 推公式 + 线段树
https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...
- Codeforces Round #316 (Div. 2) C. Replacement(线段树)
C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- 【javascript】对原型对象、原型链的理解
原型对象,原型链这些知识属于基础类知识.但是平时开发过程中也很少用到. 看网上的意思,原型链用于es5开发场景下的继承.es6有了类语法糖之后,就自带继承了. 通过理解,个人画了一张原型链解构的关系图 ...
- "添加与删除程序"报rundll32错误
无法启动"添加与删除程序"系统报rundll32错误 系统反馈以下信息: rundll32.exe应用程序错误"0x00310030"指令 解决方法: 1.启动 ...
- bzoj 1295 最长距离 - 最短路
Description windy有一块矩形土地,被分为 N*M 块 1*1 的小格子. 有的格子含有障碍物. 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离. 如果从格 ...
- 用Navicat for Mysql导入.sql文件
1.在左边右键新建一个数据库 2.字符集选gbk(不 题) 3.打开数据库,把它变成绿色. 4.把.sql文件拖到这数据库上.会出现下边的运行SQL文件对话框,按开始,等运行完后就可以关掉了. 5.最 ...
- Firemonkey的几个特色属性(二)
3.RotationAngle 控件的旋转角度,可以通过TAnimation进行角度旋转控制. 4.RotationCenter 控件旋转的中心位置,从(0,0)到(1,1),缺省是(0.5,0.5) ...
- Python3基础 response.read 输出网页的源代码
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Mac 配置教程-日常篇
今年终于在推出 2018 款 MBP 时,看到升级了 CPU,我就果断下手「拔草」.本文记录使用 Mac 的一些配置,会长期更新. 为了控制文章的篇幅,我将 Mac 使用配置分成了两篇: Mac 配置 ...
- Ubuntu 18.04 修改gedit的配色方案
下图中的蓝色的注释代码,真是有点让人瞎眼的感觉 去这个网站 https://github.com/mig/gedit-themes/tree/master 下载所有后解压到/usr/share/gtk ...
- 如何使用thinkphp的model来验证前端表单?
为了增加安全性, 在向model表中写入和修改数据时, 最好是调用 create方法来保证安全, 然后再调用add和save方法: if($Model->Validate($validate)- ...
- 转载:Systemd 命令
目录 一.由来 二.Systemd 概述 三.系统管理 3.1 systemctl 3.2 systemd-analyze 3.3 hostnamectl 3.4 localectl 3.5 time ...