HDU 4358 莫队算法+dfs序+离散化
Boring counting
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)
Total Submission(s): 2808 Accepted Submission(s): 826
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
/******************************
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序+离散化的更多相关文章
- hdu 5145(莫队算法+逆元)
NPY and girls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4638 莫队算法
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 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序+莫队算法
题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...
- 【bzoj4940】[Ynoi2016]这是我自己的发明 DFS序+树上倍增+莫队算法
题目描述 给一个树,n 个点,有点权,初始根是 1. m 个操作,每次操作: 1. 将树根换为 x. 2. 给出两个点 x,y,从 x 的子树中选每一个点,y 的子树中选每一个点,如果两个点点权相等, ...
- 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- HDU 5145 NPY and girls(莫队算法+乘法逆元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...
- bzoj 4358 Permu - 莫队算法 - 链表
题目传送门 需要高级权限的传送门 题目大意 给定一个全排列,询问一个区间内的值域连续的一段的长度的最大值. 考虑使用莫队算法. 每次插入一个数$x$,对值域的影响可以分成4种情况: $x - 1$, ...
随机推荐
- js基础之动画(二)
一.多物体同时运动 栗子一:多个Div,鼠标移入变高,动态下拉菜单 function startMove(obj,iTarget){ clearInterval(obj.timer); obj.ti ...
- 四个使用this的典型应用
(1)在html元素事件属性中使用,如 <input type=”button” onclick=”showInfo(this);” value=”点击一下”/> (2)构造函数 func ...
- HDU 3074 Multiply game(线段树)
单点更新,更新时先除去 原来的数,因为有去摸,可以用乘上逆元代替. //================================================================ ...
- 制作Linux下程序安装包——使用脚本打包bin、run等安装包
制作简单的安装包的时候可以简单的用cat命令连接两个文件,然后头部是脚本文件,执行的时候把下面的文件分解出来就行了.一般这个后部分的文件是个压缩 包,那样,就能够打包很多文件了,在脚本中解压出来即可. ...
- java基础之 http
HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从HTTP服务器(Web服务器)请求 ...
- Non-Programmer's Tutorial for Python 3/File IO
File I/O Here is a simple example of file I/O (input/output): # Write a file with open("test.tx ...
- Mac下SVN服务器环境的搭建和配置(除展示图片外,所有命令在Linux/Unix下适用)
这几天领导没有安排工作,闲着没事就想把自己这两年做iOS开发时感觉知识有欠缺的地方想好好深入地补习一下,昨天和今天就计划好好学习下SVN和git的从创建和到原理,到命令,到界面的使用.一不小心被另一领 ...
- JVM-垃圾收集器
Java虚拟机规范中对垃圾收集器应该如何实现并没有任何规定,因此不同的厂商,不同版本的虚拟机所提供的垃圾收集器可能会有很大差距. HotSpot虚拟机示意图: 说明:两个收集器之间存在连线说明它们可 ...
- apache http client vs urlconnection
Google has deprecated HttpClient Choose an HTTP Client Most network-connected Android apps use HTTP ...
- hdu 2053
Ps:找规律题....凡是平方数都是开...WA了一次..数组给的太小?...后来给到3000..就AC了 代码: #include "stdio.h"long long dp[3 ...