Luogu 4216 [SCOI2015]情报传递
BZOJ 4448。
写起来很愉悦的题。
按照时间可持久化线段树,修改就在原来的地方加$1$即可,查询就直接询问$root_1 - root_{now - c - 1}$中相应的个数。
主席树维护树链剖分即可。
时间复杂度$O(nlog^2n)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std; const int N = 2e5 + ; int n, rt, qn, tot = , head[N];
int dfsc = , id[N], siz[N], fa[N], top[N], son[N], dep[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void swap(int &x, int &y) {
int t = x; x = y; y = t;
} void dfs1(int x, int fat, int depth) {
fa[x] = fat, dep[x] = depth, siz[x] = ;
int maxson = -;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs1(y, x, depth + ); siz[x] += siz[y];
if(siz[y] > maxson) {
son[x] = y;
maxson = siz[y];
}
}
} void dfs2(int x, int topf) {
top[x] = topf, id[x] = ++dfsc;
if(!son[x]) return;
dfs2(son[x], topf);
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fa[x] || y == son[x]) continue;
dfs2(y, y);
}
} namespace PSegT {
struct Node {
int lc, rc, sum;
} s[N * ]; int root[N], nodeCnt = ; #define lc(p) s[p].lc
#define rc(p) s[p].rc
#define mid ((l + r) >> 1)
#define sum(p) s[p].sum void ins(int &p, int l, int r, int x, int pre) {
s[p = ++nodeCnt] = s[pre];
++sum(p);
if(l == r) return; if(x <= mid) ins(lc(p), l, mid, x, lc(pre));
else ins(rc(p), mid + , r, x, rc(pre));
} int query(int r1, int r2, int l, int r, int x, int y) {
if(x <= l && y >= r) return sum(r2) - sum(r1); int res = ;
if(x <= mid) res += query(lc(r1), lc(r2), l, mid, x, y);
if(y > mid) res += query(rc(r1), rc(r2), mid + , r, x, y); return res;
} } using namespace PSegT; inline void solve(int r) {
int x, y, c, res = ;
read(x), read(y), read(c);
r -= c + ;
int px = x, py = y; for(; top[x] != top[y]; ) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
if(r > ) res += query(root[], root[r], , n, id[top[x]], id[x]);
x = fa[top[x]];
} if(dep[x] > dep[y]) swap(x, y);
if(r > ) res += query(root[], root[r], , n, id[x], id[y]); printf("%d %d\n", dep[px] + dep[py] - * dep[x] + , res);
} int main() {
read(n);
for(int fat, i = ; i <= n; i++) {
read(fat);
if(!fat) rt = i;
else add(i, fat), add(fat, i);
}
dfs1(rt, , ), dfs2(rt, rt); read(qn);
for(int op, i = ; i <= qn; i++) {
root[i] = root[i - ];
read(op);
if(op == ) {
int x; read(x);
ins(root[i], , n, id[x], root[i]);
} else solve(i);
} return ;
}
Luogu 4216 [SCOI2015]情报传递的更多相关文章
- BZOJ_4448_[Scoi2015]情报传递_主席树
BZOJ_4448_[Scoi2015]情报传递_主席树 Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有 若T名(可能没有) ...
- BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树
4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...
- 【BZOJ4448】[Scoi2015]情报传递 主席树+LCA
[BZOJ4448][Scoi2015]情报传递 Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员能有若干名(可能没有)下线,除1名大头 ...
- [SCOI2015]情报传递[树剖+主席树]
[SCOI2015]情报传递 题意大概就是 使得在 \(i\) 时刻加入一个情报员帮您传情报 然后询问 \(x,y,c\) 指 \(x\)到\(y\)多少个人有风险-(大于c)的都有风险-每天风险值+ ...
- 洛谷 4216 BZOJ 4448 [SCOI2015]情报传递
[题解] 每个情报员的危险值val[i]应该是一个分段函数,前面一段是平行于x轴的横线,后面一段是一次函数.我们可以用fx(t)=t-b[x]表示这个一次函数.每次询问一条链上fx(t)大于c的点的个 ...
- [luogu] P4155 [SCOI2015]国旗计划(贪心)
P4155 [SCOI2015]国旗计划 题目描述 A 国正在开展一项伟大的计划 -- 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此 ...
- [倍增]luogu P4155 [SCOI2015]国旗计划
题面 https://www.luogu.com.cn/problem/P4155 问在环上最少取多少个区间能完全覆盖环 分析 首先发现是环,先把端点变为2n方便处理,注意离散化 其次要删去贡献不如其 ...
- bzoj4448 [Scoi2015]情报传递
第一问不解释,对于第二问的处理,可以使用cdq分治,假设分治的询问区间是[L,R],那么我们对于标号在[L,mid]的修改操作赋予一个权值,因为在当前[L,R]中[L,mid]的修改操作只会对[mid ...
- bzoj 4448 [Scoi2015]情报传递(主席树,LCA)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4448 [题意] 给定一颗树,询问一条路径上权值小于t-c的点数. [思路] 将一个2查 ...
随机推荐
- 剑指offer--13.二进制中1的个数
就是猜测试数据没有负数,哈哈 ----------------------------------------------------------------- 时间限制:1秒 空间限制:32768K ...
- Android 禁止屏幕旋转、避免转屏时重启Activity
一.禁止屏幕旋转 在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android:screenOrientation属性: 可选项: landscape = 横 ...
- Python的交互模式和命令行模式
Pyhton的交互模式 在终端输入Python3命令就会进入家Python的交互模式,在交互模式下,输入一行代码,回车,就会执行这行代码. Python的命令行模式 在终端输入Python3 1.py ...
- nginx之 nginx-1.9.7 编译安装、理论简介
nginx是一个web网站常用的高性能http和反向代理服务器,其具有较好的并发能力,被网易.百度.腾讯.新浪等网站广泛使用. 一. 理论简介 1.首先弄清楚正向代理和反向代理 正向代理:代理客户端, ...
- FPGA中竞争冒险问题的研究
什么是竞争冒险? 1 引言 现场可编程门阵列(FPGA)在结构上由逻辑功能块排列为阵列,并由可编程的内部连线连接这些功能块,来实现一定的逻辑功能. FPGA可以替代其他PLD或者各种中小规模数 ...
- 根据VM的TAG开停机
公有云一个非常大的优势,就是可以根据需求进行开停机.由于计费是按时进行的,从而实现节省成本. Azure上用脚本按时开停机已经有很多部署的案例.本文将介绍采用VM Tag中规定的时间进行开停机的脚本. ...
- 往jdk/bin目录中增加tcnative-1.dll文件以后报错 Can't load AMD 64-bit .dll on a IA 32-bit platform
开始时,运行Tomcat控制台报错: The APR based Apache Tomcat Native library which allows optimal performance in pr ...
- spring学习十二 application/x-www-form-urlencoded还是application/json
application/x-www-form-urlencoded还是application/json get. POST 用哪种格式? 后台如何得到这些值? 如何用ajax 或者是 postman ...
- PowerDesignerPDM中搜寻表名或字段名
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl '当前model '获取当前活 ...
- Eloquent ORM模型中添加自定义值
我们都知道通过Laravel中数据库查询出来的模型对象都是基于数据库字段,今天给大家展示一个 Laravel Eloquent ORM 模型特性-附加值不存在于数据表中. 举个简单的栗子,一篇文章(p ...