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]\) ...
随机推荐
- LR性能测试课程及视频教程
LR性能测试课程及视频教程课程如下: 1.性能测试核心技术-2.性能测试脚本开发-3.LR场景设计-4.LR指标分析. 1.性能测试是通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各 ...
- laotech老师唠科mac 深入浅出MAC OS X
laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...
- JAVA RPC (九) netty服务端解析
源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...
- vue pdf下载
主要技术栈是Vue,两个库: html2canvas npm地址 jspdf 具体实现代码如下: <template> <div class="priview_resume ...
- SQL连接查询基础知识点
什么是连接 连接(join)查询是基于多个表中的关联字段将数据行拼接到一起,可以同时返回多个表中的数据. 下面以两个表为例子,举例说明一下不同的连接. SELECT * FROM products i ...
- pwn学习日记Day21 《程序员的自我修养》读书笔记
Linux内核装载ELF过程 (1)bash进程调用fork()系统调用创建一个新的进程 (2)新的进程调用execve()系统调用执行指定的ELF文件,原先的bash进程继续返回等待刚才启动的新进程 ...
- Async and Await (Stephen Cleary)
https://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...
- 01背包---P2392 kkksc03考前临时抱佛脚
P2392 kkksc03考前临时抱佛脚 题解 01背包,类似于这道题,相似度99.999999%: 01-背包 P2663 越越的组队 一共有4科,每科的时间独立,然后每一科做一遍 P2663越 ...
- 使用pycharm发布python程序到ubuntu中运行
前提条件: 1.ubuntu安装了vsftpd,可以参考:https://www.cnblogs.com/xienb/p/9322805.html 2.安装专业版pycharm 步骤: 1.新建一个P ...
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...