【DFS序】【莫队算法】【权值分块】bzoj1803 Spoj1487 Query on a tree III
基本等同这个,只是询问的东西不大一样而已。
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的更多相关文章
- HDU 4358 Boring counting dfs序+莫队算法
题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- 【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 ...
- BZOJ2038: [2009国家集训队]小Z的袜子(hose) -- 莫队算法 ,,分块
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 3577 Solved: 1652[Subm ...
- 莫队算法 sqrt(n)分块思想
在此说一下本渣对莫队算法思想的一些浅薄理解 莫队算法的思想就是对真个区间的分块,然后按照每块来分别进行计算,这样最终的复杂度可以达到n*sqrt(n) 小Z的袜子是一道非常经典的题目.:题目链接htt ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- CF600E Lomsat gelral (dfs序+莫队)
题面 题解 看到网上写了很多DSU和线段树合并的题解,笔者第一次做也是用的线段树合并,但在原题赛的时候却怕线段树合并调不出来,于是就用了更好想更好调的莫队. 这里笔者就说说莫队怎么做吧. 我们可以通过 ...
- 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$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
随机推荐
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- maven与gradle的对比
Java世界中主要有三大构建工具:Ant.Maven和Gradle.经过几年的发展,Ant几乎销声匿迹.Maven也日薄西山,而Gradle的发展则如日中天.笔者有幸见证了Maven的没落和Gradl ...
- hive Illegal Operation state transition from CLOSED to ERROR的处理
异常堆栈如下: 2015-11-24 16:49:11,495 ERROR org.apache.hive.service.cli.operation.Operation: Error running ...
- Xamarin+vs2010部署错误:error MSB6004: 指定的任务可执行文件位置\sdk\\tools\zipalign.exe”无效
好不容易配好了Xamarin和vs2010,也搞好了GenyMotion的虚拟机配置,开始调试的时候又报出了这样的错误: error MSB6004: 指定的任务可执行文件位置"C:\Use ...
- 使用Word2010发布博客文章
发布博客可以直接在web页面上面编辑,也可以使用客户端编辑,其中客户端支持windows live writer以及word本身的发布博客功能.个人试用后倾向于使用word发布博客文章. 下面的内容转 ...
- Struts2 内建的验证规则
Struts2 内建的验证规则 conversion validator:转换验证器 date validator:日期验证器 double validator:浮点验证器 email validat ...
- [洛谷P2730] 魔板 Magic Squares
洛谷题目链接:魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都 ...
- 【BZOJ1101】Zap [莫比乌斯反演]
Zap Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 对于给定的整数a,b和d,有多少正整 ...
- 【BZOJ3237】【AHOI2013】连通图 [CDQ分治]
连通图 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input Output Sampl ...
- bzoj4602 [Sdoi2016]齿轮
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4602 [题解] 对于每组齿轮(u, v)连边,权值为y/x(反向边x/y) 那么直接dfs计 ...