[美团 CodeM 初赛 Round A]数列互质
题目大意:
给出一个长度为n的数列a1,a2,a3,...,an,以及m组询问(li,ri,ki),求区间[li,ri]中有多少数在该区间中的出现次数与ki互质。
思路:
莫队。
f[i]记录数字i出现的次数,用一个链表记录f[i]的出现次数。
一开始没用链表,用map,在SimpleOJ上随便A,但是在LOJ上只有50分。
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,M=;
int a[N],f[N],ans[M],block;
struct Question {
int l,r,k,id;
bool operator < (const Question &another) const {
return l/block==another.l/block?r/block<another.r/block:l/block<another.l/block;
}
};
Question q[M];
struct List {
int val[N],last[N],next[N],end;
int &operator [] (const int &p) {
return val[p];
}
void insert(const int &x) {
next[end]=x;
last[x]=end;
end=x;
}
void erase(const int &x) {
if(x==end) end=last[x];
next[last[x]]=next[x];
last[next[x]]=last[x];
val[x]=last[x]=next[x]=;
}
};
List list;
inline void insert(const int &x) {
if(f[x]&&!--list[f[x]]) list.erase(f[x]);
if(!list[++f[x]]) list.insert(f[x]);
list[f[x]]++;
}
inline void erase(const int &x) {
if(!--list[f[x]]) list.erase(f[x]);
if(--f[x]) {
if(!list[f[x]]) list.insert(f[x]);
list[f[x]]++;
}
}
int gcd(const int &a,const int &b) {
return b?gcd(b,a%b):a;
}
int main() {
const int n=getint(),m=getint();
block=sqrt(m);
for(register int i=;i<=n;i++) a[i]=getint();
for(register int i=;i<m;i++) {
q[i]=(Question){getint(),getint(),getint(),i};
}
std::sort(&q[],&q[m]);
for(register int i=,l=,r=;i<m;i++) {
while(l>q[i].l) insert(a[--l]);
while(r<q[i].r) insert(a[++r]);
while(l<q[i].l) erase(a[l++]);
while(r>q[i].r) erase(a[r--]);
for(register int j=list.end;j;j=list.last[j]) {
if(gcd(j,q[i].k)==) ans[q[i].id]+=list[j];
}
}
for(register int i=;i<m;i++) {
printf("%d\n",ans[i]);
}
return ;
}
[美团 CodeM 初赛 Round A]数列互质的更多相关文章
- #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队
#6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...
- 「美团 CodeM 初赛 Round A」试题泛做
最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...
- Loj #6164. 「美团 CodeM 初赛 Round A」数列互质
link : https://loj.ac/problem/6164 莫队傻题,直接容斥做. #include<bits/stdc++.h> #define maxn 100005 #de ...
- loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd
LINK:#6177.美团 送外卖2 一道比较传统的状压dp题目. 完成任务 需要知道自己在哪 已经完成的任务集合 自己已经接到的任务集合. 考虑这个dp记录什么 由于存在时间的限制 考虑记录最短时间 ...
- 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp
题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...
- [美团 CodeM 初赛 Round A]最长树链
题目大意: 给你一棵带点权的树,找出一个最长的树链满足链上点权的最大公因数不为1. 思路: 暴力DP. 对于每个点,记录一下以这个点为一个端点的所有链的最大公因数及长度. 然后暴力转移一下,时间复杂度 ...
- 「美团 CodeM 初赛 Round A」最长树链
题目描述 Mr. Walker 最近在研究树,尤其是最长树链问题.现在树中的每个点都有一个值,他想在树中找出最长的链,使得这条链上对应点的值的最大公约数不等于1.请求出这条最长的树链的长度. 输入格式 ...
- 【填坑】loj6159. 「美团 CodeM 初赛 Round A」最长树链
水一水 枚举各个质数,把是这个数倍数的点留下,跑直径,没了 #include <bits/stdc++.h> using namespace std; int h,t,n,p,q,M,N; ...
- LiberOJ#6178. 「美团 CodeM 初赛 Round B」景区路线规划 概率DP
题意 游乐园被描述成一张 n 个点,m 条边的无向图(无重边,无自环).每个点代表一个娱乐项目,第 i 个娱乐项目需要耗费 ci 分钟的时间,会让小 y 和妹子的开心度分别增加 h1i ,h2i ,他 ...
随机推荐
- Codeforces Round #478 C. Valhalla Siege
C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- js 加法运算
搜集网友的各种解决办法: 1.parseInt(),parseFloat()等字符串转换函数 2.eval(执行加法的表达式) 3.a-(-b) 因为减法只有算术运算意义 a*1+b a为字符串 a ...
- Cannot read property 'resetFields' of undefined 问题及引申
问题描述: 使用element开发我的后台系统,编辑和新增使用了同一个弹出框<el-dialog><el-form></el-form></el-dialog ...
- DP---背包问题
http://www.hawstein.com/posts/dp-knapsack.html http://www.cnblogs.com/wwwjieo0/archive/2013/04/01/29 ...
- python中orm框架学习
安装sqlalchemy pip3 install sqlalchemy 创建表结构: from sqlalchemy import Column,String,create_engine from ...
- 【bzoj3926- [Zjoi2015]诸神眷顾的幻想乡】广义sam
题意:给定一棵树,每个节点有一个颜色,问树上有多少种子串(定义子串为某两个点上的路径),保证叶子节点数<=20.n<=10^5 题解: 叶子节点小于等于20,考虑将每个叶子节点作为根把树给 ...
- bzoj 1293 贪心
首先我们可以将这道题看成一个数轴,数轴其中的某些点存在一些颜色,我们要选取最短的一段,使这段存 在所有颜色,那么我们使用指针i,j表示在j-i位置中包含的颜色,那么初值是0,0,我们先i++,同时添加 ...
- nginx启动脚本(class练习)
说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...
- web前端 html/css总结点
1.html块级.内联<img src="" alt="图片未加载提示" title="鼠标悬浮提示"><a href=& ...
- SuSE Linux10.1 网络设置以及和主机通信end
设置步骤如下: 1.首先判断VMware Bridge Protocol协议是否已经安装.在本地连接的属性中可以查看. 2.虚拟机设置为bridged 3. 虚拟机-Edit-Virtual Netw ...