Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty and ability.Some times Sean will fire one staff.Then one of the fired man’s Subordinates will replace him whose ability is higher than him and has the highest loyalty for company.Sean want to know who will replace the fired man.

Input

In the first line a number T indicate the number of test cases. Then for each case the first line contain 2 numbers n,m (2<=n,m<=50000),indicate the company has n person include Sean ,m is the times of Sean’s query.Staffs are numbered from 1 to n-1,Sean’s number is 0.Follow n-1 lines,the i-th(1<=i<=n-1) line contains 3 integers a,b,c(0<=a<=n-1,0<=b,c<=1000000),indicate the i-th staff’s superior Serial number,i-th staff’s loyalty and ability.Every staff ‘s Serial number is bigger than his superior,Each staff has different loyalty.then follows m lines of queries.Each line only a number indicate the Serial number of whom should be fired.

Output

For every query print a number:the Serial number of whom would replace the losing job man,If there has no one to replace him,print -1.

Sample Input

1
3 2
0 100 99
1 101 100
1
2

Sample Output

2
-1

  题目大意 给定一棵树,每个点有两个权值,忠诚度和能力值,每次询问点x的子树中能力值大于它,忠诚度最高的一位的编号。

Solution 1 树分块

  因为查询的时候,查询一个节点的子树实际上是等于查询一段区间内的数据,所以考虑对dfs序进行分块。

  按照dfs序进行分块,块内按忠诚度进行排序,再记录后缀忠诚度最大值。

  根据常用套路,每次查询,对于块两端部分,暴力for。中间每个块lower_bound upper_bound一下查询合法的一段,然后用后缀忠诚度最大值进行更新答案就好了。

  (这是我比较笨的分块方法)

  设块的大小为s,块的数量为c,则总时间复杂度为

Code

 /**
* hdu
* Problem#4366
* Accepted
* Time: 748ms
* Memory: 8468k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; typedef class Staff {
public:
int loy;
int abi;
int id; boolean operator < (Staff b) const {
if(abi != b.abi) return abi < b.abi;
return loy < b.loy;
}
}Staff; boolean operator < (const int& x, const Staff& s) {
return x < s.abi;
} const int maxcsize = ;
typedef class Chunk {
public:
int len;
Staff sta[maxcsize];
int maxv[maxcsize];
int ans[maxcsize]; void init(Staff* lis, int from, int end) {
len = end - from;
for(int i = from; i < end; i++)
sta[i - from] = lis[i];
sort(sta, sta + len);
maxv[len] = -;
ans[len] = -;
for(int i = len - ; i >= ; i--) {
if(sta[i].loy > maxv[i + ])
maxv[i] = sta[i].loy, ans[i] = sta[i].id;
else
maxv[i] = maxv[i + ], ans[i] = ans[i + ];
}
} void getAns(int limit, int& rmaxv, int& rans) {
int pos = upper_bound(sta, sta + len, limit) - sta;
if(maxv[pos] > rmaxv)
rmaxv = maxv[pos], rans = ans[pos];
}
}Chunk; int n, m;
int cs, cc;
vector<int> *g;
Staff lis[];
Staff *nlis;
Chunk chs[]; inline void init() {
scanf("%d%d", &n, &m);
g = new vector<int>[(n + )];
nlis = new Staff[(n + )];
for(int i = , x; i < n; i++) {
scanf("%d%d%d", &x, &lis[i].loy, &lis[i].abi);
lis[i].id = i;
g[x].push_back(i);
}
lis[].loy = lis[].abi = ;
lis[].id = ;
cs = sqrt(n + 0.5);
} int cnt;
int visitID[], exitID[];
int visit[];
inline void dfs(int node) {
visitID[node] = ++cnt;
visit[cnt] = node;
for(int i = ; i < (signed)g[node].size(); i++)
dfs(g[node][i]);
exitID[node] = cnt;
} inline void init_chunks() {
for(int i = ; i <= n; i++)
nlis[i] = lis[visit[i]], nlis[i].id = i;
for(cc = ; cc * cs < n; cc++)
chs[cc + ].init(nlis, cc * cs + , min((cc + ) * cs, n) + );
} inline void solve() {
int l, r, x, maxv, ans, lim;
while(m--) {
scanf("%d", &x);
maxv = -, ans = -;
l = visitID[x], r = exitID[x], lim = lis[x].abi;
int lid = l / cs + , rid = r / cs + ;
if(lid == rid) {
for(int i = l; i <= r; i++)
if(nlis[i].abi > lim && nlis[i].loy > maxv)
maxv = nlis[i].loy, ans = i;
} else {
// if(x == 992)
// putchar('a');
for(int i = l; i <= lid * cs; i++)
if(nlis[i].abi > lim && nlis[i].loy > maxv)
maxv = nlis[i].loy, ans = i;
for(int i = (rid - ) * cs + ; i <= r; i++)
if(nlis[i].abi > lim && nlis[i].loy > maxv)
maxv = nlis[i].loy, ans = i;
for(int i = lid + ; i < rid; i++)
chs[i].getAns(lim, maxv, ans);
}
printf("%d\n", (ans == -) ? (-) : (visit[ans]));
}
} inline void clear() {
delete[] g;
delete[] nlis;
} int T;
int main() {
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
scanf("%d", &T);
while(T--) {
init();
cnt = ;
dfs();
init_chunks();
solve();
clear();
}
// fprintf(stderr, "Time: %dms\n", clock());
return ;
}

Successor(Tree Division)

Solution 2 CDQ分治

  每次查询相当于查询满足能力大于某个值,深度优先时间戳在某一段区间内的最大的忠诚度,感觉有那么一点像偏序的问题,所以上CDQ分治乱搞..

  分治能力,然后对于能力大于等于mid的节点可能会对能力小于mid的节点做出贡献,所以就用一个线段树维护区间最值,对于能力值大于等于mid的节点就在线段树内将它的深度优先时间戳那一位改为它的忠诚度。对于能力值小于mid的节点x就查询[visitID[x], exitID[x]]的最值就好了。

  每次询问O(1)解决。

  总时间复杂度.

  由于自己巨懒无比,CDQ分治从来都不手写队列,直接用vector。又因为线段树和STL中的vector常数巨大无比(当然还有我的代码自带某个比较大的常数),所以直接上就TLE了。把vector全都改成手写vector才过的。

Code

 /**
* hdu
* Problem#4366
* Accepted
* Time: 842ms
* Memory: 18776k
*/
#include <bits/stdc++.h>
using namespace std;
const signed int inf = (signed)((1u << ) - ); typedef class SegTreeNode {
public:
int val;
int maxid;
SegTreeNode *l, *r;
SegTreeNode():val(-), maxid(), l(NULL), r(NULL) { } inline void pushUp() {
if(l->val > r->val)
val = l->val, maxid = l->maxid;
else
val = r->val, maxid = r->maxid;
}
}SegTreeNode; SegTreeNode pool[];
SegTreeNode *top; inline SegTreeNode* newnode() {
top->val = -;
return top++;
} typedef class SegTree {
public:
SegTreeNode* root; SegTree():root(NULL) { }
SegTree(int n) {
build(root, , n);
} void build(SegTreeNode*& node, int l, int r) {
node = newnode();
if(l == r) return;
int mid = (l + r) >> ;
build(node->l, l, mid);
build(node->r, mid + , r);
} void update(SegTreeNode*& node, int l, int r, int idx, int val) {
if(l == r) {
node->val = val, node->maxid = l;
return;
}
int mid = (l + r) >> ;
if(idx <= mid) update(node->l, l, mid, idx, val);
else update(node->r, mid + , r, idx, val);
node->pushUp();
} int query(SegTreeNode*& node, int l, int r, int ql, int qr, int& maxid) {
if(l == ql && qr == r) {
maxid = node->maxid;
return node->val;
}
int mid = (l + r) >> ;
if(qr <= mid) return query(node->l, l, mid, ql, qr, maxid);
if(ql > mid) return query(node->r, mid + , r, ql, qr, maxid);
int a, b, c;
a = query(node->l, l, mid, ql, mid, c);
b = query(node->r, mid + , r, mid + , qr, maxid);
if(a > b) maxid = c;
return max(a, b);
}
}SegTree; template<typename T>
class Vector {
protected:
int cap;
int siz;
T* l;
public:
Vector():l(NULL), cap(), siz() { } inline void push_back(T x) {
if(l == NULL) {
l = new T[];
cap = , siz = ;
}
if(siz == cap) {
l = (T*)realloc(l, sizeof(T) * cap * ); //重新申请内存,并拷贝数组
cap = cap << ;
}
l[siz++] = x;
} T& operator [] (int pos) {
return l[pos];
} inline int size() {
return siz;
} inline int capt() {
return cap;
} inline void clear() {
delete[] l;
l = NULL;
}
}; int n, m;
int valmax;
Vector<int> *g;
Vector<int> a233;
int* loys, *abis;
SegTree st; inline void init() {
scanf("%d%d", &n, &m);
top = pool;
g = new Vector<int>[(n + )];
loys = new int[(n + )];
abis = new int[(n + )];
st = SegTree(n);
loys[] = inf, abis[] = inf;
a233.clear();
for(int i = , x; i < n; i++) {
scanf("%d%d%d", &x, loys + i, abis + i);
g[x].push_back(i);
a233.push_back(i);
}
} int buf[];
void discrete() {
memcpy(buf, abis, sizeof(int) * n);
sort(buf, buf + n);
valmax = unique(buf, buf + n) - buf;
for(int i = ; i < n; i++)
abis[i] = lower_bound(buf, buf + valmax, abis[i]) - buf + ;
} int cnt;
int visitID[], exitID[];
int visit[];
inline void dfs(int node) {
visitID[node] = ++cnt;
visit[cnt] = node;
for(int i = ; i < (signed)g[node].size(); i++)
dfs(g[node][i]);
exitID[node] = cnt;
} int maxvals[];
int ans[];
void CDQDividing(int l, int r, Vector<int>& q) {
if(q.size() <= ) return;
if(l == r) return; int mid = (l + r) >> , a, b; Vector<int> ql, qr;
for(int i = ; i < (signed)q.size(); i++)
if(abis[q[i]] > mid)
qr.push_back(q[i]), st.update(st.root, , n, visitID[q[i]], loys[q[i]]);
else
ql.push_back(q[i]); for(int i = ; i < (signed)ql.size(); i++) {
a = st.query(st.root, , n, visitID[ql[i]], exitID[ql[i]], b);
if(a > maxvals[ql[i]])
maxvals[ql[i]] = a, ans[ql[i]] = b;
} for(int i = ; i < (signed)qr.size(); i++)
st.update(st.root, , n, visitID[qr[i]], -); q.clear();
CDQDividing(l, mid, ql);
CDQDividing(mid + , r, qr);
} inline void solve() {
cnt = ;
dfs();
memset(maxvals, -, sizeof(int) * (n + ));
memset(ans, -, sizeof(int) * (n + ));
CDQDividing(, valmax, a233);
int x;
while(m--) {
scanf("%d", &x);
printf("%d\n", (ans[x] == -) ? (-) : (visit[ans[x]]));
}
} inline void clear() {
delete[] g;
delete[] loys;
delete[] abis;
} int T;
int main() {
scanf("%d", &T);
while(T--) {
init();
discrete();
solve();
clear();
}
return ;
}

Successor(CDQ Divide and Conquer)

Sulution 3 线段树合并

  对于上一种做法,为了满足子树的关系多带了个log,考虑直接dfs,每一个点先访问它的子树,再把子树信息合并起来,就可以满足子树关系了。

  现在考虑如何维护子树信息。对能力值开一个值域线段树,记录当前值域内最大的忠诚度。

  于是就成功把总时间复杂度优化成.

  然而常数更大了,所以没有快多少。

Code

 /**
* hdu
* Problem#4366
* Accepted
* Time: 436ms
* Memory: 39124k
*/
#include <bits/stdc++.h>
using namespace std;
const signed int inf = (signed)((1u << ) - ); typedef class SegTreeNode {
public:
int val;
int maxid;
SegTreeNode *l, *r;
SegTreeNode():val(-), maxid(-), l(NULL), r(NULL) { }
SegTreeNode(SegTreeNode *p):val(-), maxid(-), l(p), r(p) { } inline void pushUp() {
if(l->val > r->val)
val = l->val, maxid = l->maxid;
else
val = r->val, maxid = r->maxid;
}
}SegTreeNode; SegTreeNode pool[];
SegTreeNode null = SegTreeNode(&null);
SegTreeNode *top; inline SegTreeNode* newnode() {
top->val = -;
top->maxid = -;
top->l = top->r = &null;
return top++;
} void merge(SegTreeNode*& a, SegTreeNode* b) {
if(b == &null) return;
if(a == &null) {
a = b;
return;
}
if(b->val > a->val)
a->val = b->val, a->maxid = b->maxid;
merge(a->l, b->l);
merge(a->r, b->r);
// a->pushUp();
} typedef class SegTree {
public:
SegTreeNode* root; SegTree():root(&null) { } void update(SegTreeNode*& node, int l, int r, int idx, int val, int id) {
if(node == &null)
node = newnode();
if(l == r) {
if(val > node->val)
node->val = val, node->maxid = id;
return;
}
int mid = (l + r) >> ;
if(idx <= mid) update(node->l, l, mid, idx, val, id);
else update(node->r, mid + , r, idx, val, id);
node->pushUp();
} int query(SegTreeNode*& node, int l, int r, int ql, int qr, int& maxid) {
if(l == ql && qr == r) {
maxid = node->maxid;
return node->val;
}
int mid = (l + r) >> ;
if(qr <= mid) return query(node->l, l, mid, ql, qr, maxid);
if(ql > mid) return query(node->r, mid + , r, ql, qr, maxid);
int a, b, c;
a = query(node->l, l, mid, ql, mid, c);
b = query(node->r, mid + , r, mid + , qr, maxid);
if(a > b) maxid = c;
return max(a, b);
}
}SegTree; const int limval = ; int n, m;
int valmax;
vector<int> *g;
int* loys, *abis; inline void init() {
scanf("%d%d", &n, &m);
top = pool;
g = new vector<int>[(n + )];
loys = new int[(n + )];
abis = new int[(n + )];
loys[] = inf, abis[] = inf;
for(int i = , x; i < n; i++) {
scanf("%d%d%d", &x, loys + i, abis + i);
g[x].push_back(i);
}
} int buf[];
void discrete() {
memcpy(buf, abis, sizeof(int) * n);
sort(buf, buf + n);
valmax = unique(buf, buf + n) - buf;
for(int i = ; i < n; i++)
abis[i] = lower_bound(buf, buf + valmax, abis[i]) - buf + ;
} int ans[];
int maxv[];
SegTreeNode* dfs(int node) {
SegTree st;
// if(node == 10)
// fprintf(stderr, "gg");
for(int i = ; i < (signed)g[node].size(); i++) {
SegTreeNode* buf = dfs(g[node][i]);
merge(st.root, buf);
}
if(node) {
maxv[node] = st.query(st.root, , n, abis[node] + , n, ans[node]);
st.update(st.root, , n, abis[node], loys[node], node);
} else ans[node] = -;
return st.root;
} inline void solve() {
dfs();
int x;
while(m--) {
scanf("%d", &x);
printf("%d\n", (ans[x]));
}
} inline void clear() {
delete[] g;
delete[] loys;
delete[] abis;
} int T;
int main() {
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
scanf("%d", &T);
while(T--) {
init();
discrete();
solve();
clear();
}
return ;
}

hdu 4366 Successor - CDQ分治 - 线段树 - 树分块的更多相关文章

  1. HDU - 4366 Successor DFS区间+线段树

    Successor:http://acm.hdu.edu.cn/showproblem.php?pid=4366 参考:https://blog.csdn.net/colin_27/article/d ...

  2. HDU 4366 Successor( DFS序+ 线段树 )

    Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. hdu 5830 FFT + cdq分治

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. Successor HDU - 4366 (预处理,线段树,dfs序)

    Successor HDU - 4366 Sean owns a company and he is the BOSS.The other Staff has one Superior.every s ...

  5. HDU 6183 Color it cdq分治 + 线段树 + 状态压缩

    Color it Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Pro ...

  6. ACdream1157 Segments(CDQ分治 + 线段树)

    题目这么说的: 进行如下3种类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i (1-base) 删除第i条增加的线段, ...

  7. BZOJ3711 PA2014Druzyny(动态规划+cdq分治+线段树)

    显然可以dp:设f[i]为前i个人最多能分多少组,则f[i]=max{f[j]}+1 (cmax<=i-j<=dmin). 容易发现d的限制是一段连续区间,二分或者随便怎么搞都行.c则有点 ...

  8. hdu 5126 stars cdq分治套cdq分治+树状数组

    题目链接 给n个操作, 第一种是在x, y, z这个点+1. 第二种询问(x1, y1, z1). (x2, y2, z2)之间的总值. 用一次cdq分治可以将三维变两维, 两次的话就变成一维了, 然 ...

  9. HDU 5618 Jam's problem again (cdq分治+BIT 或 树状数组套Treap)

    题意:给n个点,求每一个点的满足 x y z 都小于等于它的其他点的个数. 析:三维的,第一维直接排序就好按下标来,第二维按值来,第三维用数状数组维即可. 代码如下: cdq 分治: #pragma ...

随机推荐

  1. NewWord

    identification: 鉴定,识别; 验明; 身份证明; 认同; peer:PEER-TO-PEER:同等延迟机制.根据网络中共享资源方式的不同,局域网有两种组织形式 filters: n. ...

  2. 原生js---ajax的封装插件.js---(对get和post做了兼容)

    function ajax(method,url,data,fn){ // 1.创建对象 var xhr=null; try{ xhr=new XMLHttpRequest(); }catch(e){ ...

  3. HDU 2276 Kiki & Little Kiki 2(矩阵位运算)

    Kiki & Little Kiki 2 转载自:点这里 [题目链接]Kiki & Little Kiki 2 [题目类型]矩阵位运算 &题意: 一排灯,开关状态已知,每过一秒 ...

  4. Linux系统安装nodejs

    参考文档 官网连接 镜像连接 安装方法有三种: 1. 源码安装(耗时) 2. apt-get / yum 安装(版本比较低) 3. 解压后创建软连接(推荐) 方法一. 1 ) 指定目录下下载源码包 $ ...

  5. Unity shader学习之Forward Rendering Path

    Forward rendering path shader如下: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObje ...

  6. meta 跳转

    强无敌. 是否厌倦了页面枯燥的跳转? 试试这样的跳转吧. Duration:表示滤镜特效的持续时间(单位:秒) Transition:滤镜类型.表示使用哪种特效,取值为0-23. <Meta h ...

  7. VMWare虚拟机 window文件传递

    无论是将虚拟机的文件传到window上或者是将window上文件传到虚拟机上: 都可以选中文件,然后拖动文件到另一个系统上 提前:虚拟机安装了VMWARE Tools 1)window上文件拖到虚拟机 ...

  8. python练习:一行搞定-统计一句话中每个单词出现的个数

    一行搞定-统计一句话中每个单词出现的个数 >>> s'i am a boy a bood boy a bad boy' 方式一:>>> dict([(i,s.spl ...

  9. Linux基础命令---忽略挂起信号nohup

    nohup nohup可以使程序能够忽略挂起信号,继续运行.用户退出时会挂载,而nohup可以保证用户退出后程序继续运行.如果标准输入是终端,请将其从/dev/null重定向.如果标准输出是终端,则将 ...

  10. 【JavaScript 6连载】一、关于对象(访问)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...