Boring counting

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 2808    Accepted Submission(s): 826

Problem Description
In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?
 
Input
The first line of the input contains an integer T( T<= 5 ), indicating the number of test cases.
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.
 
Output
For each test case, output "Case #X:" first, X is the test number. Then output Q lines, each with a number -- the answer to each query.

Seperate each test case with an empty line.

 
Sample Input
1
3 1
1 2 2
1 2
1 3
3
2
1
3
 
Sample Output
Case #1:
1
1
1
 
Author
fish@UESTC_Oblivion
 
Source
 
题意:给你一个树  每个节点都有一个权值   q个询问 问x的子树中有多少种不同的权值出现了k次
题解:第一题莫队 莫队算法 正如莫涛本人而言这种方法是处理一类无修改的离线区间询问问题的  dfs序将子树的问题转换为区间问题
之后就是莫队的处理,只是一个神奇的算法,在我看来就是一种有技巧的暴力。另外还要注意对权值的离散化。PE orzz
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int T;
int n,k,w[],v[],vv[],x,y,q;
int in[],out[];
int pre[];
int pos[];
int re[];
int sum[];
int dfn=;
int ans;int nedge=;
struct node
{
int pre,ed;
}N[];
struct query
{
int id;
int l,r;
}M[];
void add(int st,int ed )
{
nedge++;
N[nedge].ed=ed;
N[nedge].pre=pre[st];
pre[st]=nedge;
}
void getdfs(int now,int fa)
{
in[now]=++dfn;
for(int i=pre[now];i;i=N[i].pre)
{
if(N[i].ed!=fa)
{
getdfs(N[i].ed,now);
}
}
out[now]=dfn;
}
void init()
{
nedge=;
memset(pre,,sizeof(pre));
dfn=;
ans=;
memset(sum,,sizeof(sum));
memset(N,,sizeof(N));
memset(M,,sizeof(M));
memset(re,,sizeof(re));
memset(pos,,sizeof(pos));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(w,,sizeof(w));
memset(v,,sizeof(v));
memset(vv,,sizeof(vv));
}
bool cmp(struct query aa,struct query bb)
{
if(pos[aa.id]<pos[bb.id])
return true;
if(pos[aa.id]==pos[bb.id])
{
if(aa.r<bb.r)
return true;
}
return false;
}
void ad(int value)
{
if(sum[value]==k)
ans--;
sum[value]++;
if(sum[value]==k)
ans++;
}
void del(int value)
{
if(sum[value]==k)
ans--;
sum[value]--;
if(sum[value]==k)
ans++;
}
int main()
{
scanf("%d",&T);
for(int i=;i<=T;i++)
{
init();
scanf("%d %d",&n,&k);
int block=sqrt(n);
for(int j=;j<=n;j++){
scanf("%d",&w[j]);
v[j]=w[j];
}
sort(w+,w++n);
for(int j=;j<=n;j++){
v[j]=lower_bound(w+,w+n+,v[j])-w-;
}
for(int j=;j<n;j++){
scanf("%d %d",&x,&y);
add(x,y);add(y,x);
}
getdfs(,);
for(int j=;j<=n;j++)
vv[in[j]]=v[j];// 将树上的权值 下放到区间当中
scanf("%d",&q);
int exm;
for(int j=;j<=q;j++){
scanf("%d",&exm);
M[j].l=in[exm];
M[j].r=out[exm];
M[j].id=j;
pos[j]=(M[j].l)/block;//分块 处理的不是特别好
}
printf("Case #%d:\n",i);
sort(M+,M++q,cmp);
int l=, r=;//初始区间
ad(vv[++r]);
for(int j=; j<=q; j++)
{
while(r<M[j].r) ad(vv[++r]);
while(r>M[j].r) del(vv[r--]);
while(l<M[j].l) del(vv[l++]);
while(l>M[j].l) ad(vv[--l]);
re[M[j].id]=ans;
}
for(int j=;j<=q;j++)
printf("%d\n",re[j]);
if(i!=T)
printf("\n");
}
return ;
}

HDU 4358 莫队算法+dfs序+离散化的更多相关文章

  1. hdu 5145(莫队算法+逆元)

    NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. HDU 4638 莫队算法

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. HDU 4358 Boring counting dfs序+莫队算法

    题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...

  5. 【bzoj4940】[Ynoi2016]这是我自己的发明 DFS序+树上倍增+莫队算法

    题目描述 给一个树,n 个点,有点权,初始根是 1. m 个操作,每次操作: 1. 将树根换为 x. 2. 给出两个点 x,y,从 x 的子树中选每一个点,y 的子树中选每一个点,如果两个点点权相等, ...

  6. 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...

  7. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  8. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  9. bzoj 4358 Permu - 莫队算法 - 链表

    题目传送门 需要高级权限的传送门 题目大意 给定一个全排列,询问一个区间内的值域连续的一段的长度的最大值. 考虑使用莫队算法. 每次插入一个数$x$,对值域的影响可以分成4种情况: $x - 1$, ...

随机推荐

  1. <input type="hidden" id="haha" name="wang" value="xiaodong" />

    jsp中一个隐藏的文本框,文本框里的值是:xiaodong id属性和name属性:就是在JavaScript中或者控制器中根据id或name属性取它的value的值 开发人员所需要,又不想让用户看到 ...

  2. JavaScript数字精度上代码。

    /**不能超过 9007199254740992 * floatObj 包含加减乘除四个方法,能确保浮点数运算不丢失精度 * * 我们知道计算机编程语言里浮点数计算会存在精度丢失问题(或称舍入误差), ...

  3. ios应用数据存储的常用方式 ios7.1应用沙盒

    归档:用某种格式保存某个对象,又称持久化. 1XML 属性列表plist归档(持久化) 2Preference(偏好设置) 3NSKeyedArchiver归档 4SQLite3 5Core Data ...

  4. [vijos P1112] 小胖的奇偶

    第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫.看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢? ...

  5. git——学习笔记(二)远程仓库

    GIT杀手锏之一——远程仓库 拥有远程仓库的两个办法 1:搭一个Git服务器  2:在GitHub上免费托管的Git仓库 本地仓库   远程仓库 一.在GitHub上免费托管的Git仓库 电脑: 1. ...

  6. win10下安装centOS 7 U盘

    前段时间我把朋友帮忙装的ubuntu15.10给玩坏了=.=虽然后来自己在另一台电脑上成功装了ubuntu16.04和win7双系统,但是...这台电脑也要装个别的系统才比较..不空.所以决定装个ce ...

  7. python随笔

    1. 使用iter实现接收用户多行输入 stopword = '' str = '' print('请将要添加的内容输入下方,输入空白行按回车退出程序:') for line in iter(inpu ...

  8. java字节数组和16进制之间的转换

    /* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...

  9. ViewBag、ViewData和TempData的使用和区别

    在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData. MVC3中保留了ViewData的使用. ViewBag 是动态类型(dynamic),ViewData ...

  10. 简单模仿javascript confirm方法的例子

    页面中有个删除按钮: <?php $i = 1; foreach ($packages as $package) { ?> <tr> <td height="3 ...