https://nanti.jisuanke.com/t/41391

这个题目没有很难想,比较暴力,但是要会算复杂度,不会算复杂度,就会觉得自己的算法会超时,实际上不会。

这个题目就是直接暴力求出每一个数的在1e5以内的所有的约数和倍数,然后更新,就像之前写过的E - No Pain No Game 线段树 离线处理 区间排序 一样

算一下复杂度,调和级数要用两次,一次来求约数一次来求倍数,复杂度都是n*logn 所以平均下来每一个的约数大约就是logn

因为有个两倍的关系所以就是2*logn

线段树的复杂度,每次更新是logn ,要遍历一次

所以总的复杂度就是2*n*logn*logn,这个肯定不会超时的。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
vector<int>num[maxn];
int a[maxn], vis[maxn], c[maxn], ans[maxn];
int n, m;
struct node
{
int l, r, id;
node(int l=,int r=,int id=):l(l),r(r),id(id){}
}ex[maxn];
bool cmp(node a,node b)
{
return a.r < b.r;
} int lowbit(int x)
{
return x & (-x);
} void update(int x,int k)
{
while (x <= n) {
c[x] += k;
x += lowbit(x);
}
} int getsum(int x)
{
int ans = ;
while (x > ) {
ans += c[x];
x -= lowbit(x);
}
return ans;
} int main()
{
for (int i = ; i < maxn; ++i) {
for (int j = *i; j < maxn; j += i) {
num[j].push_back(i);//先把每个数的因子有哪些打个表,由调和级数可知复杂度为o(nlog n)
}
}
for (int i = ; i < maxn; ++i) {
for (int j = * i; j < maxn; j += i) {
num[i].push_back(j);
}
}
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= m; i++) {
int l, r;
scanf("%d%d", &l, &r);
ex[i] = node(l, r, i);
}
memset(vis, -, sizeof(vis));
sort(ex + , ex + + m, cmp);
int now = ;
for (int i = ; i <= n; i++) {
int x = a[i];
for (int j = ; j < num[x].size(); j++) {
int y = num[x][j];
if (vis[y] != -) update(vis[y], );
}
vis[x] = i;
while (now <= m && i == ex[now].r) {
int res = getsum(ex[now].r) - getsum(ex[now].l - );
ans[ex[now].id] = res;
now++;
}
}
for (int i = ; i <= m; i++) printf("%d\n", ans[i]);
return ;
}

query 线段树 + 区间排序的更多相关文章

  1. 【bzoj2653】middle 可持久化线段树区间合并

    题目描述 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你一个长度为n的序列s.回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[ ...

  2. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. POJ 2528 ——Mayor's posters(线段树+区间操作)

    Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...

  4. 线段树区间合并优化dp——cf1197E(好)

    线段树优化dp的常见套路题,就是先按某个参数排序,然后按这个下标建立线段树,再去优化dp 本题由于要维护两个数据:最小值和对应的方案数,所以用线段树区间合并 /* dp[i]表示第i个套娃作为最内层的 ...

  5. Mayor's posters POJ - 2528 线段树区间覆盖

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

  6. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  7. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

  9. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

随机推荐

  1. k8s~helm镜像版本永远不要用latest

    对于容器编排工具k8s来说,你可以使用它规定的yaml格式的脚本,使用客户端kubectl来与k8s进行通讯,将你定义好的yaml部署脚本应用到k8s集群上,而这对yaml脚本一般来说都是很像的,就是 ...

  2. 【spring 国际化】springMVC、springboot国际化处理详解

    在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...

  3. matlab创建HDF5文件

    一.例子 1.创建写入 testdata = uint8(magic(5)); h5create('my_example.h5','/dataset1',size(testdata)); %创建 h5 ...

  4. python输出中文乱码

    首选项-浏览插件目录-USER文件夹 找到编译环境设置,编译方式为UTF-8编码 { "cmd": ["c:/Python36/python.exe",&quo ...

  5. E - Farthest Nodes in a Tree

    Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. Th ...

  6. LABEL和UUID

    基本用法 blkid 查看LABEL # blkid -s LABEL /dev/hda3: LABEL="/" /dev/hda1: LABEL="/boot1&quo ...

  7. BypassUAC

    BypassUAC 本篇主要介绍如何以ICMLuaUtil方式BypassUAC,主要内容如下: 过掉UAC提示框的方法总结 UACME项目 什么类型的COM interface可以利用? 如何快速找 ...

  8. Python - 生成随机验证码的3种实现方式

    生成6位随机验证码的3种实现方式如下: 1. 简单粗暴型:所有数字和字母都放入字符串: 2. 利用ascii编码的规律,遍历获取字符串和数字的字符串格式: 3. 引用string库. 方法1代码: i ...

  9. 负载均衡服务之HAProxy基础配置(四)

    前文我们聊了haproxy的状态页配置,状态页中显示各参数的含义,以及基于cookie做会话保持的配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12776 ...

  10. 使用STM8S i2c对TPS65987寄存器进行读写

    上图是TPS65987的i2c读写协议,和标准i2c协议有点出入,不过也不难理解,在读的时候i2c slave在发送数据过来之前会先发送1byte数据表示后面会有几个字节数据过来,在写的时候i2c h ...