hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)
For each test case, the first line contains two integers N and K, as described above. ( 1<= N <= 105, 1 <= K <= N )
Then come N integers in the second line, they are the weights of vertice 1 to N. ( 0 <= weight <= 109 )
For next N-1 lines, each line contains two vertices u and v, which is connected in the tree.
Next line is a integer Q, representing the number of queries. (1 <= Q <= 105)
For next Q lines, each with an integer u, as the root of the subtree described above.
Seperate each test case with an empty line.
3 1
1 2 2
1 2
1 3
3
2
1
3
1
1
1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
struct is
{
int v,nex;
}edge[N<<];
int head[N<<],edg,in[N],out[N],tot,pos[N],w[N];
vector<int>l;
int a[N],flag[N];
void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].nex=head[u];
head[u]=edg;
}
void dfs(int u,int fa)
{
in[u]=++tot;
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(v==fa)continue;
dfs(v,u);
}
out[u]=tot;
}
struct hh
{
int l,r,now;
bool operator <(const hh b)
{
if(pos[l]!=pos[b.l])
return pos[l]<pos[b.l];
return r<b.r;
}
}p[N];
int ans[N],mp[N],z[N];
void add(int pos)
{
z[mp[a[pos]]]--;
mp[a[pos]]++;
z[mp[a[pos]]]++;
}
void del(int pos)
{
z[mp[a[pos]]]--;
mp[a[pos]]--;
z[mp[a[pos]]]++;
}
void init(int n)
{
l.clear();
memset(mp,,sizeof(mp));
memset(z,,sizeof(z));
int s=(int)(sqrt(n));
for(int i=;i<=n;i++)
pos[i]=(i-)/s+;
memset(head,-,sizeof(head));
memset(a,,sizeof(a));
edg=;
tot=;
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
if(cas!=)
printf("\n");
int n,k;
scanf("%d%d",&n,&k);
init(n);
for(int i=;i<=n;i++)
scanf("%d",&w[i]),l.push_back(w[i]);
sort(l.begin(),l.end());
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,-);
for(int i=;i<=n;i++)
flag[in[i]]=i;
for(int i=;i<=n;i++)
a[i]=lower_bound(l.begin(),l.end(),w[flag[i]])-l.begin();
int q;
scanf("%d",&q);
for(int i=;i<=q;i++)
{
int x;
scanf("%d",&x);
p[i].l=in[x];
p[i].r=out[x];
p[i].now=i;
}
sort(p+,p++q);
int L=,R=;
for(int i=;i<=q;i++)
{
//cout<<p[i].l<<" "<<p[i].r<<" "<<p[i].now<<endl;
while(L<p[i].l)
{
del(L);
L++;
}
while(L>p[i].l)
{
L--;
add(L);
}
while(R>p[i].r)
{
del(R);
R--;
}
while(R<p[i].r)
{
R++;
add(R);
}
ans[p[i].now]=z[k];
}
printf("Case #%d:\n",cas++);
for(int i=;i<=q;i++)
printf("%d\n",ans[i]);
}
return ;
}
hdu 4358 Boring counting dfs序+莫队+离散化的更多相关文章
- HDU 4358 Boring counting dfs序+莫队算法
题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...
- 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 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- HDU - 4358 Boring counting (dsu on tree)
Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)
题目链接 Tree and Queries 题目大意 给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
- CF600E Lomsat gelral (dfs序+莫队)
题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...
- HDU - 4358 Boring counting (树上启发式合并/线段树合并)
题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
随机推荐
- sax解析案例(javabean封装xml文档数据)
package itcast.sax; import java.io.IOException; import java.util.List; import javax.xml.parsers.Pars ...
- DDoS deflate+iptables防御轻量级ddos攻击
一.查看攻击者ip #netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 二.安装ddos deflate ...
- WPF TabControl 隐藏标头
1. 将每个 TabItem 的 Visibility 属性设置为 Visibility.Collapsed 即可隐藏标头 <TabItem Visibility="Collapsed ...
- ASCII字符对照表 不时之需
ASCII可显示字符 二进制 十进制 十六进制 图形 0010 0000 32 20 (空格)(␠) 0010 0001 33 21 ! 0010 0010 34 22 " 0010 001 ...
- 修改pip源
http://www.cnblogs.com/hushaojun/p/4606986.html mkdir -p ~/.pip/ vi ~/.pip/pip.conf [global] trusted ...
- resx文件在X64位编译,提示“未能加载文件或程序集”的问题?
原文:resx文件在X64位编译,提示"未能加载文件或程序集"的问题? resx文件在X64位编译,提示"未能加载文件或程序集"的问题? 解答: 错误现象如下 ...
- 系统隐式 Intent
1. 找出系统中所有视频 private void choiceFile() { Intent intent = new Intent(Intent.ACTION_PICK, android.prov ...
- App Store有哪些原因会影响app应用上架呢?(分享)
App Store对于应用上架的审核是非常严格的,很可能一个没有注意到的细节,或者一个你根本没想想到的原因就会导致你的应用上架失败.而排除这些无可避免的错误以外,还有一些导致应用上架失败的原因,非常常 ...
- csuoj 1505: 酷酷的单词
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1505 1505: 酷酷的单词 时间限制: 1 Sec 内存限制: 128 MB 提交: 340 ...
- The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....
遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....,查找的 ...