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. 将线上服务器生成的日志信息实时导入kafka,采用agent和collector分层传输,app的数据通过thrift传给agent,agent通过avro sink将数据发给collector,collector将数据汇集后,发送给kafka

    记flume部署过程中遇到的问题以及解决方法(持续更新) - CSDN博客 https://blog.csdn.net/lijinqi1987/article/details/77449889 现将调 ...

  2. filter、servlet、interceptor的执行顺序

    1. Filter可认为是Servlet的一种“变种”,它主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理,是个典型的处理链.它与Servlet的区别在于:它不能 ...

  3. SpringBoot启动跟踪

    程序启动入口 @SpringBootApplication public class Chapter001Application { public static void main(String[] ...

  4. jconsole工具检测堆内存变化的使用

    jconsole将Java写的程序检测. 从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行.您可以轻松地使 ...

  5. js判断字符串是否包含某个字符串

    String对象的方法 1,indexOf() (推荐) 方法可返回某个指定的字符串值在字符串中首次出现的位置.如果要检索的字符串值没有出现,则该方法返回 -1 var str = "123 ...

  6. vi编辑器设置行号可见

    vi 设置行号 需要切换到命令模式下,输入set number :set number 按下回车即可

  7. ubuntu 源、codename 与 sources.list 文件

    查看 codename $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubunt ...

  8. java -- 虚拟机和内存

    从大方向来分:栈内存,堆内存,方法区,本地方法栈,程序计数器 java从存储数据的角度来分: 寄存器(register):最快的存储区,由编译器根据需求进行分配,不由认为控制. 堆栈(statck): ...

  9. 当打开一个.h或.cpp文件时, Solution Explorer就自动展开文件所在的目录

    当打开一个.h或.cpp文件时,  Solution Explorer就自动展开文件所在的目录: 如果不想展开: Tools           -> Options           -&g ...

  10. bzoj 3172: [Tjoi2013]单词【AC自动机】

    一眼AC自动机,就是先把串建一个自动机,标记每个串在自动机上的位置,然后加上间隔符连成一个串在自动机上跑,每跑到一个点就说明这个串以及它到root的所有点表示的串都要被更新一次 先在点上打上标记,最后 ...