query 2019徐州网络赛(树状数组)
query
\]
题意
补题才发现比赛的时候读了一个假题意....
给出长度为 \(n\) 的排列,在给出 \(m\) 次询问,每次询问有一对 \(l、r\),问满足 \(min(ai, aj) = gcd(ai, aj)\) 的 \(pair(i, j)\) 对数。
思路
考虑离线做
先把每个数出现的位置记录下来,然后预处理出所有的 \(pair\)。
对于一个数字 \(x\),其满足条件的另一个数有 \(\sum_{k=2}^{\lfloor\frac{n}{x}\rfloor}kx\),那么就可以预处理每一个 \(x\),找出所有的 \(kx\),记录 \(x\) 和 \(kx\) 的位置,就是一对 \(pair\)。
保存一对 \(pair\) 时,将信息保存在较右的位置上。
那么我们就可以每次更新一个位置,更新时候将以这个位置为右端点的所有左端点释放出来,表示存在一个答案。
然后边更新边查询,当某个询问的右端点正好是我现在刚刚更新完的点时,由于已经更新出来的所有 \(pair\) 的右端点都在询问的 \(r\) 左侧,那么只要保证这些 \(pair\) 的左端点都在询问的 \(l\) 右侧就可以,那么我就可以更新 \(pair\) 的时候,更新在左侧,然后用树状数组来查询区间内的和。
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : Mon 09 Sep 2019 11:49:25 PM CST
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
struct Node {
int l, r, id;
bool operator < (Node a) const {
return r<a.r;
}
} node[maxn];
int a[maxn], p[maxn];
int ans[maxn];
vector<int> vv[maxn];
int sum[maxn];
void update(int p) {
for(int i=p; i<=n; i+=lowbit(i)) {
sum[i] ++;
}
}
int query(int p) {
int ans = 0;
for(int i=p; i>=1; i-=lowbit(i)) {
ans += sum[i];
}
return ans;
}
int main() {
// freopen("in", "r", stdin);
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) {
scanf("%d", &a[i]);
p[a[i]] = i;
vv[i].clear();
}
for(int i=1; i<=n; i++) {
for(int j=i+i; j<=n; j+=i) {
vv[max(p[i], p[j])].pb(min(p[i], p[j]));
}
}
for(int i=1; i<=m; i++) {
scanf("%d%d", &node[i].l, &node[i].r);
node[i].id = i;
}
sort(node+1, node+1+m);
int ask = 1;
for(int i=1; i<=n; i++) {
if(ask > m) break;
for(auto j : vv[i]) update(j);
while(ask<=m && node[ask].r == i) {
ans[node[ask].id] = query(i) - query(node[ask].l-1);
ask++;
}
}
for(int i=1; i<=m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
query 2019徐州网络赛(树状数组)的更多相关文章
- Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组
Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...
- ICPC 2019 徐州网络赛
ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...
- 【树状数组】2019徐州网络赛 query
(2)首先成倍数对的数量是nlogn级别的,考虑每一对[xL,xR](下标的位置,xL < xR)会对那些询问做出贡献,如果qL <= xL && qR >= xR, ...
- 2019徐州网络赛 I.query
这题挺有意思哈!!!看别人写的博客,感觉瞬间就懂了. 这道题大概题意就是,给一串序列,我们要查找到l-r区间内,满足min(a[ i ],a[ j ]) = gcd(a[ i ],a[ j ]) 其实 ...
- 19市赛 树状数组 第k个小的糖果
int find_kth(int k) { , cnt = , i; ; i >= ; i--)/ { ans += ( << i); if (ans >= maxn|| cn ...
- 2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...
- 2019徐州网络赛 I J M
I. query 比赛时候没有预处理因子疯狂t,其实预处理出来因子是\(O(nlog(n))\)级别的 每个数和他的因子是一对偏序关系,因此询问转化为(l,r)区间每个数的因子在区间(l,r)的个数 ...
- 2019 徐州网络赛 M Longest subsequence t
对于答案来说,一定是 前 i-1 个字符和 t的前 i 个一样,然后第 i 个字符比 t的 大 \(i\in [1,m]\) 前缀为t,然后长度比t长 对于第一种情况,枚举这个 i ,然后找最小的 p ...
- 2019徐州网络赛 H.function
题意: 先有\(n=p_1^{k_1}p_2^{k_2}\cdots p_m^{k_m}\),定义\(f(n)=k_1+k_2+\cdots+k_m\). 现在计算 \[ \sum_{i=1}^nf( ...
随机推荐
- ffmpeg Operation not permitted 报错的解决过程记录
问题重现 由于视频的录制过程出现了一些小问题,需要重新将视频文件切割和合并,找了几个视频编辑软件来做这个事情,最终的结果都不是特别满意,当时已经挺晚的了,本来打算上床睡觉第二天再去想辙,从椅子上起身的 ...
- LeetCode 28:实现strStr() Implement strStr()
爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needl ...
- SQL -------- 简单的增删改查
sql 结构化查询语言,一种ansi 的标准计算机语言,为了访问数据库 可以做什么:可以对数据库 和表进行创建于删除, 对表里面的数据进行增删改查. 也可以创建存储过程和视图,对表设置权限 RDBM ...
- Redis(四)Pub/Sub
发布与订阅 Pub/Sub模式应该非常熟悉,在现实应用中被广泛的使用.如:微博中关注某个号,这个号有发新博时,关注的都会收到:github上watch了某个项目,当有issue时,就会发邮件. Red ...
- digital clock based C
/********************************** * Name : timeDisplay.cpp * Purpose: Display digital clock accord ...
- .net core+topshelf+quartz创建windows定时任务服务
.net core+topshelf+quartz创建windows定时任务服务 准备工作 创建.net core 控制台应用程序,这里不做过多介绍 添加TopShelf包:TopShelf: 添加Q ...
- webapi处理OPTIONS请求
报错1信息 Access to XMLHttpRequest at 'http://localhost:4445/api/v/getmsg' from origin 'http://localhost ...
- Python - 解释器 - 第三天
Python解释器 安装好Python3.x之后,我们可以使用文本文件去编写Python代码,编写完成后将扩展名改成.py结尾的文本文件. 想要执行编写好的.py文件就需要用到Python解释器. 解 ...
- Java面试必问通信框架NIO,原理详解
NIO 流与块 通道与缓冲区 缓冲区状态变量 文件 NIO 实例 选择器 套接字 NIO 实例 内存映射文件 NIO与IO对比 Path Files NIO 新的输入/输出 (NIO) 库是在 JDK ...
- Java集合Collection基本方法
jdk1.7 api中的方法摘要: 参考java集合大全图:https://www.cnblogs.com/xkzhangsanx/p/10889114.html Collection为List.Se ...