[美团 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 ,他 ...
随机推荐
- codeforces2015ICL,Finals,Div.1#J Ceizenpok’s formula 扩展Lucas定理 扩展CRT
默默敲了一个下午,终于过了, 题目传送门 扩展Lucas是什么,就是对于模数p,p不是质数,但是不大,如果是1e9这种大数,可能没办法, 对于1000000之内的数是可以轻松解决的. 题解传送门 代码 ...
- JavaBean定义、JSP中使用以及内省操作
Apache commons 一系列的开源工具室非常值得学习的实现. 一 JavaBean定义 JavaBean是一种可重复使用.且跨平台的软件组件.JavaBean可分为两种:一种是 ...
- IE设置信任站点和安全级别(bat文件)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges\Range1 ...
- 如何打开小米,oppo,华为等手机的系统应用的指定页面
如题,拿Oppo 手机做个示例,小米 华为也是如此. 在编写Android应用的时候,我们经常会有这样的需求,我们想直接打开系统应用的某个页面.比如在Oppo R9 手机上我们想打开某个应用的通知管理 ...
- 小白科普之JavaScript的DOM模型
微信公众号“前端大全”推送了一篇名为“通俗易懂的来讲讲DOM”的文章,把javascript原生DOM相关内容讲解的很详细.仔细读了一遍,觉得整理总结的不错,对自己也很使用,所以把内容整理过来,并根据 ...
- CTL_CODE说明
DeviceIoControl函数的第二个参数IoControlCode就是由CTL_CODE宏定义的,下边我们可以了解一下CTL_CODE的内容. CTL_CODE:用于创建一个唯一的32位系统I/ ...
- Method and apparatus for providing total and partial store ordering for a memory in multi-processor system
An improved memory model and implementation is disclosed. The memory model includes a Total Store Or ...
- spin_lock浅析【转】
转自:http://blog.csdn.net/frankyzhangc/article/details/6569475 版权声明:本文为博主原创文章,未经博主允许不得转载. 今天我们详细了解一下sp ...
- ARM 中断状态和SVC状态的堆栈切换 (异常)【转】
转自:http://blog.csdn.net/edwardlulinux/article/details/9261393 版权声明:本文为博主原创文章,未经博主允许不得转载. ARM 中断状态和SV ...
- 浅谈redux 中间件的原理
在使用redux管理异步数据流的时候,我们会使用中间件,以redux-thunk中间件为例,我们做一下分析: 首先是构建store,我们需要以下代码进行揉入中间件的类似creatStore函数的构造: ...