线段树 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 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
随机推荐
- [转]Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)
Redis的集群方案大致有三种:1)redis cluster集群方案:2)master/slave主从方案:3)哨兵模式来进行主从替换以及故障恢复. 一.sentinel哨兵模式介绍Sentinel ...
- SVN中如何执行clean up
在要清理的文件夹上点右键,菜单:TortoiseSVN--选择cleanup,会出现一个菜单栏,在你菜单栏有一个属性breaklock意思是打破锁定,你勾选打破锁定,然后cleanup就会成功,之后再 ...
- js实现小数点四舍五入
js实现小数点四舍五入 其实这个问题,在之前的面试中被提问到了,由于笔者平时都是用原生的toFixed()的方法来保留小数点,所以当时并没有回答出来这个问题,呜呜呜~.~
- 1626 - Brackets sequence——[动态规划]
Let us define a regular brackets sequence in the following way: Empty sequence is a regular sequence ...
- linux预备知识
我们正在接近去看一些实际的模块代码. 但是首先, 我们需要看一些需要出现在你的模块 源码文件中的东西. 内核是一个独特的环境, 它将它的要求强加于要和它接口的代码上. 大部分内核代码包含了许多数量的头 ...
- 【u107】数字游戏(bds)
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行 ...
- ajaxStart()和ajaxStop()方法
使用ajaxSetup()方法设置全局Ajax默认选项 使用ajaxSetup()方法可以设置Ajax请求的一些全局性选项值,设置完成后,后面的Ajax请求将不需要再添加这些选项值,它的调用格式为: ...
- Linux 内核存取配置空间
在驱动已探测到设备后, 它常常需要读或写 3 个地址空间: 内存, 端口, 和配置. 特别 地, 存取配置空间对驱动是至关重要的, 因为这是唯一的找到设备被映射到内存和 I/O 空间的位置的方法. 因 ...
- jmeter安装配置教程及使用
背景: 因为双11,黑五快到了,所有的互联网电商行业都要做一件事情,那就是压测,常见的压测很多区分,接口压测和全链路压测.线上压测和线下压测,单元压测和功能压测.我们这里介绍一下接口压测和全链路压测. ...
- 【Kubernetes】部署K8s-dashboard v1.10.1
一.官方kubernetes-dashboard.yaml简介 ①首先认识一下官方的kubernetes-dashboard.yaml,我们先下载: https://github.com/kubern ...