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 ...
随机推荐
- Android网络开发之WIFI
WIFI全称Wireless Fidelity, 又称802.11b标准.WIFI联盟成立于1999年,当时的名称叫做Wireless Ethernet Compatibility Alliance( ...
- HDUOJ---(2203)亲和串
亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 那些恶心人的Screen基本概念
Screen的这些基本概念中,最重要的就是dip的理解,而理解dip就是理解android适配不同设备的关键. Screen Size 实际物理尺寸.就是我们常说的3.5英寸屏幕,4.7英寸屏幕等等, ...
- 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.'
产生这个错误的原因是,StaticResource必须先定义再引用,但是DynamicResource就没有这个限制,为避免这个错误出现,可将StaticResource的定义放在Window.xam ...
- 关系数据库元数据处理类(一) 创建MSSQL元数据具体处理类
public class SqlServer : BaseMetadata { public SqlServer(string connectionString) : base(new DbUtili ...
- 以太网帧格式、IP数据报格式、TCP段格式+UDP段格式 详解
转载:http://www.cnblogs.com/lifan3a/articles/6649970.html 以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 1.ISO开放系统有 ...
- 使用xshell远程登录ubuntu使用vi编辑不能使用删除键方向键
近期安装了xshell,远程登录上ubuntu后,在插入模式下,按删除键没有任何反应,按方向键分别打印出A.B.C.D,每个字符一行. 这是因为ubuntu初始化安装的是vi的tiny版本,解决办法安 ...
- python3 functools partial 用于函数的包装器详解
一.partial 的作用: partial 用于对一个已有函数进行包装,达到功能的定制的目的. 二.例子: 假设我们要完成两个功能,第一个功能是完成两个数相加,第二个功能是给一个自增一下 1.传统方 ...
- linux分享一:进程全攻略--守护进程(服务)
概括: 进程是程序的运行实例.进程对应一个唯一的进程PID, 统一程序的多个实例可以同时运行,他们的pid互不相同. 进程一般分为交互进程.批处理进程和守护进程(daemons)三类 一:什么是守护进 ...
- php批量删除数据库下指定前缀的表
如何用php批量删除数据库下所有前缀为prefix_的表. 例子,统一删除前缀为“prefix_”的表. <?php //设置数据库连接信息.数据库服务器地址,数据库用户名,数据密码 mysql ...