传送门

题意:

一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线


吐槽:

本来一道可以离散的莫队我非要强制在线用分块做;上午就开始写了然后发现思路错了...;改 下午继续写....然后发现看大了数据范围卡空间了...;改 然后又发现好多bug...;再改 然后发现TLE了... ;改块的大小....可恶又卡空间了.... ;改short...可恶溢出了;改unsigned short....可恶n总共才1e5怎么练unsigned short也溢出了.....; 开O2...还不行....;然后发现之前把块的大小和数量搞反了....;继续改块的大小再加上有理有据对本题特性的vector优化.....终于A了.................

题解:

一开始想成已经知道k预处理f不用第三维了(md那还用分块干什么)

对出现次数$>S$和$\le S$的分开讨论

预处理$f[i][j][k]$为块i到块j出现次数$[k,S]$的有几种

$s[i][j]$为前i块颜色j出现了几次

询问的时候

两边不完整的块暴力枚举

$>S$的部分不超过$\frac{N}{S}$种,单独暴力枚举(注意如果两边枚举过了就不能重复枚举了)

$[k,S]$的部分直接用预处理的f

#pragma GCC optimize ("O2")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1e5+, M=, S=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n,Q,col,a[N],u,v,k;
int cou[N], big[N], tot, mark[N];bool biiig[N];
struct edge{int v,ne;}e[N<<];
int cnt,h[N];
inline void ins(int u,int v){
e[++cnt]=(edge){v,h[u]}; h[u]=cnt;
e[++cnt]=(edge){u,h[v]}; h[v]=cnt;
}
int dfc,L[N],R[N];
int t[N];
void dfs(int u,int fa){
L[u]=++dfc; a[dfc]=t[u];
for(int i=h[u];i;i=e[i].ne)
if(e[i].v!=fa) dfs(e[i].v, u);
R[u]=dfc;
} int block,m,pos[N];
struct _blo{int l,r;}b[M];
void ini(){
//block=sqrt(n);
block=;
m=(n-)/block+;
for(int i=;i<=n;i++) pos[i]=(i-)/block+;
for(int i=;i<=m;i++) b[i].l=(i-)*block+, b[i].r=i*block;
b[m].r=n;
} struct Block{
int f[M][M][S], c[N], s[M][N]; void Set0(int x){
for(int i=;i<=col;i++) s[x][i]=s[x-][i];
for(int i=b[x].l; i<=b[x].r; i++) s[x][a[i]]++;
} void Set1(int x){
for(int t=x;t<=m;t++){
for(int i=b[t].l; i<=b[t].r; i++) if(!biiig[ a[i] ]) c[a[i]]++;
for(int i=b[t].l; i<=b[t].r; i++) if(!biiig[ a[i] ] && c[a[i]]>){
int _=s[t-][a[i]] - s[x-][a[i]];
f[x][t][ _+c[a[i]] ]++;
f[x][t][ _ ]--;
c[a[i]]=;
}
for(int i=block; i>=; i--) f[x][t][i]+=f[x][t][i+];
for(int i=; i<=block; i++) f[x][t][i]+=f[x][t-][i];
}
} int Que(int l,int r,int k){
int pl=pos[l], pr=pos[r];
int ans=;
if(pl==pr){
for(int i=l; i<=r; i++) c[a[i]]++;
for(int i=l; i<=r; i++) if(c[a[i]]>) ans+= c[a[i]]>=k, c[a[i]]=;
}else{
for(int i=; i<=tot; i++) mark[ big[i] ]=;
vector<int> v;
int *rr=s[pr], *ll=s[pl-];
for(int i=l; i<=b[pl].r; i++){
mark[ a[i] ]=;
if(rr[a[i]] - ll[a[i]]>=k)
c[a[i]]++, v.push_back(a[i]);
}
for(int i=b[pr].l; i<=r; i++){
mark[ a[i] ]=;
if(rr[a[i]] - ll[a[i]]>=k)
c[a[i]]++, v.push_back(a[i]);
} for(int i=; i<(int)v.size(); i++) if(c[v[i]]>){
int _=s[pr-][v[i]] - s[pl][v[i]];
if(biiig[ v[i] ]) ans+= _+c[v[i]]>=k;
else ans+= (_<k && _+c[v[i]]>=k);
c[v[i]]=;
} if(k<=block) ans+=f[pl+][pr-][k];
for(int i=;i<=tot;i++) if(!mark[ big[i] ])
ans+= s[pr-][big[i]] - s[pl][big[i]] >= k;
}
return ans;
}
}B; int main(){
// freopen("in","r",stdin);
n=read(); Q=read(); ini();
for(int i=;i<=n;i++) a[i]=t[i]=read(), col=max(col, a[i]), cou[a[i]]++;
for(int i=;i<n;i++) ins(read(), read());
dfs(,); for(int i=;i<=col;i++) if(cou[i]>block) big[++tot]=i, biiig[i]=;
for(int i=;i<=m;i++) B.Set0(i);
for(int i=;i<=m;i++) B.Set1(i); while(Q--){
u=read(); k=read();
printf("%d\n", B.Que(L[u], R[u], k) );
}
}

CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】的更多相关文章

  1. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  2. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  3. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  4. CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  5. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  6. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  7. SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)

    You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...

  8. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  9. SP1487 PT07J - Query on a tree III 主席树+dfs序

    Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...

随机推荐

  1. A. Vasya and Football

    A. Vasya and Football time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. DFS(dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2212 DFS Time Limit: 5000/2000 MS (Java/Others)    Me ...

  3. GDI绘制时钟效果,与系统时间保持同步,基于Winform

    2018年工作之余,想起来捡起GDI方面的技术,特意在RichCodeBox项目中做了两个示例程序,其中一个就是时钟效果,纯C#开发.这个CSharpQuartz是今天上午抽出一些时间,编写的,算是偷 ...

  4. nth-child()选择器小结

    之前也用到过nth-child,只是当时理解的不够透彻.今天回过头去看这个知识点时,发现了一个易错点. 浏览器支持情况: 所有主流浏览器都支持nth-child()选择器,除了IE8及更早的版本. 下 ...

  5. centos 6.4 mysql rpm 离线安装【备忘】

    离线状态下使用rpm的安装包进行mysql的安装,仅作备忘 准备工作: 官网下载mysql离线rpm安装包(这里就不演示了,拿现成的做演示) =================更新线 2018-01- ...

  6. Vue下路由History mode导致页面无法渲染的原因

    用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js ,我们已经可以通过组合组件来组成应用程序,当你要把 vue-router 添加进来,我们需要做的是,将组件( ...

  7. 基于VUE的九宫格抽奖功能

    HTML代码: <template> <div class="luckDraw"> <title-bar :title="title&quo ...

  8. IOS开发之UIView总结1

    太长了,请看 http://blog.csdn.net/xdrt81y/article/details/9128695 performSelector: performSelector:withObj ...

  9. eclipse中git解决冲突

    摘录自http://blog.csdn.net/rosten/article/details/17068285 1. 工程->Team->同步 2.从远程pull至本地,就会出现如下内容 ...

  10. 导入Mybatis_Spring项目遇到的问题

    1.  问题: jdk版本不匹配  解决方法:首先 到项目空间的   .setting文件中找到  org.eclipse.wst.common.project.facet.core.xml  修改参 ...