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 ...
随机推荐
- 数据库表空间收缩之pg_squeeze,pg_repack
数据库表空间收缩之pg_squeeze,pg_repack 目录 数据库表空间收缩之pg_squeeze,pg_repack pg_squeeze1.2 原理 优点 安装 使用 pgstattuple ...
- oracle 11.2.0.1.0 升级 11.2.0.4.0 并 patch 到11.2.0.4.7
升级步骤: (1) 备份数据库 (2) 运行patchset,升级oracle 软件 (3) 准备新的ORACLE_HOME (4) 运行dbua 或者脚本升级实例 (5) ...
- (二)数据源处理5-excel数据转换实战(上)
把excel_oper02.py 里面实现的:通过字典的方式获取所有excel数据.放进utils: ️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ utils: def get_al ...
- 原生工程接入Flutter实现混编
前言 上半年我定的OKR目标是帮助团队将App切入Flutter,实现统一技术栈,变革成多端融合开发模式.Flutter目前是跨平台方案中最有潜力实现我们这个目标的,不管是Hybird还是React ...
- 翻译 - ASP.NET Core 基本知识 - 中间件(Middleware)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0 中间件是集成 ...
- Navicat linux 官方最新版安装破解
我们直接去官网下载linux版navicat 下载好之后执行命令打开软件 chmod +x navicat15-premium-cs.AppImage ./navicat15-premium-cs.A ...
- ROS教程(二):创建工作空间(图文)
ros教程:创建工作空间 目录 前言 一.工作空间? 二.创建一个工作空间 1.创建目录 2.编译 前言 使用catkin创建一个工作空间 一.工作空间? 在ROS系统下,我们所有的项目都放在一个工作 ...
- 云原生流水线 Argo Workflow 的安装、使用以及个人体验
注意:这篇文章并不是一篇入门教程,学习 Argo Workflow 请移步官方文档 Argo Documentation Argo Workflow 是一个云原生工作流引擎,专注于编排并行任务.它的特 ...
- 在原生开发中控制HTML5视频!
在原生开发中控制HTML5视频! PC端 视频如何自动播放! 在video标签中添加 autoplay + muted(静音属性!) 温馨提醒: video是一个块级元素! 但是唯一的缺陷就是视频没有 ...
- python RecursionError: maximum recursion depth exceeded while calling
import copyimport sys # 导入sys模块sys.setrecursionlimit(8192) # 将默认的递归深度修改为r = sys.getrecursionlimit()_ ...