题目大意:
  给出一个长度为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]数列互质的更多相关文章

  1. #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队

    #6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...

  2. 「美团 CodeM 初赛 Round A」试题泛做

    最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...

  3. Loj #6164. 「美团 CodeM 初赛 Round A」数列互质

    link : https://loj.ac/problem/6164 莫队傻题,直接容斥做. #include<bits/stdc++.h> #define maxn 100005 #de ...

  4. loj #6177. 「美团 CodeM 初赛 Round B」送外卖2 状压dp floyd

    LINK:#6177.美团 送外卖2 一道比较传统的状压dp题目. 完成任务 需要知道自己在哪 已经完成的任务集合 自己已经接到的任务集合. 考虑这个dp记录什么 由于存在时间的限制 考虑记录最短时间 ...

  5. 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp

    题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...

  6. [美团 CodeM 初赛 Round A]最长树链

    题目大意: 给你一棵带点权的树,找出一个最长的树链满足链上点权的最大公因数不为1. 思路: 暴力DP. 对于每个点,记录一下以这个点为一个端点的所有链的最大公因数及长度. 然后暴力转移一下,时间复杂度 ...

  7. 「美团 CodeM 初赛 Round A」最长树链

    题目描述 Mr. Walker 最近在研究树,尤其是最长树链问题.现在树中的每个点都有一个值,他想在树中找出最长的链,使得这条链上对应点的值的最大公约数不等于1.请求出这条最长的树链的长度. 输入格式 ...

  8. 【填坑】loj6159. 「美团 CodeM 初赛 Round A」最长树链

    水一水 枚举各个质数,把是这个数倍数的点留下,跑直径,没了 #include <bits/stdc++.h> using namespace std; int h,t,n,p,q,M,N; ...

  9. LiberOJ#6178. 「美团 CodeM 初赛 Round B」景区路线规划 概率DP

    题意 游乐园被描述成一张 n 个点,m 条边的无向图(无重边,无自环).每个点代表一个娱乐项目,第 i 个娱乐项目需要耗费 ci 分钟的时间,会让小 y 和妹子的开心度分别增加 h1i ,h2i ,他 ...

随机推荐

  1. 在WPF中应用弱事件模式

    http://www.cnblogs.com/rickiedu/archive/2007/03/15/676021.html 在wpf中应用弱事件模式        感谢VS 的Intellisens ...

  2. 前端面试:提升web性能

    1,减少HTTP请求数 A,从设计实现层简化页面 B,合理设置HTTP缓存 C,资源合并与压缩.如果可以的话,尽可能的将外部脚本,央视进行合并,多个合为一,css,javascript,image都可 ...

  3. 列出top中的pid

    #!/usr/bin/env python import os import string #方法1:通过字符串的isdigits来判断 #filelist = os.listdir('/proc') ...

  4. elementary os 5配置

    打开终端,执行以下步骤: 1.更新软件源 sudo apt update 2.安装add-apt-repository命令所在的软件包 sudo apt install software-proper ...

  5. python 学习笔记 多进程

    要让python程序实现多进程,我们先了解操作系统的相关知识 Unix/Linux操作系统提供了一个fork()系统调用,他非常特殊,普通的函数调用,调用一次,返回一次,但是fork调用一次, 返回两 ...

  6. HTTP===如何理解网关

    首先举个例子: 假设你的名字叫小不点(很小),你住在一个大院子里,你的邻居有很多小伙伴,父母是你的网关.当你想跟院子里的某个小伙伴玩,只要你在院子里大喊一声他的名字,他听到了就会回应你,并且跑出来跟你 ...

  7. 【POI2017||bzoj4726】Sabota?

    上学期putsnan过了一次,这学期认真写了一遍…… #include<bits/stdc++.h> #define N 500010 using namespace std; ]; ,n ...

  8. mybatis获取表信息,以及遍历ResultSet

    @RunWith(SpringRunner.class) @SpringBootTest public class BravolinksCrmServerApplicationTests { @Aut ...

  9. javascript字符串中包含特殊字符问题

    我们都知道,在javascript中,字符串写在单引号或者双引号之中.因为这种要求,我们有些时候一些需要的字符串不能够被javascript解析,如下: "We are "Huma ...

  10. Ubuntu 18.04 sublime text 3176 安装、汉化及配置中文输入

    转载自:https://blog.csdn.net/weixin_42508385/article/details/82152393 一.下载: 在https://www.sublimetext.co ...