http://acm.hdu.edu.cn/showproblem.php?pid=4366

今日重新做了这题的分块,果然是隔太久了,都忘记了。。

首先,用DFS序变成一维的问题

关键是它有两个权值,该如何处理呢?

首先假设我们的DFS序列是List,

那么,对其进行分块。对于每一个块,先按能力排序,用数组tosort[]保存,这样我就可以用O(magic)的时间,就是扫一次这个块,维护出一个数组,mx[i]表示大于等于tosort[i].ablity时,最大的忠诚度。

那么我查询的时候,就可以,如果不在块里的,暴力,否则,因为每一个块已经排好序,那么我可以二分出一个位置,找到第一个能力值大于等于所查询的val,那么mx[pos]是答案,因为mx[pos]就表示大于等于这个位置的能力值,所拥有的最大忠诚度。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
struct Edge {
int u, v, w;
int tonext;
}e[ * maxn];
int num;
int first[maxn + ];
int getid[ + ];
struct node {
int loy, abi;
bool operator < (const struct node & rhs) const {
return abi < rhs.abi;
}
}a[maxn], List[maxn], tosort[maxn];
void addEgde(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
int DFN;
int L[maxn], R[maxn];
bool vis[maxn];
void dfs(int cur) {
L[cur] = DFN;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
// assert(vis[v] == false);
// vis[v] = true;
++DFN;
List[DFN] = tosort[DFN] = a[v];
dfs(v);
}
R[cur] = DFN;
}
int magic;
int mx[maxn];
int tofind(int begin, int end, int val) {
// cout << begin << " " << end << " ***" << endl;
if (tosort[end].abi <= val) return -;
if (tosort[begin].abi > val) return mx[begin];
while (begin <= end) {
int mid = (begin + end) >> ;
if (tosort[mid].abi > val) {
end = mid - ;
} else begin = mid + ;
}
return mx[begin];
}
void work() {
int n, m;
cin >> n >> m;
magic = (int)sqrt(n * 1.0);
for (int i = ; i <= n - ; ++i) {
int fa;
cin >> fa >> a[i].loy >> a[i].abi;
getid[a[i].loy] = i;
addEgde(fa, i);
}
dfs();
// for (int i = 0; i <= n - 1; ++i) {
// cout << L[i] << " " << R[i] << endl;
// }
for (int i = ; i < n; i += magic) {
int j = i + magic;
if (j > n) break;
sort(tosort + i, tosort + j);
mx[j - ] = tosort[j - ].loy;
for (int k = j - ; k >= i; --k) {
if (tosort[k].loy < mx[k + ]) {
mx[k] = mx[k + ];
} else mx[k] = tosort[k].loy;
}
// for (int k = i; k < i + magic; ++k) {
// cout << mx[k] << endl;
// }
}
while (m--) {
int id;
cin >> id;
int val = a[id].abi;
int begin = L[id], end = R[id];
// cout << begin << " " << end << " ***" << endl;
int ans = -;
for (int i = begin + ; i <= end;) {
if (i % magic == && i + magic <= end) {
ans = max(ans, tofind(i, i + magic - , val));
i += magic;
} else {
if (List[i].abi > val && ans < List[i].loy) {
ans = List[i].loy;
}
++i;
}
}
if (ans == -) {
cout << - << endl;
} else cout << getid[ans] << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
a[].abi = a[].loy = -;
List[] = tosort[] = a[];
int t;
cin >> t;
while (t--) {
DFN = ;
num = ;
memset(mx, -, sizeof mx);
memset(first, , sizeof first);
work();
}
return ;
}

这题可以用线段树做。

对于这一类题目,就是要维护的东西有两个的。

1、能力值要比所查询的元素大

2、要在一定的区间里查询这些值。

有一个很好用的方法就是排序。

我们肯定是要排除一个因数的影响的了。

我们可以按能力排序,然后先查询能力值最大的(肯定是-1)

然后把它插进线段树,然后找次大的。

这样就确保了每个元素查询的,都是合法的(就是里面的元素,能力值都比他大)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
const int maxn = + ;
struct Edge {
int u, v, w;
int tonext;
}e[ * maxn];
int num;
int first[maxn + ];
int getid[ + ];
struct node {
int loy, abi, id;
bool operator < (const struct node & rhs) const {
if (abi != rhs.abi) return abi > rhs.abi;
else return id < rhs.id;
}
}a[maxn], List[maxn], tosort[maxn];
void addEgde(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
int DFN;
int LQ[maxn], RQ[maxn];
void dfs(int cur) {
LQ[cur] = DFN;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
++DFN;
List[DFN] = tosort[DFN] = a[v];
dfs(v);
}
RQ[cur] = DFN;
}
int mx[maxn << ];
void pushUp(int cur) {
mx[cur] = max(mx[cur << ], mx[cur << | ]);
}
void UpDate(int pos, int val, int L, int R, int cur) {
if (L == R) {
if (pos == L) {
mx[cur] = val; //wa
}
return;
}
int mid = (L + R) >> ;
if (pos <= mid) UpDate(pos, val, lson);
else UpDate(pos, val, rson);
pushUp(cur);
}
int query(int begin, int end, int L, int R, int cur) {
if (L >= begin && R <= end) {
return mx[cur];
}
int mid = (L + R) >> ;
int lans = -inf, rans = -inf;
if (mid < end) rans = query(begin, end, rson);
if (mid >= begin) lans = query(begin, end, lson);
return max(lans, rans);
}
int ans[maxn];
void work() {
int n, m;
cin >> n >> m;
for (int i = ; i <= n - ; ++i) {
int fa;
cin >> fa >> a[i].loy >> a[i].abi;
a[i].id = i;
getid[a[i].loy] = i;
addEgde(fa, i);
}
dfs();
sort(a, a + n);
memset(mx, -, sizeof mx);
for (int i = ; i < n; ++i) {
int id;
if (i == n - ) id = ;
else id = getid[a[i].loy];
int res = query(LQ[id], RQ[id], , n - , );
if (res == -) ans[id] = -;
else ans[id] = getid[res];
UpDate(LQ[id], a[i].loy, , n - , );
}
for (int i = ; i <= m; ++i) {
int id;
cin >> id;
cout << ans[id] << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS; int t;
cin >> t;
while (t--) {
a[].abi = a[].loy = -inf;
a[].id = ;
DFN = ;
num = ;
memset(first, , sizeof first);
work();
}
return ;
}

HDU 4366 Successor 分块做法的更多相关文章

  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 - CDQ分治 - 线段树 - 树分块

    Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...

  3. HDU 4366 Successor(dfs序 + 分块)题解

    题意:每个人都有一个上司,每个人都有能力值和忠诚值,0是老板,现在给出m个询问,每次询问给出一个x,要求你找到x的所有直系和非直系下属中能力比他高的最忠诚的人是谁 思路:因为树上查询很麻烦,所以我们直 ...

  4. HDU - 4366 Successor DFS序 + 分块暴力 or 线段树维护

    给定一颗树,每个节点都有忠诚和能力两个参数,随意指定一个节点,要求在它的子树中找一个节点代替它,这个节点要满足能力值大于它,而且是忠诚度最高的那个. 首先,dfs一下,处理出L[i], R[i]表示d ...

  5. HDU 4366 Successor

    Successor Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  6. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

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

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

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

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

  9. Successor HDU - 4366 分块

    代码+注释: 1 /* 2 题意: 3 一共有n个人,其中0号是总裁(金字塔顶尖).后面输入其他n-1个人的信息啊a.b.c,分别代表第i个人的上级是a,他的 4 忠诚度为b,他的能力为c.后面有m次 ...

随机推荐

  1. log Configuration

    Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...

  2. 使用zxing编写的二维码生成解析工具:QRCoder

    zxing GitHub地址 QRCoder GitHub地址 TipDialog.java package com.wolf_pan; import java.util.Timer; import ...

  3. UI标签库专题三:JEECG智能开发平台 FormValidation(表单提交及验证标签)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/28484209  自己定义弹出框提示 ...

  4. (28)java web的hibernate使用

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  5. codeforces 414B B. Mashmokh and ACM(dp)

    题目链接: B. Mashmokh and ACM time limit per test 1 second memory limit per test 256 megabytes input sta ...

  6. Linux下使用pip安装keras

    Keras是一个底层使用Theano或TensorFlow的深度学习框架,它的设计参考了Torch,用Python语言编写,也很方便使用Python调用,是一个高度模块化的神经网络库,支持使用GPU和 ...

  7. Flask log配置,实现按照日期自动生成日志文件

    Flask自带了logger模块,用来方便程序员群众记录日志,这里粘贴出来的是一段代码,用来初始化日志各项配置参数,并根据日期自动生成日志文件. #log配置,实现日志自动按日期生成日志文件def m ...

  8. 【前端】CentOS 7 系列教程之三: 搭建 git 服务器

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_3.html 上一篇我们安装好了git,这一篇我们搭建git服务器 创建一个用户组 groupadd g ...

  9. 【旧文章搬运】《从PEB获取内存中模块列表》的补充

    原文发表于百度空间,2008-7-26========================================================================== 继续研究PE ...

  10. 关于数据库优化2——关于表的连接顺序,和where子句的前后顺序,是否会影响到sql的执行效率问题

    有好多时候,我们常听别人说大表在前,小表在后,包括现在好多百度出来的靠前的答案都有说数据库是从右到左加载的,所以from语句最后关联的那张表会先被处理.如果三表交叉,就选择交叉表来作为基础表.等等一些 ...