(2)首先成倍数对的数量是nlogn级别的,考虑每一对【xL,xR】(下标的位置,xL < xR)会对那些询问做出贡献,如果qL <= xL && qR >= xR,那么这一对就会对询问【qL,qR】贡献1;现在把它们看成平面上的点,那么对于每一个询问【qL,qR】,就相当于计算这个点右下角有多少个点,这个就可以通过排序+树状数组解决
————————————————
大致就是,我们可以先处理出区间内每一对(i,j)倍数对,然后按i从大到小排序
排序介绍:按i排序,从大到小,再让添加在前,询问在后,例如,我们区间(1,2,4,6)
然后倍数对就有(1 2,1 3 ,1 4,2 4)我们让2 4排前边,这样就可以一个个当成点来使用树状数组,这样保证先询问到就是刚加进去的
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 1e5+15;
const int maxm = 2e6+26;
int n,m,a[maxn],pos[maxn];
struct node{ //f标记是倍数对还是询问,id标识询问的编号
int f,l,r,id;
}p[maxm];
bool cmp(node a,node b){
if(a.l == b.l) return a.f < b.f;
return a.l > b.l;
}
inline int lowbit(int x){
return x & (-x);
}
LL tr[maxn],ans[maxn];
void updata(int x,int val){
while(x <= n){tr[x] += val; x += lowbit(x);}
}
LL query(int x){
LL res = 0;
while(x){res += tr[x]; x -= lowbit(x);}
return res;
}
int main(){
scanf("%d %d",&n,&m); int cnt = m;
for(int i = 1;i <= n; ++i) scanf("%d",a+i),pos[a[i]] = i;
for(int i = 0;i < m; ++i) scanf("%d %d",&p[i].l,&p[i].r),p[i].f=1,p[i].id=i;
for(int i = 1;i <= n; ++i){
for(int j = a[i]*2;j <= n; j+=a[i]){
if(pos[a[i]] < pos[j]) p[cnt++] = (node){0,pos[a[i]],pos[j],0};
else p[cnt++] = (node){0,pos[j],pos[a[i]],0};
}
}
sort(p,p+cnt,cmp);
for(int i = 0;i < cnt; ++i){
if(p[i].f == 1) ans[p[i].id] = query(p[i].r);
else updata(p[i].r,1);
}
for(int i = 0;i < m; ++i) cout << ans[i] << '\n';
return 0;
}

【树状数组】2019徐州网络赛 query的更多相关文章

  1. ICPC 2019 徐州网络赛

    ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...

  2. query 2019徐州网络赛(树状数组)

    query \[ Time Limit: 2000 ms \quad Memory Limit: 262144 kB \] 题意 补题才发现比赛的时候读了一个假题意.... 给出长度为 \(n\) 的 ...

  3. 2019徐州网络赛 I.query

    这题挺有意思哈!!!看别人写的博客,感觉瞬间就懂了. 这道题大概题意就是,给一串序列,我们要查找到l-r区间内,满足min(a[ i ],a[ j ]) = gcd(a[ i ],a[ j ]) 其实 ...

  4. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  5. 树状数组 LA 4329 亚洲赛北京赛区题

    复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...

  6. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

  7. 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...

  8. 2019徐州网络赛 I J M

    I. query 比赛时候没有预处理因子疯狂t,其实预处理出来因子是\(O(nlog(n))\)级别的 每个数和他的因子是一对偏序关系,因此询问转化为(l,r)区间每个数的因子在区间(l,r)的个数 ...

  9. 2019 徐州网络赛 M Longest subsequence t

    对于答案来说,一定是 前 i-1 个字符和 t的前 i 个一样,然后第 i 个字符比 t的 大 \(i\in [1,m]\) 前缀为t,然后长度比t长 对于第一种情况,枚举这个 i ,然后找最小的 p ...

随机推荐

  1. June 16th, 2019. Week 25th, Sunday.

    I can fly higher than an eagle, for you are the wind beneath my wings. 我之所以能飞得比鹰还高,是因为有你做我羽翼下的风. You ...

  2. vscode相关设置

    一.如何设置vs每次点击文件打开新的窗口: 1.打开设置 2.搜索:"workbench.editor.enablePreview" 3.设置"workbench.edi ...

  3. dom元素上添加断点(使用dom breakpoint找到修改属性的javascript代码)

    使用dom breakpoint能快速找到修改了某一个dom element的JavaScript code位于何处.在Chrome development tool里,选中想要inspect的dom ...

  4. eclipse强行停止buliding workspace

    使用Eclipse的过程中可能会遇到buliding workspace卡在一半走不动的情况. 出现这个情况往往是因为Eclipse太调皮了,需要拉出去打屁股,打一顿就好了. 开玩笑的,事实上出现这个 ...

  5. 数据库——SQL-SERVER练习(6) 数据库安全性

    一.实验准备 (1)运行SQL-SERVER服务管理器, 启动服务(2)运行查询分析器,  以DBA身份登录数据库服务器:      用户名sa,  密码123456(3)打开CREATE-TABLE ...

  6. Web前端——css

    css 推荐的样式编写顺序: Positioning:定位 Box model:盒子模型.大小等 Typographic:文字系列.排印等 Visual:可视化.背景等 Misc:其它混杂模式 居中 ...

  7. C# shell32.dll 的用法

    1 首先要使用shell32  请在项目引用中添加shell32.dll 的引用  (备注:该引用是系统dll文件 在C:\Windows\System32  目录下 可以自行拷贝到项目中) priv ...

  8. vue-基本动画

    不使用动画 <div id="app"> <input type="button" value="toggle" @cli ...

  9. TreeMap源码分析,看了都说好

    概述 TreeMap也是Map接口的实现类,它最大的特点是迭代有序,默认是按照key值升序迭代(当然也可以设置成降序).在前面的文章中讲过LinkedHashMap也是迭代有序的,不过是按插入顺序或访 ...

  10. MySQL学习——操作数据库

    MySQL学习——操作数据库 摘要:本文主要学习了使用DDL语句操作数据库的方法. 创建数据库 语法 create database [if not exists] 数据库名 [default] ch ...