loj 2011
对于第 $i$ 天的询问
前 $i - c - 1$ 天都会影响答案
主席树维护
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define int long long #define LL long long #define gc getchar()
inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 2e5 + ; int Root[N], Lson[N * ], Rson[N * ], W[N * ];
struct Node {int v, nxt;} G[N];
int head[N], cnt;
int n, q, Rt; int fa[N], deep[N], size[N], topp[N], son[N], tree[N], treejs; inline void Link(int u, int v) {G[++ cnt].v = v; G[cnt].nxt = head[u]; head[u] = cnt;} void Dfs_1(int u, int f_, int depth) {
fa[u] = f_, deep[u] = depth, size[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v == f_) continue;
Dfs_1(v, u, depth + );
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
} void Dfs_2(int u, int tp) {
topp[u] = tp, tree[u] = ++ treejs;
if(!son[u]) return ;
Dfs_2(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa[u] && v != son[u]) Dfs_2(v, v);
}
} inline int Lca(int x, int y) {
int tpx = topp[x], tpy = topp[y];
while(tpx != tpy) {
if(deep[tpx] < deep[tpy]) swap(x, y), swap(tpx, tpy);
x = fa[tpx], tpx = topp[x];
}
return deep[x] < deep[y] ? x : y;
} int Hjt; void Fill(int x, int y) {
Lson[x] = Lson[y], Rson[x] = Rson[y], W[x] = W[y];
} #define lson jd << 1
#define rson jd << 1 | 1 void Sec_G(int &rt, int l, int r, int x) {
Fill(++ Hjt, rt);
rt = Hjt;
W[rt] ++;
if(l == r) return ;
int mid = (l + r) >> ;
if(x <= mid) Sec_G(Lson[rt], l, mid, x);
else Sec_G(Rson[rt], mid + , r, x);
} int Ans; void Sec_A(int rt, int l, int r, int x, int y) {
if(!rt) return ;
if(x <= l && r <= y) {
Ans += W[rt];
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_A(Lson[rt], l, mid, x, y);
if(y > mid ) Sec_A(Rson[rt], mid + , r, x, y);
} inline int Sec_A_imp(int rt, int x, int y) {
if(rt <= ) return ;
int ret = , tpx = topp[x], tpy = topp[y];
while(tpx != tpy) {
if(deep[tpx] < deep[tpy]) swap(x, y), swap(tpx, tpy);
Ans = ;
Sec_A(Root[rt], , n, tree[tpx], tree[x]);
ret += Ans;
x = fa[tpx], tpx = topp[x];
}
if(deep[x] < deep[y]) swap(x, y);
Ans = ;
Sec_A(Root[rt], , n, tree[y], tree[x]);
ret += Ans;
return ret;
} main() {
n = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i <= n; i ++) {
int v = read();
if(!v) Rt = i;
else Link(v, i);
}
Dfs_1(Rt, , );
Dfs_2(Rt, Rt);
q = read();
for(int i = ; i <= q; i ++) {
int opt = read();
Root[i] = Root[i - ];
if(opt == ) {
int x = read(), y = read(), C = read();
int a = deep[x] + deep[y] - * deep[Lca(x, y)] + ;
int b = Sec_A_imp(i - C - , x, y);
cout << a << " " << b << "\n";
} else {
int x = read();
Sec_G(Root[i], , n, tree[x]);
}
}
return ;
}
loj 2011的更多相关文章
- LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On
题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有 N×M 个这样 ...
- 【LOJ】 #2011. 「SCOI2015」情报传递
题解 一写过一交A的一道数据结构水题 我们发现大于C可以转化为这条路径上有多少个在某天之前开始调查的情报员,离线全部读入,变成树上路径查询某个区间的数出现过多少次,构建一棵根缀的主席树,查询的时候用两 ...
- LOJ 一本通一句话题解系列:
第一部分 基础算法 第 1 章 贪心算法 1):「一本通 1.1 例 1」活动安排:按照结束时间排序,然后扫一遍就可以了. 2):「一本通 1.1 例 2」种树:首先要尽量的往区间重叠的部分种树,先按 ...
- [C#项目开源] MongoDB 可视化管理工具 (2011年10月-至今)
正文 该项目从2011年10月开始开发,知道现在已经有整整5年了.MongoDB也从一开始的大红大紫到现在趋于平淡. MongoCola这个工具在一开始定位的时候只是一个Windows版本的工具,期间 ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3028 Solved: 1460[Submit][Sta ...
- Step by step Dynamics CRM 2011升级到Dynamics CRM 2013
原创地址:http://www.cnblogs.com/jfzhu/p/4018153.html 转载请注明出处 (一)检查Customizations 从2011升级到2013有一些legacy f ...
- http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
http://www.cnblogs.com/youring2/archive/2011/03/28/1997694.html
- Microsoft Dynamics CRM 2013 and 2011 Update Rollups and Service Packs
Microsoft Dynamics CRM 2013 BTW: RC stands for Release for Candidate, and RTM stands for Release ...
- http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html
http://www.blogjava.net/zhangchao/archive/2011/05/26/351051.html
随机推荐
- 数据结构-单链表-类定义C++
原理可访问https://www.cnblogs.com/yang901112/p/11674333.html 头文件 #ifndef RLIST_H #define RLIST_H #include ...
- 机器学习之Bagging与随机森林笔记
集成学习通过将多个学习器进行结合,常可获得比单一学习器显著优越的泛化性能.这对“弱学习器”尤为明显,因此集成学习的很多理论研究都是针对弱学习器进行的,而基学习器有时也被直接称为弱学习器.虽然从理论上来 ...
- 微软商店一直安装不上Intel Media SDK DFP
具体表现为一直安装失败,但是下载进度条一直在,无法去除. 此方法来自 https://answers.microsoft.com/en-us/windows/forum/all/error-code- ...
- SPA中使用jwt
什么是jwt? JSON Web Token (JWT),它是目前最流行的跨域身份验证解决方案 JWT的工作原理 1. 是在服务器身份验证之后,将生成一个JSON对象并将其发送回用户,示例如下:{&q ...
- Authorization
授权,也叫访问控制,即在应用中控制谁访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角色 ...
- EfCore基本用法
db first 和 code first的基本使用方法 https://www.cnblogs.com/Starts_2000/p/mysql-efcore20-codefirst-dbfirst- ...
- Element ui tree 搜索
搜索框 属性 :filter-node-method="filterNode" 对树节点进行筛选时执行的方法,返回 true 表示这个节点可以显示,返回 false 则表示这个节点 ...
- centos7 GNOME 安装微信客户端
写在前边 最近新装了一个 centos7 GNOME 系统,用了很久了 win,突然转换 linux 桌面版,觉得焕然一新,给搬砖生活增添了一份新意 ~ 先看一下效果图: 怎么弄呢? 下载最新版本 t ...
- Python标准库3.4.3-urllib.request-21.6
21.6. urllib.request — Extensible library for opening URLs 翻译:Z.F. The urllib.request module defines ...
- Ubuntu16.04, CUDA8 CUDNN6 下安装 Tensorflow-gpu, Keras, Pytorch, fastai
如何访问tensorflow官方网站 tensorflow官方网站变为:https://tensorflow.google.cn/ 安装深度学习框架 0. ubuntu查看CUDA和cuDNN版本 C ...