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 树状数组的更多相关文章

  1. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  2. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...

  3. 计蒜客 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 ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  5. 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 ...

  6. 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 ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 F. Features Track

    262144K   Morgana is learning computer vision, and he likes cats, too. One day he wants to find the ...

  8. 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 ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和

    链接 https://nanti.jisuanke.com/t/31456 参考题解  https://blog.csdn.net/ftx456789/article/details/82590044 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛(8/11)

    ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...

随机推荐

  1. access函数

    access函数是按照实际用户ID和实际组ID进行访问测试的.函数的定义如下: #include <unistd.h> int access(const char* pathname, i ...

  2. 数据结构实验之图论八:欧拉回路(SDUT 3364)

    Problem Description 在哥尼斯堡的一个公园里,有七座桥将普雷格尔河中两个岛及岛与河岸连接起来. 能否走过这样的七座桥,并且每桥只走一次?瑞士数学家欧拉最终解决了这个问题并由此创立了拓 ...

  3. struts2方法无法映射问题:There is no Action mapped for namespace [/] and action name [m_hi] associated with context path []

    使用struts的都知道,下面使用通配符定义的方式很常见,并且使用也很方便: <action name="Crud_*" class="example.Crud&q ...

  4. redhat7.4安装gitlab

    1.参考官方安装指南 https://about.gitlab.com/install/#centos-7 2.遇到的问题 2.1.启动postfix出错 错误内容 Job for postfix.s ...

  5. SQL Server Management Studio 清除用户名和密码

    SQL Server Management Studio 2018 delete the file C:\Users\%username%\AppData\Roaming\Microsoft\SQL  ...

  6. 经典算法:n个人围成一圈,报m的离开,最后剩下谁?

    public int remainPersonNumber(int n, int m) { //输入不合法 if(n < 1 || m < 1) return -1; //初始化,存入Li ...

  7. pwn学习日记Day13 《程序员的自我修养》读书笔记

    重定位就是把程序的逻辑地址空间变换成内存中的实际物理地址空间的过程.它是实现多道程序在内存中同时运行的基础.重定位有两种,分别是动态重定位与静态重定位. 静态重定位:即在程序装入内存的过程中完成,是指 ...

  8. 笔记七(编写第一个UEFI程序)

    搭建好uefi开发环境之后,在MyWorkspace文件夹中建立一个文件夹ExamplePkg; ,然后在ExamplePkg文件夹中创建HelloWorld文件夹,Include文件夹,Exampl ...

  9. js中两个感叹号的原理与用法分析

    在javascript中有时会看到有两个!!的用法 var foo; alert(!foo);//undifined情况下,一个感叹号返回的是true; alert(!goo);//null情况下,一 ...

  10. 通过sqlserver用户操作远程服务器

    USE masterGORECONFIGURE --先执行一次刷新,处理上次的配置GOEXEC sp_configure 'show advanced options',1 --启用xp_cmdshe ...