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 ...
随机推荐
- Git在Eclipse中的使用
一.把远程仓库的项目clone到eclipse里面: 最新版的Eclipse上已经集成了Git插件.所以在Eclipse中可以很方便的使用Git的功能. 在使用Git功能之前,需要先进行下简单的设置. ...
- Hbase 过滤器的使用
Filter filter= new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("."+d ...
- Scrapy小技巧-MySQL存储, MYSQL拼接
这两天上班接手,别人留下来的爬虫发现一个很好玩的 SQL脚本拼接. 只要你的Scrapy Field字段名字和 数据库字段的名字 一样.那么恭喜你你就可以拷贝这段SQL拼接脚本.进行MySQL入库处理 ...
- ELK学习笔记之F5利用ELK进行应用数据挖掘系列(1)-HTTP
0x00 概述 F5 BIGIP从应用角度位于网络结构的关键咽喉位置,可获取所有应用的流量,针对流量执行L7层处理,即便是TLS加密的流量也可以通过F5进行SSL offload.通过F5可以统一获取 ...
- python的shutil模块-文件的移动、复制、打包、压缩、解压等
参考https://www.cnblogs.com/xiangsikai/p/7787101.html os模块提供了对目录或者文件的新建.删除.查看文件属性,还提供了对文件以及目录的路径操作,比如说 ...
- log4j2配置推荐
<?xml version="1.0" encoding="UTF-8"?> <!-- monitorInterval为监听配置变化的间隔,3 ...
- windows 7/10 安装u盘制作
今天,在拷贝数据时,发现那台丢在那两三年的pc密码忘了,故计划重装,因为从来都是公司信息中心管这事,至少都七八年没有自己装机了,故整理过程如下: 1.从itellyou.cn下载原版镜像: 2.准备一 ...
- 【题解】 Luogu CF375D Tree and Queries
原题传送门 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 我博客中对莫队的详细介绍 莫队好题 我一上来想写线段树,随后觉得不好写并弃坑 我们可以看见没有修改操作,钦定莫队 但这是在树上,所以不能 ...
- 01:MongoDB基础
1.1 MongoDB简介 1.特点 1. MongoDB的提供了一个面向文档存储,操作起来比较简单和容易. 2. 你可以在MongoDB记录中设置任何属性的索引 (如:FirstName=" ...
- mysql 用存储过程插入11位 随机数
BEGIN #Routine body goes here... ; ); ); ); ); ); ); ); ); ); ); ); ); ) DEFAULT ''; ); ); WHILE row ...