Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he has m queries:

  • Query number i is represented as a pair of integers liri (1 ≤ li ≤ ri ≤ n).
  • The answer to the query li, ri is the number of pairs of integers qw (li ≤ q, w ≤ ri) such that pq is the divisor of pw.

Help Yaroslav, answer all his queries.

Input

The first line contains the integers n and m (1 ≤ n, m ≤ 2·105). The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). The following m lines contain Yaroslav's queries. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n).

Output

Print m integers — the answers to Yaroslav's queries in the order they appear in the input.

题目大意:给一个1~n的排列,有m个询问,问L到R之间有多少对数满足一个数是另一个数的约数。

思路:果然数学才是王道……

1~n个数,满足一个数是另一个数的约数的数对,一共有n/1 + n/2 + …… + n/n ≈ nlogn个(大概吧其实我也不会算),也就是32位够了

然后怎么计算L到R有多少对呢?本来想的是用1~R的对数减去1~L-1的对数,结果发现这样算的结果包含了一个属于1~L-1另一个属于L~R的合法对。

于是进一步思考,令x = 1~R的对数减去1~L-1的对数,y = 一个属于1~L-1另一个属于L~R的合法对数,答案ans = x - y。

这个好像没有办法在短时间内在线处理出来,于是采用离线处理。

i从1到n循环,对所有的query.L=i,减去sum[R] - sum[L-1],即上面所说的y(此时L~R的合法对还没被计算出来)。然后找出所有i的倍数,加到sum里面。再对所有的query.R=i,加上sum[R] - sum[L-1],即上面所说的x(此时L~R的合法对已经计算出来了)。

对于这种需要单点更新,区间求值的操作,树状数组可以满足你。

代码(498MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = ; int a[MAXN], pos[MAXN], tree[MAXN];
int n, m; int lowbit(int x) {
return x & (-x);
} int get_sum(int k) {
int ans = ;
while(k > ) {
ans += tree[k];
k -= lowbit(k);
}
return ans;
} void modify(int k, int val) {
while(k <= n) {
tree[k] += val;
k += lowbit(k);
}
} struct QUERY {
int id, L, R;
bool operator < (const QUERY &rhs) const {
return L < rhs.L;
}
} query[MAXN], query_t[MAXN]; int ans[MAXN]; int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
pos[a[i]] = i;
}
for(int i = ; i <= m; ++i) {
scanf("%d%d", &query[i].L, &query[i].R);
query_t[i].L = query[i].R;
query_t[i].R = query[i].L;
query[i].id = query_t[i].id = i;
}
sort(query + , query + m + );
sort(query_t + , query_t + m + );
for(int i = , j = , k = ; i <= n; ++i) {
while(j <= m && query[j].L == i) {
ans[query[j].id] -= get_sum(query[j].R) - get_sum(query[j].L - );
++j;
}
for(int p = a[i]; p <= n; p += a[i]) modify(pos[p], );
while(k <= m && query_t[k].L == i) {
ans[query_t[k].id] += get_sum(query_t[k].L) - get_sum(query_t[k].R - );
++k;
}
}
for(int i = ; i <= m; ++i) printf("%d\n", ans[i]);
}

codeforces 301D Yaroslav and Divisors(树状数组)的更多相关文章

  1. CodeForces 828E DNA Evolution(树状数组)题解

    题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...

  2. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  3. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  4. Codeforces 635D Factory Repairs【树状数组】

    又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...

  5. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  6. codeforces E. DNA Evolution(树状数组)

    题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...

  7. Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]

    题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...

  8. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

  9. codeforces 1288E. Messenger Simulator(树状数组)

    链接:https://codeforces.com/contest/1288/problem/E 题意:序列p的长度为n,初始序列为1 2 3 4 ...n,然后有m次操作,每次指定序列中一个数移动到 ...

随机推荐

  1. Vue.js与 ASP.NET Core 服务端渲染功能整合

    http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...

  2. string::size_type类型

    string::size_type类型 对于string中的size函数,size函数返回的是string对象的字符个数(长度),我们知道,对size()来说,返回一个int或者是一个unsigned ...

  3. CF1066CBooks Queries(数组的特殊处理)

    题意描述 您需要维护一个数据结构,支持以下三种操作: L id:在现在序列的左边插一个编号为id的物品 R id:在现在序列的右边插一个编号为id的物品 ? id:查询该点左面有几个元素,右面有几个元 ...

  4. linux下pip错误 ImportError: No module named 'pip_internal'

    wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate sudo python get-pip.py

  5. SpringBoot整合Mybatis,TypeAliases配置失败的问题

    SpringBoot整合Mybatis,TypeAliases配置失败的问题 问题描述 在应用MyBatis时,使用对象关系映射,将对象和Aliase映射起来. 在Mybatis的文档明确写出,如果你 ...

  6. SQL Server 2012 - 开窗函数

    -- 开窗函数:在结果集的基础上进一步处理(聚合操作) -- Over函数,添加一个字段显示最大年龄 SELECT * , MAX(StuAge) OVER ( ) MaxStuAge FROM db ...

  7. 中国软件大会上大快搜索入选中国数字化转型TOP100服务商

    大快搜索自荣获“2018中国大数据企业50强”殊荣,12月20日在由工信部指导,中国电子信息产业化发展研究院主办的2018中国软件大会上,大快搜索获评“2018中国大数据基础软件领域领军企业”称号,入 ...

  8. hadoop的自定义分组实现 (Partition机制)

    hadoop开发中我们会遇到类似这样的问题,比如 如何将不同省份的手机号分别输出到不同的文件中,本片文章将对hadoop内置的Partition类进行重写以解决这个问题. MapReduce的使用者通 ...

  9. Python学习 :json、pickle&shelve 模块

    数据交换格式 json 模块 json (JavaScript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读.尽管 json 是JavaScript的一个子 ...

  10. python remove跟pop的区别

    remove根据值来删除 li=[1,2,3,4] li.pop[0] li.remove['] 或者是a=li[-1]li.remove(a) pop是根据索引来删除