CF #271 F Ant colony 树
题目链接:http://codeforces.com/contest/474/problem/F
一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字。
能够整除其他所有数字的数一定是这些数字的gcd,所以可以用一个线段树来查询区间gcd。
接着需要统计区间内这个数字的出现个数,在读取数组时,顺便保存一个pair<int,int>键为数字,val为位置的数组,再sort一下,统计区间内某个数字出现次数,直接upper_bound-lower_bound即可。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <numeric>
#include <cassert> using namespace std; const int N=1e5+; int s[N];
pair<int,int> d[N];
int t[N<<]; int gcd(int a,int b){
if (b==) return a;
return gcd(b,a%b);
}
void up(int rt) {
t[rt]=gcd(t[rt<<],t[rt<<|]);
}
void build(int l,int r,int rt) {
if (l==r){
t[rt]=s[l];
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
up(rt);
}
int ask(int L,int R,int l,int r,int rt) {
if (L<=l&&r<=R) {
return t[rt];
}
int m=(l+r)>>;
int ret=,u=,v=;
if (L<=m)
u=ask(L,R,l,m,rt<<);
if (R>m)
v=ask(L,R,m+,r,rt<<|);
return gcd(u,v);
}
int main () {
int n;
scanf("%d",&n);
for (int i=;i<=n;i++) {
scanf("%d",s+i);
d[i-]=make_pair(s[i],i);
}
build(,n,);
sort(d,d+n);
int m;
scanf("%d",&m);
while (m--) {
int l,r;
scanf("%d %d",&l,&r);
int g=ask(l,r,,n,);
int u=lower_bound(d,d+n,make_pair(g,l))-d;
int v=lower_bound(d,d+n,make_pair(g,r+))-d;
printf("%d\n",r-l+-(v-u));
//printf("%d %d %d\n",g,u,v);
}
return ;
}
CF #271 F Ant colony 树的更多相关文章
- [CF] 474 F. Ant colony
区间重复不会影响GCD,ST表当然是支持的啦,常数这么小. 学到了三个东西: 1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围. 2.ST表更 ...
- 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 ...
- Codeforces 474 F. Ant colony
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分
3872: [Poi2014]Ant colony Time Limit: 30 Sec Memory Limit: 128 MB Description There is an entranc ...
- 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
- 【BZOJ3872】Ant colony(二分,动态规划)
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...
随机推荐
- 导航栏转场动画CATransition
CATransition动画来实现, 下面的代码是跳转到下一个视图: CATransition *animation = [CATransition animation]; [animation se ...
- 升级后 VTE 类虚拟终端不工作
故障现象 运行 vte 终端,如 gnome terminal.sakura 等光标不出来.xterm 可以运行. 在 xterm 终端中运行 gnome terminal 出现一下错误: grant ...
- python 使用内置函数sorted对各种数据类型进行排序
python有两个内置的函数用于实现排序,一个是list.sort()函数,一个是sorted()函数. 区别1:list.sort()函数只能处理list类型数据的排序:sorted()则可以处理多 ...
- 自动化测试工具Appium环境搭建
Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持IOS.Android及FirefoxOS平台.Appium使用WebDriver的json wire协议 ...
- 【转】nginx配置:location配置方法及实例详解
location匹配的是nginx的哪个变量? $request_uri location的匹配种类有哪些? 格式 location [ 空格 | = | ~ | ~* | !~ | !~* ] /u ...
- ViewPager 滑动一半的判断方法以及左滑右滑判断
做项目的时候,会碰到用viewpager + fragments去实现多页滑动.有些时候需要完成:界面在滑动到一半或是一半以上的时候,需要把title之类的切换到下一个页面.这个时候仅仅依赖Viewp ...
- Omi v1.0.2发布 - 正式支持传递javascript表达式
原文地址:https://github.com/AlloyTeam/omi/ 写在前面 Omi框架可以通过在组件上声明 data-* 把属性传递给子节点. Omi从设计之初,就是往标准的DOM标签的标 ...
- (23)IO之打印流 PrintStream & Printwriter
PrintStream PrintStream可以接受文件和其他字节输出流,所以打印流是对普通字节输出流的增强,其中定义了很多的重载的print()和println(),方便输出各种类型的数据. Pr ...
- Oracle数据块损坏的恢复实例
测试环境:11.2.0.4 1.构建数据块损坏的测试环境 2.有备份:常规恢复坏块 3.无备份:跳过坏块 1.构建数据块损坏的测试环境 1.1 创建测试表 --Create Table t_test ...
- mac开发环境配置
折腾了好几天了,终于安装一部分了,mac装的win10,太占空间了,看到学习资源使用的工具,自己搜了一下安装了,在学习使用git的时候,都说mac比win好用多了,我tm为啥抱着mac装win费劲呢! ...