[Luogu] 网络
https://www.luogu.org/problemnew/show/P3250
树链剖分 + 线段树 + 优先队列
要求未被影响的请求中最大的
所以每次将每条路径在整棵树上的补集的每个节点的优先队列加入该路径的权值
mid 敲错调了一上午
气炸。。。
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <cstring>
- #include <string>
- #include <queue>
- #include <vector>
- using namespace std;
- const int N = 1e5 + ;
- #define yxy getchar()
- #define lson jd << 1
- #define rson jd << 1 | 1
- #define RR freopen("gg.in", "r", stdin)
- int n, Ty, Tim, how, ans;
- int fa[N], topp[N], deep[N], size[N], son[N], tree[N];
- priority_queue <int> Q1[N << ], Q2[N << ];
- vector <int> vec[N << ];
- struct Node{int up, down;} D[N];
- struct Node2{int l, r, val;} Ask[N << ];
- inline int read() {
- int x = ; char c = yxy;
- while(c < '' || c > '') c = yxy;
- while(c >= '' && c <= '') x = x * + c - '', c = yxy;
- return x;
- }
- void Dfs_son(int u, int f_, int dep) {
- fa[u] = f_;
- deep[u] = dep;
- size[u] = ;
- int siz_ = vec[u].size();
- for(int i = ; i < siz_; i ++) {
- int v = vec[u][i];
- if(v != f_) {
- Dfs_son(v, u, dep + );
- size[u] += size[v];
- if(size[v] > size[son[u]]) son[u] = v;
- }
- }
- }
- void Dfs_un(int u, int tp) {
- topp[u] = tp;
- tree[u] = ++ Tim;
- if(!son[u]) return ;
- Dfs_un(son[u], tp);
- int siz_ = vec[u].size();
- for(int i = ; i < siz_; i ++) {
- int v = vec[u][i];
- if(v != fa[u] && v != son[u]) Dfs_un(v, v);
- }
- }
- inline bool cmp(Node a, Node b) {return a.up < b.up;}
- void Sec_G(int l, int r, int jd, int x, int y, int yj) {
- if(x <= l && r <= y) {
- if(how == ) Q1[jd].push(yj);
- else Q2[jd].push(yj);
- return ;
- }
- int mid = (l + r) >> ;
- if(x <= mid) Sec_G(l, mid, lson, x, y, yj);
- if(y > mid) Sec_G(mid + , r, rson, x, y, yj);
- }
- inline void Sec_G_imp(int x, int y, int val) {
- int tp1 = topp[x], tp2 = topp[y], js();
- while(tp1 != tp2) {
- if(deep[tp1] < deep[tp2]) swap(x, y), swap(tp1, tp2);
- D[++ js].up = tree[tp1];
- D[js].down = tree[x];
- x = fa[tp1];
- tp1 = topp[x];
- }
- if(deep[x] < deep[y]) swap(x, y);
- D[++ js].up = tree[y]; D[js].down = tree[x];
- sort(D + , D + js + , cmp);
- int last = ;
- for(int i = ; i <= js; i ++) {
- int l_ = last + , r_ = D[i].up - ;
- if(l_ <= r_) Sec_G(, n, , l_, r_, val);
- last = D[i].down;
- }
- int l_ = last + , r_ = n;
- if(l_ <= r_) Sec_G(, n, , l_, r_, val);
- }
- int get_answer(int jd) {
- int ret = -;
- while(!Q2[jd].empty() && (Q1[jd].top() == Q2[jd].top())) Q1[jd].pop(), Q2[jd].pop();
- if(Q1[jd].empty()) return -;
- return Q1[jd].top();
- }
- void Sec_A(int l, int r, int jd, int x) {
- ans = max(ans, get_answer(jd));
- if(l == r) return ;
- int mid = (l + r) >> ;
- if(x <= mid) Sec_A(l, mid, lson, x);
- else Sec_A(mid + , r, rson, x);
- }
- int main() {
- n = read();
- Ty = read();
- for(int i = ; i < n; i ++) {
- int u = read(), v = read();
- vec[u].push_back(v);
- vec[v].push_back(u);
- }
- Dfs_son(, , );
- Dfs_un(, );
- for(int i = ; i <= Ty; i ++) {
- int opt = read();
- if(opt == ) { //添加
- Ask[i].l = read(), Ask[i].r = read(), Ask[i].val = read();
- how = ;
- Sec_G_imp(Ask[i].l, Ask[i].r, Ask[i].val);
- } else if(opt == ) { //删除
- int x = read();
- how = -;
- Sec_G_imp(Ask[x].l, Ask[x].r, Ask[x].val);
- } else { //查询
- int x = read();
- ans = -;
- Sec_A(, n, , tree[x]);
- cout << ans << endl;
- }
- }
- return ;
- }
[Luogu] 网络的更多相关文章
- [Luogu 2604] ZJOI2010 网络扩容
[Luogu 2604] ZJOI2010 网络扩容 第一问直接最大流. 第二问,添加一遍带费用的边,边权 INF,超级源点连源点一条容量为 \(k\) 的边来限流,跑费用流. 大约是第一次用 nam ...
- luogu 1327 数列排序 & 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J题 循环节
luogu 1327 数列排序 题意 给定一个数列\(\{an\}\),这个数列满足\(ai≠aj(i≠j)\),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? ...
- BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...
- P3376 【模板】网络最大流(luogu)
P3376 [模板]网络最大流(luogu) 最大流的dinic算法模板(采取了多种优化) 优化 时间 inline+当前弧+炸点+多路增广 174ms no 当前弧 175ms no 炸点 249 ...
- 【Luogu P3376】网络最大流
Luogu P3376 最大流是网络流模型的一个基础问题. 网络流模型就是一种特殊的有向图. 概念: 源点:提供流的节点(入度为0),类比成为一个无限放水的水厂 汇点:接受流的节点(出度为0),类比成 ...
- LOJ #2547 Luogu P4517「JSOI2018」防御网络
好像也没那么难写 LOJ #2547 Luogu P4517 题意 在一棵点仙人掌中等概率选择一个点集 求选出点集的斯坦纳树大小的期望 定义点仙人掌为不存在一个点在多个简单环中的连通图 斯坦纳树为在原 ...
- Luogu P2002 消息扩散&&P1262 间谍网络
怕自己太久没写Tarjan了就会把这种神仙算法忘掉. 其实这种类型的图论题的套路还是比较简单且显然的. P2002 消息扩散 很显然的题目,因为在一个环(其实就是强连通分量)中的城市都只需要让其中一个 ...
- 【题解】Luogu P2604 [ZJOI2010]网络扩容
原题传送门:P2604 [ZJOI2010]网络扩容 这题可以说是板题 给你一个图,先让你求最大流 再告诉你,每条边可以花费一些代价,使得流量加一 问至少花费多少代价才能使最大流达到k 解法十分简单 ...
- 【luogu P1262 间谍网络】 题解
题目链接:https://www.luogu.org/problemnew/show/P1262 注意: 1.缩点时计算出入度是在缩完点的图上用color计算.不要在原来的点上计算. 2.枚举出入度时 ...
随机推荐
- Scratch第四十九讲:完美的下落和反弹
做了很多小游戏,都会遇到碰撞和反弹的情况,CC哥大多时候也都是简单处理一下,包括之前的讲座也有提过,但是没有认真的讲解过.今天就专门为这个主题做一讲,把这部分内容彻底讲透,大家可以一起探讨一下. 是不 ...
- ELK日志分析系统的搭建
一.ELK简介 ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心组件. Elasticsearch是数据存储.搜索.分析引擎,功能非常强大:Logstash是日志 ...
- 美化linux客户端zsh和oh-my-zsh
linuxbashzshoh-my-zsh 一.安装zsh 二.安装oh-my-zsh 一.安装zsh 安装 zsh yum -y install zsh 替换默认shell chsh -s /bin ...
- VBA术语(三)
在本章中,将介绍常用的Excel VBA术语.这些术语将在很多的模块中使用,因此理解其中的每一个术语都很重要. 模块 模块是编写代码的区域.如下图中,这是一个新的工作簿,因此没有任何模块. 要插入模块 ...
- IE6图片透明问题
网上很多解决IE6下png透明问题的方案,但是经本人实践,有的时候有用,有的时候并不能解决自己的问题.当是后者的时候,想到另外一种办法,就是当在IE6.IE7下使用gif图片,自己在测试的时候,如果g ...
- XCode5环境下利用crash log调试线上Crash的流程
1.前言 本文主要介绍在XCode5环境下,如何根据App自己生成的crashlog来调试真机上运行时产生的crash问题. 2. 步骤 (1)构造一段会crash的代码,并放到viewDidLoad ...
- stm32 ds18b20 温度传感器
相关文章:http://blog.csdn.net/zhangxuechao_/article/details/74991985 举例 void DS18B20_in() { GPIO_InitTyp ...
- Netty——基本使用介绍
https://blog.csdn.net/haoyuyang/article/details/53243785 1.为什么选择Netty 上一篇文章我们已经了解了Socket通信(IO/NIO/AI ...
- 【Struts2】Json插件使用
一.使用步骤 1.1 引入依赖 1.2 在struts.xml文件中配置 一.使用步骤 1.1 引入依赖 <!-- https://mvnrepository.com/artifact/org. ...
- 【leetcode】637. Average of Levels in Binary Tree
原题 Given a non-empty binary tree, return the average value of the nodes on each level in the form of ...