【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$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...
随机推荐
- POJ2559 Largest Rectangle in a Histogram (单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26012 ...
- 复杂的json分析
在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...
- react 记录:React Warning: Hash history cannot PUSH the same path; a new entry will not be added to the history stack
前言: react-router-dom 4.4.2 在页面中直接使用 import { Link } from 'react-router-dom' //使用 <Link to={{ path ...
- 【bzoj1010-toy】斜率优化入门模板
dsy1010: [HNOI2008]玩具装箱 [题目描述] 有n个数,分成连续的若干段,每段(假设从第j个到第i个组成一段)的分数为 (X-L)^2,X为j-i+Sigma(Ck) i<=k& ...
- 【bzoj1911-[Apio2010]特别行动队】斜率优化
[题目描述] 有n个数,分成连续的若干段,每段的分数为a*x^2+b*x+c(a,b,c是给出的常数),其中x为该段的各个数的和.求如何分才能使得各个段的分数的总和最大. [输入格式] 第1行:1个 ...
- A trick in Exploit Dev
学习Linux BOF的时候,看了这个文章,https://sploitfun.wordpress.com/2015/06/23/integer-overflow/ ,原文给出的exp无法成功, 此时 ...
- 转:Android 调试桥(adb)是多种用途的工具
转自:http://my.oschina.net/xuwa/blog/1574 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态. 可以通过下列几种方法加入 ...
- StringUtils.isEmpty()和StringUtils.isBlank() 区别
isBlank()判断空的情况包括了isEmpty()的情况,isBlank()不仅判断了 无对象.空对象的情况,而且也判断了无意义的空白字符,比如空格等.
- 4.flume实战(一)
需求:从指定网络端口采集数据输出到控制台 使用flume的关键就是写配置文件 a)配置source b)配置channel c)配置sink d)把以上三个组件串起来 我们看一下官网给的配置文件 # ...
- C++类中引用成员和常量成员的初始化(初始化列表)
如果一个类是这样定义的: Class A { public: A(int pram1, int pram2, int pram3); privite: int a; int &b; const ...