线段树 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 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
随机推荐
- [转]Spring 注解大全与详解
Spring使用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...
- [转]在Windows中安装Memcached
Memcached是一个高并发的内存键值对缓存系统,它的主要作用是将数据库查询结果,内容,以及其它一些耗时的计算结果缓存到系统内存中,从而加速Web应用程序的响应速度. Memcached最开始是作为 ...
- vscode编辑如何保存时自动校准eslint规范
在日常开发中,一个大点的项目会有多人参与,那么可能就会出现大家的代码风格不一,各显神通,这个时候就要祭出我们的eslint. 在这之前磨刀不误砍柴工,我们先来配置一下我们的代码编辑工具,如何在vsco ...
- java声明异常(throws)
在可能出现异常的方法上声明抛出可能出现异常的类型: 声明的时候尽可能声明具体的异常,方便更好的处理. 当前方法不知道如何处理这种异常,可将该异常交给上一级调用者来处理(非RuntimeExceptio ...
- CCPC2018 桂林 D "Bits Reverse"
传送门 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ,,) means chan ...
- 【21.58%】【codeforces 746D】Green and Black Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- css3颜色+透明度渐变
.linear { width: 630px; height: 120px; line-height: 150px; text-align: center; font-size: 26px; posi ...
- HDU6581 Vacation (HDU2019多校第一场1004)
HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...
- CentOS 7 端口白名单设置
# 查看白名单列表 firewall-cmd --zone=public --list-ports # 添加白名单端口 firewall-cmd --zone=public --add-port=/t ...
- Linux 2>&1的意思
2>&1的意思是将标准错误(2)也定向到标准输出(1)的输出文件中. 我们来具体了解下:Linux 中三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1, ...