Boring counting

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

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

题目链接:HDU 4358

把DFS序和莫队算法结合了起来,前两发杯具PE,如果用过DFS序配合线段树的话就大概能知道怎么做了,对于一颗子树的询问显然DFS序是很适合的,然后这样就得到了每一个点所管理的区间[L,R],那莫队移动的时候怎么判断是否遇到了某一个原树上的节点呢?显然用先序遍历的方式来得到某一个点管理的区间,那么L这个点必定是子树树根的位置,若这个点管理的位置是[L,R]那么实际上这个子树根点的值就是arr[L],不过由于每一个区间都是满点的,就映射成val[timeorder]=arr[u],其中u是某次dfs时的起点。

除此之外还要判断当前减掉的数字是从k减到k-1还是k+1减到k,加上的同理,最后每一个case之间换一行,结尾不换行Orz……另外最近在玩C++11的匿名函数,在sort里随便用下玩

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e5+7;
struct edge
{
int to;
int pre;
};
struct info
{
int l,r;
int id,b;
};
info Q[N];
edge E[N];
int head[N],tot;
int L[N],R[N],order,val[N];
int arr[N],cnt[N],ans[N];
vector<int>vec; void init()
{
CLR(head,-1);
tot=0;
CLR(L,0);
CLR(R,0);
order=0;
CLR(val,0);
CLR(cnt,0);
vec.clear();
}
inline void add(int s,int t)
{
E[tot].to=t;
E[tot].pre=head[s];
head[s]=tot++;
}
void dfs(const int &now,const int &pre)
{
L[now]=++order;
val[order]=arr[now];
for (int i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
//if(v!=pre)
dfs(v,now);
}
R[now]=order;
}
int main(void)
{
int tcase,n,k,i,a,b,m,rt;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
init();
scanf("%d%d",&n,&k);
for (i=1; i<=n; ++i)
{
scanf("%d",&arr[i]);
vec.push_back(arr[i]);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for (i=1; i<=n; ++i)
arr[i]=lower_bound(vec.begin(),vec.end(),arr[i])-vec.begin();
for (i=0; i<n-1; ++i)
{
scanf("%d%d",&a,&b);
add(a,b);
}
dfs(1,-1);
scanf("%d",&m);
int unit=(int)sqrt(1.0*n);
for (i=0; i<m; ++i)
{
scanf("%d",&rt);
Q[i].l=L[rt];
Q[i].r=R[rt];
Q[i].id=i;
Q[i].b=Q[i].l/unit;
}
sort(Q,Q+m,[&](const info &x,const info &y){return (x.b==y.b&&x.r<y.r)||x.b<y.b;});
int l=1,r=0,temp=0;
for (i=0; i<m; ++i)
{
while (l<Q[i].l)
{
--cnt[val[l]];
if(cnt[val[l]]==k)
++temp;
else if(cnt[val[l]]==k-1)
--temp;
++l;
}
while (l>Q[i].l)
{
--l;
++cnt[val[l]];
if(cnt[val[l]]==k)
++temp;
else if(cnt[val[l]]==k+1)
--temp;
}
while (r<Q[i].r)
{
++r;
++cnt[val[r]];
if(cnt[val[r]]==k)
++temp;
else if(cnt[val[r]]==k+1)
--temp;
}
while (r>Q[i].r)
{
--cnt[val[r]];
if(cnt[val[r]]==k-1)
--temp;
else if(cnt[val[r]]==k)
++temp;
--r;
}
ans[Q[i].id]=temp;
}
printf("Case #%d:\n",q);
for (i=0; i<m; ++i)
printf("%d\n",ans[i]);
if(q!=tcase)
putchar('\n');
}
return 0;
}

HDU 4358 Boring counting(莫队+DFS序+离散化)的更多相关文章

  1. hdu 4358 Boring counting dfs序+莫队+离散化

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

  2. hdu 4358 Boring counting 离散化+dfs序+莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...

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

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

  4. HDU - 4358 Boring counting (dsu on tree)

    Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...

  5. P2336 [SCOI2012]喵星球上的点名(后缀自动机+莫队+dfs序)

    P2336 [SCOI2012]喵星球上的点名 名字怎么存?显然是后缀自动机辣 询问点到多少个喵喵喵其实就是 查询后缀自动机上parent树的一个子树 于是我们考虑莫队 怎么树上莫队呢 我们用dfs序 ...

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

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

  7. 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 ...

  8. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  9. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

随机推荐

  1. DOM--4 响应用户操作和事件(1)

    简介 事件:事件并不是以"on"开头的.例如,onclick引用的是一个对象的属性,click才是事件. 事件侦听器:当指定的事件发生时会执行的函数或方法. 事件注册:为DOM元素 ...

  2. String equals()方法使用以及子串加密

    String equals()方法的实现方法: 名称 说明 String.Equals (Object) 确定此 String 实例是否与指定的对象(也必须是 String)具有相同的值. Strin ...

  3. The Unique MST(次小生成树)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22335   Accepted: 7922 Description Give ...

  4. jquery.color.js的使用

    Jquery本身不支持变色,Jquery Color.js弥补了这缺陷并为animate动画赋予变色效果,如下红变蓝后执行回调再由蓝变红. <!DOCTYPE html> <html ...

  5. SQL Prompt激活

    SQL脚本越写越多,总是觉得编写效率太过于低下,这和打字速度无关.在我个人编写SQL脚本时,至少会把SQL的格式排列成易于阅读的,因为其他人会阅读到你的SQL,无论是在程序中或是脚本文件中,良好的排版 ...

  6. 使用递推解题:EOJ2999

    题目: Description 给定一个多项式 (ax+by)k,计算多项式展开后 xnym 项的系数. Input 第1行:一个整数T(1≤T≤10)为问题数. 接下来共T行.每行5个整数,分别为a ...

  7. BZOJ3514 : Codechef MARCH14 GERALD07加强版

    以边编号为权值 用Link-cut Tree维护最大生成树 对于新加的第i条边(u,v) a[i]表示当a[i]这条边加入后连通块个数会减少 若u==v则a[i]=m 若u与v不连通则连上,a[i]= ...

  8. Storm实战集锦

    一.Kafka+Storm+HDFS整合实践 本文导读: 前言 Kafka安装配置 Storm安装配置 整合Kafka+Storm 整合Storm+HDFS 整合Kafka+Storm+HDFS 参考 ...

  9. Android作业分组与选题

    期末大作业 序号 题目 组员分工 完成度 1 基于安卓系统的游戏开发 2 设计一个安卓手机小游戏 3 Android平台应用——音乐播放器设计 4 基于Android技术的个人博客 5 电子阅读器 6 ...

  10. 让mysql有直接写redis能力

    1.文件包下载 http://pan.baidu.com/s/1qW9DHYc 2.安装 gcc -fPIC -Wall -I/usr/local/mysql/include/mysql -I. -s ...