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]\) ...
随机推荐
- gitlab高可用模式
高可用模式 企业版 社区版 我们这里说一下成本比较低的主备模式,它主要依赖的是DRBD方式进行数据同步,需要2台ALL IN ONE的GitLab服务器,也就是通过上面安装方式把所有组件都安装在一起的 ...
- 如何在vue中使用svg
1.安装依赖 npm install svg-sprite-loader --save-dev 2.在config文件中配置 const path = require('path'); func ...
- 分页——为Mybatis配置PageHelper
1.pom.xml追加 pagehelper : 4.1.4 2.mappers.xml中追加 <plugins> <plugin interceptor="com.git ...
- 初次Java web项目的建立以及与数据库的连接
题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...
- 基于 intellij IDEA 快速搭建Spring Boot项目
在<一步步搭建 Spring Boot maven 框架的工程>一文中,已经介绍了如何使用Eclipse快速搭建Spring Boot项目.由于最近将开发工具由Eclipse ...
- 5.3.4 Hadoop序列化框架
序列化框架 除了writable实现序列化之外,只要实现让类型和二进制流相互转换,都可以作为hadoop的序列化类型,为此Hadoop提供了一个序列化框架接口,他们在org.apache.hadoop ...
- FYI是什么意思?
FYI是For Your Information的缩写,英语解释为:提供给您的信息,供您参考,是当前在外企中流行的英文缩写,为工作用语,常用于工作的电子邮件中. 文章来源:刘俊涛的博客 欢迎关 ...
- Linux发行版本简介
Linux发行版 1. Linux本身 1.1. 1991年,当时一名来自赫尔辛基的计算机科学学生LinusTorvalds创建了一个操作系统内核 1.1.1. 一年后 ...
- MySQL连接错误:Can't connect to MySQL server on'localhost' (10055)
在Windows服务器上确认服务器和mysql都是正常运行,但就是连接不上.搜了一下别人的解决方案, 参考这篇https://blog.csdn.net/langren697/article/deta ...
- [Scikit-learn] Dynamic Bayesian Network - Conditional Random Field
李航,第十一章,条件随机场 参考:[PGM] Markov Networks 携代码:用 Python 通过马尔可夫随机场(MRF)与 Ising Model 进行二值图降噪[推荐!] CRF:htt ...