ACM-ICPC 2018 徐州赛区网络预赛 I. query 树状数组
I. query
题目链接:
Problem Description
Given a permutation \(p\) of length \(n\), you are asked to answer \(m\) queries, each query can be represented as a pair \((l ,r )\), you need to find the number of pair \((i ,j)\) such that \(l \le i < j \le r\) and \(\min(p_i,p_j) = \gcd(p_i,p_j )\).
Input
There is two integers \(n(1 \le n \le 10^5)\), \(m(1 \le m \le 10^5)\) in the first line, denoting the length of \(p\) and the number of queries.
In the second line, there is a permutation of length \(n\), denoting the given permutation \(p\). It is guaranteed that \(p\) is a permutation of length \(n\).
For the next \(m\) lines, each line contains two integer \(l_i\) and \(r_i(1 \le l_i \le r_i \le n)\), denoting each query.
Output
For each query, print a single line containing only one integer which denotes the number of pair \((i,j)\).
样例输入
3 2
1 2 3
1 3
2 3
样例输出
2
0
题意
给你一个序列,求很多段子区间\((l ,r )\)满足\(l \le i < j \le r\) and \(\min(p_i,p_j) = \gcd(p_i,p_j )\) 的个数。
题解
1.转化一下就是求一个区间有多少对满足一个是另一个的倍数。
2.我们会发现这个是一个排列,每个数x的倍数个数为\(\frac{n}{x}\),那么所有的倍数个数即为\(\sum_{i=1}^{n}\frac{n}{i})(\le nlog_{2}{n+1})\)
3.我们将所有倍数点对预处理出来,问题就变成了问一个区间有多少倍数点对同时存在。
4.是不是很熟悉啦(不知道也没关系),我来细细讲解一下:
- 先将区间按右端点从小到大排序,保证右端点单调递增
- 那么起作用的就是左端点,这是我们碰到一个点就将它左边的所有是它约数以及倍数的位置权值全部+1,这样如果左边这个点在区间里,右端点必然也在区间里因为右端点单调递增。
如果真的理解了的话想想按左端点从大到小也可以做,想想怎么做?
其实这题是cf原题,网络赛时我不会做,然后竟然搜到了原题(还是有极其微小的差异),然后现学啦,哈哈哈。
cf链接:codeforces 301D
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 100050
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int n,m,a[N],p[N],c[N],ans[N];
vector<int>vec[N];
struct Query
{
int l,r,id;
bool operator <(const Query b)const
{return r<b.r;}
}que[N];
void change(int x){while(x<=n)c[x]++,x+=x&-x;}
int ask(int x){int ans=0;while(x)ans+=c[x],x-=x&-x;return ans;}
void work()
{
read(n); read(m);
for(int i=1;i<=n;i++) read(a[i]),p[a[i]]=i;
for(int i=1;i<=m;i++) read(que[i].l),read(que[i].r),que[i].id=i;
for(int i=1;i<=n;i++)
{
for(int j=a[i]+a[i];j<=n;j+=a[i])
if (i<p[j])vec[p[j]].push_back(i);
else vec[i].push_back(p[j]);
}
sort(que+1,que+m+1);
int r=0;
for(int i=1;i<=m;i++)
{
for(int j=r+1;j<=que[i].r;j++)
for(int k=0;k<vec[j].size();k++)change(vec[j][k]);
r=que[i].r;
ans[que[i].id]=ask(que[i].r)-ask(que[i].l-1);
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
work();
}
ACM-ICPC 2018 徐州赛区网络预赛 I. query 树状数组的更多相关文章
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)
ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...
- 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)
H.Ryuji doesn't want to study 27.34% 1000ms 262144K Ryuji is not a good student, and he doesn't wa ...
- ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)
传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...
- ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE
In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study
262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...
- ACM-ICPC 2018 徐州赛区网络预赛 F. Features Track
262144K Morgana is learning computer vision, and he likes cats, too. One day he wants to find the ...
- ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash
Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...
- ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和
链接 https://nanti.jisuanke.com/t/31456 参考题解 https://blog.csdn.net/ftx456789/article/details/82590044 ...
- ACM-ICPC 2018 徐州赛区网络预赛(8/11)
ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...
随机推荐
- 7.26T1四分图匹配
四分图匹配 题目描述 一天晚上,zzh 在做梦,忽然梦见了她. 见到她,zzh 也不去看她,只顾低头自语…… “噫,OI 这个东西,真是无奇不有.” “嘿,你又学了什么?” “嗯,学到了一种算法,”z ...
- skb head/data/tail/end/介绍
2017年04月26日 18:21:12 abcLinux 阅读数 799 This first diagram illustrates the layoutof the SKB data are ...
- 哈希表(hash table)基础概念
哈希是什么 引入:我们在学习数组的时候,使用数组元素的下标值即可访问到该元素,所花费的时间是O(1),与数组元素的个数n没有关系,这就是哈希方法的核心思想. 哈希方法:以关键值K为自变量,通过一定的函 ...
- ubuntu下安装g++
主要来自ubuntu中文社区http://www.ubuntu.org.cn/support/documentation/doc/VMware 首选,确认你已经安装了build-essential程序 ...
- java 设计模式 之 装饰器模式
装饰器模式的作用 在不修改原先对象核心的功能的情况下,对功能进行增强. 增强对象的功能的途径 通过类继承的方式,对父对象进行增强操作,例如造车是父类,改装跑车,跑车加大灯,改装房车,房车加私人电影院. ...
- kotlin使用中辍标记法调用函数
fun main(arg: Array<String>) { var str = "hello world" print(str div("l")) ...
- MySQL、sqlalchemy、pymysql、mysqldb、DBAPI之间关系梳理(终于明白了)
MySQL.sqlalchemy.pymysql.mysqldb.DBAPI之间关系梳理(终于明白了) python3不再支持mysqldb 请用pymysql和mysql.connector 问题背 ...
- 利用mysql数据库中的TMD表修复“is marked as crashed and last (automatic?) repair failed”的错误 Database query error
===========================测试成功============================================= 1.页面出现错误:Database query ...
- spring data jpa实现多条件查询(分页和不分页)
目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...
- 轻量级通用上采样算子-CARAFE
转载:https://zhuanlan.zhihu.com/p/76063768 前言 这篇论文被 ICCV 2019 接收为 oral presentation.之前我们主要研究物体检测(例如 Hy ...