Codeforces301D. Yaroslav and Divisors
题意: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的更多相关文章
- codeforces 301D Yaroslav and Divisors(树状数组)
Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he ...
- Yaroslav and Divisors
Codeforces Round #182 (Div. 1) D:http://codeforces.com/contest/301/problem/D 题意:给一个1-n,n个数的序列,然后查询一个 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #182 (Div. 1 + Div. 2)
A. Eugeny and Array \(r-l+1\)是奇数时,和显然无法为0. 奇数的情况需要判断-1和1的个数是否大于等于长度的一半. B. Eugeny and Play List 模拟. ...
- 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 ...
- 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 ...
- Divisors
计算小于n的数中,约数个数最多的数,若有多个最输出最小的一个数. http://hihocoder.com/problemset/problem/1187 对于100有 60 = 2 * 2 * 3 ...
- Xenia and Divisors
Xenia and Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hihocoder1187 Divisors
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given an integer n, for all integers not larger than n, f ...
随机推荐
- 使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网)
使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网) 一,前期准备 1,Java IDE(Eclipse)与JDK的安装与配置jdk-15.0.1-免配置路径版提取码:earu免安装版 ...
- Docker踩过的坑
前言 主要是记录Docker遇到的坑,更多的是因为自己的粗心大意,以此警示 正文 Dockerfile里的RUN 某一次把启动服务的命令写在了 Dockerfile 中,后来发现服务一直拉不起来. 原 ...
- MySQL常用的数据类型和字段属性
数据类型 数值 tinyint 十分小的数据 1个字节 smallint 较小的数据 2个字节 mediumint 中等大小的数据 3个字节 int 标准的整数 4个字节 常用 bigint 较大的数 ...
- 【Linux】awk想打印制定列以后的所有列
今天偶然研究awk,有一个文件,文件内容是全篇的1 2 3 4 5 6 7 8 9 0 现在想打印除了第一列意外的所有列 文件内容: [root@localhost ~]# cat test.txt ...
- Xctf攻防世界—crypto—Normal_RSA
下载压缩包后打开,看到两个文件flag.enc和pubkey.pem,根据文件名我们知道应该是密文及公钥 这里我们使用一款工具进行解密 工具链接:https://github.com/3summer/ ...
- centos7安装宝塔面板
在终端下执行如下命令 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.s ...
- 如何在 Blazor WebAssembly中 使用 功能开关
微软Azure 团队开发的 功能管理 (Feature Management) 包 Microsoft.FeatureManagement可用于实现 功能开关,可以通过 功能开关 特性动态的改变应用程 ...
- 前端知识(一)04 Vue.js入门-谷粒学院
目录 一.介绍 1.Vue.js 是什么 2.初识Vue.js 二.基本语法 1.基本数据渲染和指令 2.双向数据绑定 3.事件 4.修饰符 5.条件渲染 6.列表渲染 7.实例生命周期 一.介绍 1 ...
- 关于springboot项目通过jar包启动之后无法读取项目根路径静态资源
在一次项目开发过程中,项目根路径下存放了一张图片,生成二维码的时候调用了该图片作为二维码的logo,在windows环境下二维码可以正常生成,但是部署到生产测试环境之后二维码生成报错,FileNotF ...
- 配接Cisco设备