hdu4366 Successor
好题。 可是感觉题目描写叙述不是非常清楚
这题仅仅是询问开除某人后,他的下属中谁会替代他的位置。不会更新这个位置
要求一个子树中忠诚度最高的人。
能够想到dfs树。保留时间戳。每一个节点便表示一个区间
那么便能够建树维护最高忠诚度。。。仅仅是要保证能力值也要比被开除者高
那么依据能力值从大到小对员工排序,依次更新。那么能够保证之前更新的节点的能力值都大于当前要查询的节点
这里要注意一点,能力值同样的员工要同一时候查询和更新
最后一点是。。
。按理说更新时应该更新这个员工表示的区间 可是这样会超时
事实上仅仅用更新此员工区间的第一个值就能够了,由于查询的时候是员工表示的区间。那么必定能够查询到更新的这个值
记得数组开大一点。。。非常easyRE
//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm> #include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstdlib> using namespace std;
//LOOP
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s) #define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
#define SZ(V) (int)V.size()
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
#define sqr(x) x * x #define LL(x) ((x) << 1)
#define RR(x) ((x) << 1 | 1)
typedef vector <int> VI;
typedef unsigned long long ULL;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 50010;
const double eps = 1e-10;
const LL MOD = 1e9 + 9; int n, m, dfs_c;
int s[maxn], e[maxn];
int ans[maxn];
map<int, int> mm; struct Node{
int id, loy, ab;
bool operator<(const Node& x) const{
if (ab != x.ab)
return ab > x.ab;
return id < x.id;
}
}a[maxn]; struct Seg{
int l, r, num;
}seg[maxn * 5]; VI t[maxn]; void dfs(int u, int fa)
{
s[u] = ++dfs_c;
REP(i, t[u].size())
{
int v = t[u][i];
if (v != fa)
dfs(v, u);
}
e[u] = ++dfs_c;
} void build(int l, int r, int rt )
{
seg[rt].num = -1;
seg[rt].l = l, seg[rt].r = r;
if (l == r)
return;
int mid = (l + r) >> 1;
build(l, mid, LL(rt));
build(mid + 1, r, RR(rt));
} void update(int pos, int val, int rt)
{
if (seg[rt].l == seg[rt].r && seg[rt].l == pos)
{
// cout << "pos " << pos << ' ' << val<< endl;
seg[rt].num = val;
return;
}
int mid = (seg[rt].l + seg[rt].r) >> 1;
if (pos <= mid)
update(pos, val, LL(rt));
else
update(pos, val, RR(rt));
seg[rt].num = max(seg[LL(rt)].num, seg[RR(rt)].num);
} int query(int l, int r, int rt)
{
if (seg[rt].l == l && seg[rt].r == r)
return seg[rt].num;
int mid = (seg[rt].l + seg[rt].r) >> 1;
if (r <= mid)
return query(l, r, LL(rt));
else if (l > mid)
return query(l, r, RR(rt));
return max(query(l, mid, LL(rt)), query(mid + 1, r, RR(rt)));
} int main()
{
int T; RI(T);
while (T--)
{
RII(n, m);
REP(i, n + 1) t[i].clear();
dfs_c = 0; mm.clear();
int x, y, z;
a[0].id = 0, a[0].loy = -1, a[0].ab = -1;
FE(i, 1, n - 1)
{
RIII(x, y, z);
t[x].PB(i);
a[i].loy = y, a[i].ab = z, a[i].id = i;
mm[y] = i;
}
dfs(0, -1);
// cout << "dfs Done" << endl;
build(1, dfs_c, 1);
// cout << "build Done" << endl;
sort(a, a + n);
CLR(ans, -1);
for (int i = 0, j; i < n; i = j)
{
j = i;
while (j < n && a[j].ab == a[i].ab)
{
int tmp = query(s[a[j].id], e[a[j].id], 1);
if (mm.count(tmp))
ans[a[j].id] = mm[tmp];
// cout << tmp << endl;
j++;
}
for (int k = i; k < j; k++)
update(s[a[k].id], a[k].loy, 1);
// for (int k = 1; k < 3 * dfs_c; k++)
// printf(" L: %d R:%d num:%d\n", seg[k].l, seg[k].r , seg[k].num);
}
while (m--)
{
RI(x);
WI(ans[x]);
}
}
return 0;
}
/* */
hdu4366 Successor的更多相关文章
- HDU4366 Successor【dfs序 分块】
HDU4366 Successor 题意: 给出一棵根为\(1\)的树,每个点有两个权值\(x,y\),每次询问一个点的子树中\(x\)比这个点的\(x\)大且\(y\)值最大的那个点 题解: 如果以 ...
- hdu4366 Successor (dfs序+zkw线段树)
Successor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 【HDU4366】【DFS序+分块】Successor
Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st ...
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...
- Leetcode 285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 本题 ...
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- 71 Query Rank Min Max Successor of BST
[本文链接] http://www.cnblogs.com/hellogiser/p/query-min-max-successor-of-bst.html [代码] C++ Code 12345 ...
- LeetCode Inorder Successor in BST
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...
- 285. Inorder Successor in BST
题目: Given a binary search tree and a node in it, find the in-order successor of that node in the BST ...
随机推荐
- zookeeper单节点和多节点配置
单机单节点模式 zookeeper解压, 放到 /opt/zookeeper/下, 新建一个latest的软链 $ latest 将 conf/zoo-sample.cfg 重命名为 zoo.cfg, ...
- 每一个软件开发人员绝对必须掌握的关于 Unicode 和字符集的最基础的知识
2013-02-05 14:18 48人阅读 评论(0) 收藏 举报 关键字: Unicode, Character Set, 字符集, UTF-8, ANSI, ASCII, UTF-7 ...
- OpenStack 网络:Neutron 初探
OpenStack Neutron 网络模型 OpenStack nova-network 独立成为单独的组件 Neutron 后,形象的网络模型的多平面网络.混合平面私有网络.如图 3,图 4,图 ...
- 【laravel5.4】引入自定义类库+卸载已有的自定义库(以引入钉钉应用为例)composer dumpautoload -o
本文之前,首先感谢: Azeroth_Yang 传送门:https://blog.csdn.net/zwrj1130/article/details/73467320 强烈建议引入的类 都是含有命名 ...
- 给定随机数列求第k大的数字
原来STL我还是有很多不知道的地方 STL 采用的算法是: 当数组长度 <= 3时, 采用插入排序. 当长度 > 3时, 采用快排 Partition 的思想,也就是说类似快速排序(这里不 ...
- ubuntu(14.04) remote access(远程连接数据库)
1.修改mysql的配置文件. /etc/mysql/my.cnf 把 bind-address 的那行代码注释掉,保存退出,重启mysql
- Android 小技巧-- TextView与EditText 同步显示
方法一.利用View.OnKeyListener"同步"显示 EditText myEdit = (EditText)findViewById(R.id.myEdit); Tex ...
- C# 自定义文件格式并即时刷新注册表 非关闭explorer
转自:http://blog.csdn.net/zhangtirui/article/details/4309492 最近公司在做一个项目 用到关于自定义格式的文件,但在注册表图标更改后 文件图标 ...
- python练习笔记——map | sum | pow 的应用
1 函数简要 map 函数 | sum 函数 | pow函数 | lambda函数 2 简要计算 2.1 1^2 + 2^2 + 3^2 .....9^2 方法1 print([pow(x,2 ...
- Vim进阶技术:搜索和替换
行内搜索 行内搜索,也就是在当前行内进行搜索和移动,通常都与编辑命令一起使用. fx -- 移动到下一个字符x的位置,光标停留在x字符上面 tx -- 移动到下一个字符x的位置,光标停留在x前一个字符 ...