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. Xamarin Android 记事本(三)删改

    这篇我就不做太多的说明了,数据操作之前也都举过例子了,这里就直接贴出删除和修改的代码. public override bool OnOptionsItemSelected(IMenuItem ite ...

  2. A Practical Introduction to Blockchain with Python

    A Practical Introduction to Blockchain with Python // Adil Moujahid // Data Analytics and more http: ...

  3. Springboot读取自定义的yml文件中的List对象

    Yml文件(novellist.xml)如下: novellist:   list:     - name: 笑傲江湖       type: 武侠       master: 令狐冲       a ...

  4. UIView局部点击

    今天上班遇到一种情况,需要局部响应点击事件,比如在一个UIImageView中设置一个小圆圈图片,要求点击圆圈里面不响应点击,点击小圆圈外面的部分响应点击.可以通过重写hitTest:withEven ...

  5. bzoj5406: Gift

    全程膜拜 码得都要一样了.. 对于这种数列置换的可以理解成多个环,而对于一个大小为d的环把顺序弄对要做d-1次 总起来就是n-环数的次数 加上暴力30pt到手啦 假如题目没有限制,那就是第一类斯特林数 ...

  6. hdu 1548 A strange lift 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目意思:给出 n 个 floor 你,每个floor 有一个数k,按下它可以到达 floor ...

  7. codeforces 448B. Suffix Structures 解题报告

    题目链接:http://codeforces.com/problemset/problem/448/B 题目意思:给出两种操作automaton:可以删除字符串中任意一个字符: array:交换字符串 ...

  8. WAS:服务器停电后,重启DMGR,控制台访问不了

    1.   今天有现场WAS服务器停电,重启DMGR后,控制台网页打不开: 后来得知,防火墙可能有问题.(虽然之前该机器上防火墙是关着的,但掉电后,防火墙会重启规则) 关掉防火墙后,问题解决. 2.   ...

  9. 「网络流24题」「Codevs1237」 餐巾计划问题

    1237 餐巾计划问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond   题目描述 Description 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相 ...

  10. Spring中Bean获取IOC容器服务的方法

    Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...