好题。   可是感觉题目描写叙述不是非常清楚

这题仅仅是询问开除某人后,他的下属中谁会替代他的位置。不会更新这个位置

要求一个子树中忠诚度最高的人。

能够想到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的更多相关文章

  1. HDU4366 Successor【dfs序 分块】

    HDU4366 Successor 题意: 给出一棵根为\(1\)的树,每个点有两个权值\(x,y\),每次询问一个点的子树中\(x\)比这个点的\(x\)大且\(y\)值最大的那个点 题解: 如果以 ...

  2. hdu4366 Successor (dfs序+zkw线段树)

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

  3. 【HDU4366】【DFS序+分块】Successor

    Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st ...

  4. [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 ...

  5. 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. 本题 ...

  6. 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 ...

  7. 71 Query Rank Min Max Successor of BST

    [本文链接] http://www.cnblogs.com/hellogiser/p/query-min-max-successor-of-bst.html [代码]  C++ Code  12345 ...

  8. LeetCode Inorder Successor in BST

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...

  9. 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 ...

随机推荐

  1. centos/rhel最小化安装图形化

    图形化,一般不再服务器中安装.为了提升系统的利用率. centos的yum源对应centos的源 RHEL的yum源对应RHEL的源 我演示的Centos6.5,我挂载的RHEL6.5的源.作为软件源 ...

  2. php替换str_replace的使用方法,支持多个替换

    废话不多说,直接上代码: str_replace(['a','b','c'],'a',$str);//a或b或c都替换成a str_replace(['a','b','c'],['d','e','f' ...

  3. centos7安装MySQL5.7无法设置密码问题

    前言 在使用centos7系统yum方式安装MySQL5.7后 不知道默认密码是多少  知道后没办法修改? 一.找到MySQL密码 service mysqld start vim /var/log/ ...

  4. HDUOJ---2082

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. PAT 1085 Perfect Sequence

    PAT 1085 Perfect Sequence 题目: Given a sequence of positive integers and another positive integer p. ...

  6. [转]一千行MySQL学习笔记

    Shocker /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL ...

  7. Java数据库表自动转化为PO对象

    本程序简单实现了数据库内省,生成PO对象. 数据库内省有如下两种实现方式: 通过mysql元表 通过desc table,show tables等命令 import java.io.IOExcepti ...

  8. 测试化工具XCTestCase

    layout: post title: "Xcode 7智能测试化工具XCTest学习" subtitle: "Xcode 7智能测试化工具XCTest学习" ...

  9. ASP中页面之间传递值的几种方式

    ASP.NET页面之间传递值的几种方式 页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryS ...

  10. 使用Apktools反编译apk应用

    使用Apktools反编译apk应用 1.获取APK的classes.dex文件: 得到你想要的应用的apk文件,用解压软件打开apk,从apk中复制出classes.dex文件. 2.classes ...