HDU 4358
看了题解那个弱化版后,马上就去做了HDU 3333这道题,发现有可用的地方。于是往这方面想,主要是处理如何确定一个数出现K次的问题。想到了从左往右把每个数出现的次数记下来,但感觉不是这样,呃,再看别人做的,真的是这样的--!
主要是处理一个数出现K次后的情况,把每个数出现的位置记录下来,当出现大于等于K次时,假设此时已出现sz个,则把sz-k这个位置加1,把之前的SZ-K-1的位置-2,使之状态是-1(因为之前刚好出现K次时就已加1)。于是当查询到右端点时,求出区间和,+1和-1刚好消去。需要注意的是,当SZ-K-2>=0时,应当把SZ-K-2之前为-1的位置置成0,即加1。这样结果才是正确的。
至于把树映射到数组,很简单,使用DFS+时间戳的方法就可以了,记录每个结点第一次出现的次序以及深搜完以该结点为根的子树的最后一个时间戳即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#define LL __int64
#define lowbit(x) ((x)&(-x))
using namespace std; const int N=100100;
const int Q=100100; struct Query{
int l,r,Id;
Query(){}
Query(int ll,int rr,int d){
l=ll; r=rr; Id=d;
}
bool operator <(const Query &a)const{
if(r<a.r) return true;
return false;
}
};
Query query[Q];
LL su[N],ans[Q];
int num[N],val[N];
struct Node{
int bgn,en;
};
Node node[N];
vector<int>pos[N];
int n,k,q,tot,DEP; struct Edge{
int u,v,next;
}edge[N*2];
int head[N]; void addedge(int u,int v){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
} LL sum(int x){
if(x==0) return 0;
LL s=0;
for(;x;x-=lowbit(x)){
s+=su[x];
}
return s;
} void update(int x,LL w){
for(;x<=n;x+=lowbit(x))
su[x]+=w;
} void dfs(int u,int f){
++DEP;
node[u].bgn=DEP;
val[DEP]=num[u];
for(int e=head[u];e!=-1;e=edge[e].next){
int v=edge[e].v;
if(v!=f){
dfs(v,u);
}
}
node[u].en=DEP;
} int main(){
int T,t=0,u,v,cnt=0;
scanf("%d",&T);
while(++t<=T){
map<int,int>mp;
tot=0;DEP=0;
cnt=0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&num[i]);
head[i]=-1;
su[i]=0;
pos[i].clear();
if(!mp[num[i]])
mp[num[i]]=++cnt;
}
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(1,0);
scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%d",&cnt);
query[i]=Query(node[cnt].bgn,node[cnt].en,i);
}
sort(query+1,query+1+q);
cnt=1;
int sz;
for(int i=1;i<=n;i++){
int t=mp[val[i]];
pos[t].push_back(i);
sz=pos[t].size();
if(sz>=k){
if(sz==k){
update(pos[t][sz-k],1);
}
else{
update(pos[t][sz-k-1],-2);
update(pos[t][sz-k],1);
}
if(sz-k-2>=0)
update(pos[t][sz-k-2],1);
}
while(query[cnt].r==i){
ans[query[cnt].Id]=sum(query[cnt].r)-sum(query[cnt].l-1);
cnt++;
}
}
printf("Case #%d:\n",t);
for(int i=1;i<=q;i++)
printf("%I64d\n",ans[i]);
if(t<T)
puts("");
}
return 0;
}
HDU 4358的更多相关文章
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- HDU - 4358 Boring counting (dsu on tree)
Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- HDU 4358 莫队算法+dfs序+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)T ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- HDU 4358 Boring counting dfs序+莫队算法
题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...
- HDU - 4358 Boring counting (树上启发式合并/线段树合并)
题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...
- ACM数据结构相关资料整理【未完成,待补充】
在网上总是查不到很系统的练ACM需要学习的数据结构资料,于是参考看过的东西,自己整理了一份. 能力有限,欢迎大家指正补充. 分类主要参考<算法竞赛入门经典训练指南>(刘汝佳),山东大学数据 ...
随机推荐
- poj 1321(DFS)
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- poj 3259-- Wormholes(SPFA)
...
- Selenium的文件上传JAVA脚本
在写文件上传脚本的时候,遇到了很多问题,包括元素定位,以及上传操作,现在总结下来以下几点: 1. 上传的控件定位要准确,必要时要进行等待 WebElement adFileUpload = drive ...
- Super超级ERP系统---(4)采购管理--采购单创建
Erp系统中采购是系统必不可少的一部分,也就是ERP种的进货模块,超级ERP系统中的采购模块选选择采购供应商,然后选择进货商品的数量和采购价格,创建采购进货单 1.创建采购单 2.审核采购单 采购单创 ...
- Oracle-基本SQL语句
--添加一个表 create table TestUser ( id int primary key , name varchar(20) , address varchar(20) ) /* *设置 ...
- BZOJ1096: [ZJOI2007]仓库建设(dp+斜率优化)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5790 Solved: 2597[Submit][Status][Discuss] Descript ...
- JAVA基本数据类型转换的注意事项
JAVA中基本数据类型: 类型: 字节: 范围: 默认值: byte 1 -128~127 0 short 2 -32768~32767 0 char 2 0~65535 '\u0000' int 4 ...
- 更换WordPress后台登录地址
在后台找到wp-content—themes—twentyfifteen(当前的网站主题)—functions.php 在代码的最下面加入以下代码: //后台唯一登录地址 add_action('lo ...
- hdu3926 Hand in Hand 判断同构
因为每个人小朋友只有两只手,所以每个点最多只有2度.图有可能是环.链,以及环和链构成的复杂图. 如何判断两幅图是否相似呢?判断相似是判断两幅图的圈的数量,以及构成圈的点数是否相同.还有判断链的数目和构 ...
- spring IOC(DI)和AOP
软件152谭智馗 IOC(Inversion of Control,控制倒转)Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. DI—Dependency Injecti ...