基本等同这个,只是询问的东西不大一样而已。

http://www.cnblogs.com/autsky-jadek/p/4159897.html

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int Num,CH[12],f,c;
inline void R(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
inline void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
#define maxn 100001
int num[maxn],num2[maxn],l[maxn],sum=1,b[maxn],w[maxn],now,from,to;
int es,n,m,v[200001],first[maxn],next[200001],a[maxn],ma[maxn],en,cnts[maxn];
int Lt[maxn],Rt[maxn],X,K,anss[maxn],fdfn[maxn];
bool vis[100001];
struct Point{int v,p;}t[maxn];
struct Ask{int l,r,k,p;}Q[10001];
bool operator < (const Point &a,const Point &b){return a.v<b.v;}
bool operator < (const Ask &a,const Ask &b)
{return num2[a.l]!=num2[b.l] ? num2[a.l]<num2[b.l] : a.r<b.r;}
void AddEdge(const int &U,const int &V){v[++es]=V; next[es]=first[U]; first[U]=es;}
void dfs(int U)
{
Lt[U]=++now; fdfn[now]=U; t[now].v=w[U]; t[now].p=now; vis[U]=1;
for(int i=first[U];i;i=next[i]) if(!vis[v[i]]) dfs(v[i]);
Rt[U]=now;
}
void Val_Make_Block()
{
int sz=sqrt(en); if(!sz) sz=1;
for(;sum*sz<en;++sum)
{
l[sum]=(sum-1)*sz+1; int r=sum*sz;
for(int i=l[sum];i<=r;++i) num[i]=sum;
}
l[sum]=(sum-1)*sz+1;
for(int i=l[sum];i<=en;++i) num[i]=sum;
}
void Mo_Make_Block()
{
int sum=1,sz=sqrt(n); if(!sz) sz=1;
for(;sum*sz<n;++sum)
{
int r=sum*sz;
for(int i=(sum-1)*sz+1;i<=r;++i) num2[i]=sum;
}
for(int i=(sum-1)*sz+1;i<=n;++i) num2[i]=sum;
}
void Insert(const int &x){++b[x]; ++cnts[num[x]];}
void Delete(const int &x){--b[x]; --cnts[num[x]];}
int Kth(const int &x)
{
int cnt=0;
for(int i=1;;++i)
{
cnt+=cnts[i];
if(cnt>=x)
{
cnt-=cnts[i];
for(int j=l[i];;++j)
{cnt+=b[j]; if(cnt>=x) return j;}
}
}
}
int main()
{
R(n); for(int i=1;i<=n;++i) R(w[i]);
for(int i=1;i<n;++i)
{
R(from); R(to);
AddEdge(from,to);
AddEdge(to,from);
}
dfs(1); sort(t+1,t+n+1);
ma[a[t[1].p]=++en]=fdfn[t[1].p];
for(int i=2;i<=n;++i)
{
if(t[i].v!=t[i-1].v) ++en;
ma[a[t[i].p]=en]=fdfn[t[i].p];
}
Val_Make_Block(); R(m);
for(int i=1;i<=m;++i)
{R(X); R(K); Q[i].l=Lt[X]; Q[i].r=Rt[X]; Q[i].k=K; Q[i].p=i;}
Mo_Make_Block();
sort(Q+1,Q+m+1);
for(int i=Q[1].l;i<=Q[1].r;++i) Insert(a[i]);
anss[Q[1].p]=ma[Kth(Q[1].k)];
for(int i=2;i<=m;++i)
{
if(Q[i].l<Q[i-1].l) for(int j=Q[i-1].l-1;j>=Q[i].l;--j) Insert(a[j]);
else for(int j=Q[i-1].l;j<Q[i].l;++j) Delete(a[j]);
if(Q[i].r<Q[i-1].r) for(int j=Q[i-1].r;j>Q[i].r;--j) Delete(a[j]);
else for(int j=Q[i-1].r+1;j<=Q[i].r;++j) Insert(a[j]);
anss[Q[i].p]=ma[Kth(Q[i].k)];
}
for(int i=1;i<=m;++i) P(anss[i]),puts("");
return 0;
}

  

【DFS序】【莫队算法】【权值分块】bzoj1803 Spoj1487 Query on a tree III的更多相关文章

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

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

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

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

  3. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  4. BZOJ2038: [2009国家集训队]小Z的袜子(hose) -- 莫队算法 ,,分块

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 3577  Solved: 1652[Subm ...

  5. 莫队算法 sqrt(n)分块思想

    在此说一下本渣对莫队算法思想的一些浅薄理解 莫队算法的思想就是对真个区间的分块,然后按照每块来分别进行计算,这样最终的复杂度可以达到n*sqrt(n) 小Z的袜子是一道非常经典的题目.:题目链接htt ...

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

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

  7. CF600E Lomsat gelral (dfs序+莫队)

    题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...

  8. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  9. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

随机推荐

  1. POJ1308:Is It A Tree?(并查集)

    Is It A Tree? 题目链接:http://poj.org/problem?id=1308 Description: A tree is a well-known data structure ...

  2. @Resource注解完成自动装配

    @Resource注解是通过名字来自动装配的.在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直. 下面是详细代码:说明了@Resource注解是 ...

  3. 复杂的json分析

    在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...

  4. bzoj 3720 Gty的妹子树 树分块?瞎搞

    Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2149  Solved: 781[Submit][Status][Discuss] D ...

  5. jquery和ajax,json写法的说明

    一: 在ajax中,如果没有用jquery,则如xmlHttpRequest.open("POST", "AjaxServlet", true); (1)如果用 ...

  6. 【BZOJ1419】 Red is good [期望DP]

    Red is good Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 桌面上有R张红牌和B张 ...

  7. 51nod 1791 合法括号子段

    有一个括号序列,现在要计算一下它有多少非空子段是合法括号序列. 合法括号序列的定义是: 1.空序列是合法括号序列. 2.如果S是合法括号序列,那么(S)是合法括号序列.3.如果A和B都是合法括号序列, ...

  8. sphinx 分词搭建手册

    步奏1. yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel expa ...

  9. linux下新硬盘的自动检测及格式化--支持硬盘的热插拔处理

    说明 可能存在bug,所以慎用!!! 且只在mbr分区格式下测试过. parted.sh 可以用在系统起来的时候,比如rc.local脚本里面. parted.c 需要parted.sh脚本配合使用, ...

  10. Winform 中panel的mousewheel鼠标滚轮事件触发

    如果将窗体或容器控件(如Panel控件)的AutoScroll属性设置为True时,那么当窗体或Panel容不下其中的子控件时就会出现 滚动条,通过移动滚动条可以上下显示出窗体或Panel中的全部内容 ...