ref不是太懂……

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, tot, val[200005], fa[200005], idx[200005], num, lf[200005], cnt;
int ans[200005], ch[200005][2], sum[200005], rg[200005], opt, uu, vv, ww;
struct Node{
int pos, idx, u, v;
bool operator<(const Node &x)const{
if(pos!=x.pos) return pos<x.pos;
return idx<x.idx;
}
}nd[400005];
int getW(int x){
return ch[fa[x]][1]==x;
}
void upd(int x){
sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + val[x];
}
bool isRoot(int x){
return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
}
void rotate(int x){
int old=fa[x], oldf=fa[old], w=getW(x);
if(!isRoot(old)) ch[oldf][ch[oldf][1]==old] = x;
ch[old][w] = ch[x][w^1]; ch[x][w^1] = old;
fa[ch[x][w^1]] = x; fa[ch[old][w]] = old; fa[x] = oldf;
upd(old); upd(x);
}
void splay(int x){
while(!isRoot(x)){
int f=fa[x];
if(!isRoot(f)) rotate(getW(f)==getW(x)?f:x);
rotate(x);
}
upd(x);
}
int access(int x){
int y=0;
while(x){
splay(x);
ch[x][1] = y;
upd(x);
y = x;
x = fa[x];
}
return y;
}
void cut(int x){
access(x);
splay(x);
fa[ch[x][0]] = 0;
ch[x][0] = 0;
upd(x);
}
int main(){
cin>>n>>m;
val[1] = lf[1] = tot = num = idx[1] = 1;
rg[1] = n;
fa[++tot] = 1;
int now=tot;
for(int i=1; i<=m; i++){
scanf("%d", &opt);
if(!opt){
scanf("%d %d", &uu, &vv);
val[++tot] = 1; idx[++num] = tot; lf[num] = uu; rg[num] = vv;
fa[tot] = now;
}
else if(opt==1){
scanf("%d %d %d", &uu, &vv, &ww);
uu = max(uu, lf[ww]); vv = min(vv, rg[ww]);
if(uu>vv) continue;
fa[++tot] = now;
nd[++cnt] = (Node){uu, i-m, tot, idx[ww]};
nd[++cnt] = (Node){vv+1, i-m, tot, now};
now = tot;
}
else{
scanf("%d %d %d", &ww, &uu, &vv);
nd[++cnt] = (Node){ww, i, idx[uu], idx[vv]};
}
}
sort(nd+1, nd+1+cnt);
int k=1;
memset(ans, -1, sizeof(ans));
for(int i=1; i<=n; i++){
for(; k<=cnt && nd[k].pos==i; k++){
if(nd[k].idx<=0){
cut(nd[k].u);
fa[nd[k].u] = nd[k].v;
}
else{
access(nd[k].u); splay(nd[k].u); int qwq=sum[nd[k].u];
int t=access(nd[k].v); splay(nd[k].v); qwq += sum[nd[k].v];
access(t); splay(t); qwq -= sum[t] << 1;
ans[nd[k].idx] = qwq;
}
}
}
for(int i=1; i<=m; i++)
if(ans[i]>=0)
printf("%d\n", ans[i]);
return 0;
}

loj2092 「ZJOI2016」大森林的更多相关文章

  1. 「ZJOI2016」大森林 解题报告

    「ZJOI2016」大森林 神仙题... 很显然线段树搞不了 考虑离线操作 我们只搞一颗树,从位置1一直往后移动,然后维护它的形态试试 显然操作0,1都可以拆成差分的形式,就是加入和删除 因为保证了操 ...

  2. @loj - 2092@ 「ZJOI2016」大森林

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Y 家里有一个大森林,里面有 n 棵树,编号从 1 到 n. ...

  3. 「ZJOI2016」解题报告

    「ZJOI2016」解题报告 我大浙的省选题真是超级神仙--这套已经算是比较可做的了. 「ZJOI2016」旅行者 神仙分治题. 对于一个矩形,每次我们从最长边切开,最短边不会超过 \(\sqrt{n ...

  4. 「ZJOI2016」旅行者 解题报告

    「ZJOI2016」旅行者 对网格图进行分治. 每次从中间选一列,然后枚举每个这一列的格子作为起点跑最短路,进入子矩形时把询问划分一下,有点类似整体二分 至于复杂度么,我不会阿 Code: #incl ...

  5. LOJ#2230. 「BJOI2014」大融合

    LOJ#2230. 「BJOI2014」大融合 题目描述 小强要在$N$个孤立的星球上建立起一套通信系统.这套通信系统就是连接$N$个点的一个树.这个树的边是一条一条添加上去的. 在某个时刻,一条边的 ...

  6. 2090. 「ZJOI2016」旅行者 分治,最短路

    2090. 「ZJOI2016」旅行者 链接 loj 思路 \((l,mid)(mid+1,r)\).考虑跨过mid的贡献. 假设选的中间那条线的点为gzy,贡献为\(dis(x,gzy)+dis(g ...

  7. 【ZJOI2016】大♂森林

    题目描述 小Y家里有一个大森林,里面有 $n$ 棵树,编号从 $1$ 到 $n$ .一开始这些树都只是树苗,只有一个节点,标号为 $1$ .这些树都有一个特殊的节点,我们称之为生长节点,这些节点有生长 ...

  8. 「NOI2014」魔法森林

    题目链接 戳我 \(Solution\) 两个变量,emm...不好搞啊. 于是我们可以按照\(A\)排序.然后动态加边,因为\(A\)是越来越大,所以不需要管他,只要使得\(1\)~\(n\)的路径 ...

  9. @loj - 2090@ 「ZJOI2016」旅行者

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Y 来到了一个新的城市旅行.她发现了这个城市的布局是网格状的 ...

随机推荐

  1. leetcode85 Maximal Rectangle

    思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...

  2. 配置Linux环境

    安装mariadb +主从复制https://www.cnblogs.com/pyyu/p/9467289.html 参考博客 redis https://www.cnblogs.com/pyyu/p ...

  3. pandas error记录随笔

    1.sys:1: DtypeWarning: Columns (0,1) have mixed types. Specify dtype option on import or 解决办法:PANDAS ...

  4. 关于Mybatis的pagehelper使用遇到的坑

    参考博客: https://blog.csdn.net/wzyxdwll/article/details/66473466 下面给出pagehelp使用的配置, 在springmvc中的配置: 下面是 ...

  5. iOS开发:自定义带下划线文本的UIButton

    Uiunderlinedbutton.h代码 @interface UIUnderlinedButton : UIButton { } + (UIUnderlinedButton *) underli ...

  6. POJ 3046 Ant Counting(递推,和号优化)

    计数类的问题,要求不重复,把每种物品单独考虑. 将和号递推可以把转移优化O(1). f[i = 第i种物品][j = 总数量为j] = 方案数 f[i][j] = sigma{f[i-1][j-k], ...

  7. POJ-3080 Blue Jeans---字符串+暴力

    题目链接: https://vjudge.net/problem/POJ-3080 题目大意: 找最长的公共字串(长度>=3),长度相同就找字典序最小的 解题思路: 枚举第一个串的所以子串,处理 ...

  8. flash + php对称密钥加密的交互

    这几天研究了下php和flash中的对称密钥加密的交互问题,经过研究以后决定,在项目中使用aes加密.问题也就来了,在flash中的加密数据如何与php的amf进行数据交互,最终决定使用base64编 ...

  9. Oracle数据库学习(三)

    6.关于null 数据库中null是一个未知数,没有任何值:进行运算时使用nvl,但是结果仍为空:在聚集函数中只有全部记录为空才会返回null. 7.insert插入 (1)单行记录插入 insert ...

  10. 二十八、MySQL 元数据

    MySQL 元数据 你可能想知道MySQL以下三种信息: 查询结果信息: SELECT, UPDATE 或 DELETE语句影响的记录数. 数据库和数据表的信息: 包含了数据库及数据表的结构信息. M ...