线段树 or 并查集 (多一个时间截点)
The company usually assigns some tasks to some employees to
finish.When a task is assigned to someone,He/She will assigned it to all
his/her subordinates.In other words,the person and all his/her
subordinates received a task in the same time. Furthermore,whenever a
employee received a task,he/she will stop the current task(if he/she
has) and start the new one.
Write a program that will help in figuring out some employee’s
current task after the company assign some tasks to some employee.
For each test case:
The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.
The following N - 1 lines each contain two integers u and v, which
means the employee v is the immediate boss of employee
u(1<=u,v<=N).
The next line contains an integer M (M ≤ 50,000).
The following M lines each contain a message which is either
"C x" which means an inquiry for the current task of employee x
or
"T x y"which means the company assign task y to employee x.
(1<=x<=N,0<=y<=10^9)OutputFor each test case, print the test case number (beginning
with 1) in the first line and then for every inquiry, output the
correspond answer per line.Sample Input
1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
Sample Output
Case #1:
-1
1
2 题意 : 每个老板有很多员工 , 当老板有任务时 , 他的所有员工都会执行此任务 , 并且弃掉他以前的任务。 思路 :
这题是在线段树专题里的 , 然后我自己构造了一个树 , 用 vector 存的点 , 但是超内存了 , 我也不知道什么鬼 , 搜网上的也有用 vector 写的 , 但他们没事 , 搞不懂 。 还有一种解法 , 是用并查集解的 , 他与普通的区别在于 他对每个节点在多加一个参数 ,表示当前变量出现的时间截点 。 代码示例 :
/*
* Author: ry
* Created Time: 2017/10/13 20:59:29
* File Name: 1.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 5e4+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long struct s
{
int task;
int time;
}po[eps]; int pre[eps]; int main() {
int t, n, m;
int a, b;
char ch[5];
int k = 1; cin >>t;
while (t--){
cin >>n;
for(int i = 1; i <= n; i++){
po[i].task = -1;
po[i].time = 0;
}
for(int i = 1; i <= n; i++) pre[i] = i;
for(int i = 1; i < n; i++){
scanf("%d%d", &a, &b);
pre[a] = b;
}
printf("Case #%d:\n", k++);
cin >>m;
int t = 0;
while (m--){
scanf("%s", ch);
if (ch[0] == 'C'){
scanf("%d", &a);
int ans = -1, time = 0;
int i = a;
while (pre[i] != i){
if (po[i].time > time) {
ans = po[i].task;
time = po[i].time;
}
i = pre[i];
}
if (po[i].time > time) ans = po[i].task;
printf("%d\n", ans);
}
else {
scanf("%d%d", &a, &b);
t++;
po[a].task = b;
po[a].time = t;
}
}
} return 0;
}
线段树 or 并查集 (多一个时间截点)的更多相关文章
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- BZOJ4399魔法少女LJJ——线段树合并+并查集
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- 【BZOJ2733】永无乡(线段树,并查集)
[BZOJ2733]永无乡(线段树,并查集) 题面 BZOJ 题解 线段树合并 线段树合并是一个很有趣的姿势 前置技能:动态开点线段树 具体实现:每次合并两棵线段树的时候,假设叫做\(t1,t2\), ...
- 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)
题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树. 开始和队友 ...
- 2018.09.30 bzoj4025: 二分图(线段树分治+并查集)
传送门 线段树分治好题. 这道题实际上有很多不同的做法: cdq分治. lct. - 而我学习了dzyo的线段树分治+并查集写法. 所谓线段树分治就是先把操作分成lognlognlogn个连续不相交的 ...
- 洛谷P4121 [WC2005]双面棋盘(线段树套并查集)
传送门 先膜一下大佬->这里 据说这题正解是LCT,然而感觉还是线段树套并查集的更容易理解 我们对于行与行之间用线段树维护,每一行内用并查集暴力枚举 每一行内用并查集暴力枚举连通块这个应该容易理 ...
- 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C
http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...
- Codeforces.1051G.Distinctification(线段树合并 并查集)
题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在 ...
- BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并
题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
随机推荐
- 浅谈集合框架三、Map常用方法及常用工具类
最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...
- python 使用PyInstaller遇到的坑
给个链接 https://github.com/yinghualuowu/Python_VLPR/releases 参数问题 在网上随处可以见到PyInstaller的参数教程. -F : 打包成单个 ...
- 如何通过命令行 msbuild 编译项目
本文告诉大家如何通过 msbuild 编译一个项目,通过命令行编译可以输出更多的编译信息,可以用来调试自己写的编译相关方法,可以看到是哪个文件编译失败 在开始菜单可以找到 VisualStudio 的 ...
- 从http到https--phpStudy2018
0. 将SSL证书解压到以下目录,申请方式见 百度 Apache/cert/ 分别更名为 my_public.crt my.key my_chain.crt 1. phpStudy->其它选项菜 ...
- linux 禁止所有中断
如果你需要禁止所有中断如何? 在 2.6 内核, 可能关闭在当前处理器上所有中断处理, 使用任一个下面 2 个函数(定义在 <asm/system.h>): void local_irq_ ...
- POJ 1797 Heavy Transportation(Dijkstra运用)
Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can no ...
- CSS选择器权重计算规则
从CSS代码存放位置看权重优先级:内嵌样式 > 内部样式表 > 外联样式表.其实这个基本可以忽视之,大部分情况下CSS代码都是使用外联样式表. 从样式选择器看权重优先级:important ...
- CF526F Pudding Monsters
CF526F Pudding Monsters 题目大意:给出一个\(n* n\)的棋盘,其中有\(n\)个格子包含棋子. 每行每列恰有一个棋子. 求\(k*k\)的恰好包含\(k\)枚棋子的子矩形个 ...
- HDU1172猜数字 [模拟]
1.题意 任务是猜一个四位数,每次尝试后会给出这次猜中了几个数字和猜中了几个位置,求能否根据尝试的记录给出答案 2.分析 数据给出查询次数和每次查询的数及其有几个数和几个位置符合,值得注意的是,猜对的 ...
- unmask
当我们登录系统之后创建一个文件总是有一个默认权限的,那么这个权限是怎么来的呢?这就是umask干的事情.umask 设置了用户创建文件的默认权限,它与chmod的效果刚好相反,umas ...