题意:2e5的全排列 每次询问一个区间有多少对数 满足一个数是另一个数的倍数

题解:考虑离线来做 看到有个说法说 在处理有两种约束的问题时 一般用数据结构边插入边询问的方式

   这个题正是如此 我们用sum_i表示处理完1-i时所有的对数 那么可以用sum_r - sum_l-1得到一个答案

   这个答案显然是多包含了一部分 一个数在前面 他的倍数在区间里这种方式

   那么我们在插入到区间左端点的时候就可以很巧妙的 减去这一段贡献 插入到区间右端点的时候 加上一段贡献

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5; int n, m;
int a[MAXN], b[MAXN];
int l[MAXN], r[MAXN];
int sum[MAXN];
int ans[MAXN];
vector<int> li[MAXN], ri[MAXN]; void add(int x) {
for(int i = x; i <= n; i += (i & -i)) sum[i]++;
} int ask(int x) {
int res = 0;
for(int i = x; i >= 1; i -= (i & -i)) res += sum[i];
return res;
} int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]), b[a[i]] = i;
for(int i = 1; i <= m; i++) {
scanf("%d%d", &l[i], &r[i]);
li[l[i]].push_back(i);
ri[r[i]].push_back(i);
} for(int i = 1; i <= n; i++) {
for(int j = 0; j < li[i].size(); j++) ans[li[i][j]] -= ask(r[li[i][j]]) - ask(i - 1);
for(int j = 1; j * a[i] <= n; j++) add(b[j * a[i]]);
for(int j = 0; j < ri[i].size(); j++) ans[ri[i][j]] += ask(i) - ask(l[ri[i][j]] - 1);
}
for(int i = 1; i <= m; i++) cout << ans[i] << endl;
return 0;
}

Codeforces301D. Yaroslav and Divisors的更多相关文章

  1. codeforces 301D Yaroslav and Divisors(树状数组)

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

  2. Yaroslav and Divisors

    Codeforces Round #182 (Div. 1) D:http://codeforces.com/contest/301/problem/D 题意:给一个1-n,n个数的序列,然后查询一个 ...

  3. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  4. Codeforces Round #182 (Div. 1 + Div. 2)

    A. Eugeny and Array \(r-l+1\)是奇数时,和显然无法为0. 奇数的情况需要判断-1和1的个数是否大于等于长度的一半. B. Eugeny and Play List 模拟. ...

  5. codeforces 27E Number With The Given Amount Of Divisors

    E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...

  6. HDU - The number of divisors(约数) about Humble Numbers

    Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence ...

  7. Divisors

    计算小于n的数中,约数个数最多的数,若有多个最输出最小的一个数. http://hihocoder.com/problemset/problem/1187 对于100有 60 = 2 * 2 * 3 ...

  8. Xenia and Divisors

    Xenia and Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. hihocoder1187 Divisors

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...

随机推荐

  1. Azure Terraform(五)利用Azure DevOps 实现自动化部署基础资源

    一,引言 上一篇我们结合学习 Azure Traffic Manger 的内容,做了一个负载均衡的基础设施架构.通过 Terraform 部署执行计划,将整个 Azure Traffic Manage ...

  2. 【Spring】Spring的事务管理 - 2、声明式事务管理(实现基于XML、Annotation的方式。)

    声明式事务管理 文章目录 声明式事务管理 基于XML方式的声明式事务 基于Annotation方式的声明式事务 简单记录 - 简单记录-Java EE企业级应用开发教程(Spring+Spring M ...

  3. 2019 Java开发利器Intellij IDEA安装、配置和使用

    进入Intellij IDEA的官网,选择电脑对应的合适版本进行下载,这儿我选择的是Intellij IDEA的社区版,安装旗舰版可去网上找相应的教程. Intellij IDEA的官网:https: ...

  4. 【Linux】 多个会话同时执行命令后history记录不全的解决方案

    基本认识 linux默认配置是当打开一个shell终端后,执行的所有命令均不会写入到~/.bash_history文件中,只有当前用户退出后才会写入,这期间发生的所有命令其它终端是感知不到的. 问题场 ...

  5. python异步回调顺序?是否加锁?

    话不多说,直接上代码: import time from functools import partial from concurrent.futures.process import Process ...

  6. 24V转3.3V稳压芯片,高效率同步降压DC-DC变换器3A输出电流

    PW2312是一个高频,同步,整流,降压,开关模式转换器与内部功率MOSFET.它提供了一个非常紧凑的解决方案,以实现1.5A的峰值输出电流在广泛的输入电源范围内,具有良好的负载和线路调节. PW23 ...

  7. 使用Logback日志

    使用Logback日志 spring boot内部使用Logback作为日志实现的框架. Logback和log4j非常相似,如果你对log4j很熟悉,那对logback很快就会得心应手. logba ...

  8. 手动添加Ini4idea,解决pycharm无法打开ini文件

    1. 查看本地pycharm的版本号 help -> about 2. 进入官网:http://plugins.jetbrains.com,选中pycharm及相关版本,搜索ini,切到版本,下 ...

  9. CISCO 如何重置3850交换机密码

    SUMMARY STEPS: Connect a terminal or PC to the switch. Set the line speed on the emulation software ...

  10. CPU飙高,系统性能问题如何排查?

    CPU飙高,系统性能问题如何排查? 原创 雍雍 阿里技术 2020-09-29 https://mp.weixin.qq.com/s/fzLcAkYwKhj-9hgoVkTzaw