[SPOJ 4155]OTOCI
Description
给你 \(n\) 个节点,让你兹磁以下操作,维护一棵树:
- 动态加边;
- 修改点权;
- 询问路径上点权和。
\(1\leq n\leq 30000\)
Solution
好久不打 \(lct\) 了,水一发。 \(lct\) 板子题。
好像 \(lct\) 的题都是板子。
我也不知道我怎么找到这么多板子题的...
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 30000;
int n, m, u, v; char ch[20];
struct Link_Cut_Tree {
int ch[N+5][2], val[N+5], sum[N+5], pre[N+5], rev[N+5], isrt[N+5];
Link_Cut_Tree () {for (int i = 1; i <= N; i++) isrt[i] = 1; }
void pushdown(int o) {
if (rev[o] == 0) return; int ls = ch[o][0], rs = ch[o][1];
swap(ch[ls][0], ch[ls][1]), swap(ch[rs][0], ch[rs][1]);
rev[ls] ^= 1, rev[rs] ^= 1, rev[o] = 0;
}
void pushup(int o) {sum[o] = sum[ch[o][0]]+sum[ch[o][1]]+val[o]; }
void push(int o) {if (isrt[o] == 0) push(pre[o]); pushdown(o); }
void rotate(int o, int kind) {
int p = pre[o];
ch[p][!kind] = ch[o][kind], pre[ch[o][kind]] = p;
if (isrt[p]) isrt[p] = 0, isrt[o] = 1; else ch[pre[p]][ch[pre[p]][1] == p] = o;
pre[o] = pre[p];
ch[o][kind] = p, pre[p] = o;
pushup(p), pushup(o);
}
void splay(int o) {
push(o);
while (isrt[o] == 0) {
if (isrt[pre[o]]) rotate(o, ch[pre[o]][0] == o);
else {
int p = pre[o], kind = ch[pre[p]][0] == p;
if (ch[p][kind] == o) rotate(o, !kind), rotate(o, kind);
else rotate(p, kind), rotate(o, kind);
}
}
}
void access(int o) {
int y = 0;
while (o) {
splay(o);
isrt[ch[o][1]] = 1, isrt[ch[o][1] = y] = 0;
pushup(o), o = pre[y = o];
}
}
void makeroot(int o) {access(o); splay(o); rev[o] ^=1, swap(ch[o][0], ch[o][1]); }
void split(int x, int y) {makeroot(x); access(y); splay(y); }
void link(int x, int y) {makeroot(x); pre[x] = y; }
void cut(int x, int y) {split(x, y); ch[y][0] = pre[x] = 0, isrt[x] = 1; pushup(y); }
void update(int x, int key) {makeroot(x); val[x] = key; pushup(x); }
int query(int x, int y) {split(x, y); return sum[y]; }
int find(int x) {access(x), splay(x); while (ch[x][0]) x = ch[x][0]; return x; }
}T;
void work() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &T.val[i]);
scanf("%d", &m);
while (m--) {
scanf("%s%d%d", ch, &u, &v);
if (ch[0] == 'b') {
if (T.find(u)^T.find(v)) {puts("yes"); T.link(u, v); }
else puts("no");
}else if (ch[0] == 'p') T.update(u, v);
else {
if (T.find(u)^T.find(v)) puts("impossible");
else printf("%d\n", T.query(u, v));
}
}
}
int main() {work(); return 0; }
[SPOJ 4155]OTOCI的更多相关文章
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- SPOJ - OTOCI LCT
OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
随机推荐
- c语言博客第二次作业
一.PTA实验作业 题目1:计算分段函数[2] 1.实验代码 { double x,y; scanf("%lf",&x); if(x>=0) { y=pow(x,0. ...
- 201621123054 《Java程序设计》第六周实验总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 1.2 可选:使用常规方法总结其他上课内容. 2 ...
- Argparse简易教程
Argparse简易教程 原文:Argparse Tutorial 译者:likebeta 本教程是对于Python标准库中推荐使用的命令行解析模块argparse的简单介绍. PS:还有其他两个模块 ...
- edittext实现自动查询,刷新listview
mEdittextqueryvalue.addTextChangedListener(new TextWatcher() { @Override pub ...
- 支付宝sdk集成,报系统繁忙 请稍后再试(ALI64)
移动快捷支付,往往需要集成支付宝的sdk,集成的过程相对简单,只要按照支付宝的文档,进行操作一般不会出问题. 下面主要说明一下,集成sdk后报"系统繁忙 请稍后再试(A ...
- nat和napt技术
私网IP地址是指内部网络或主机的IP地址,公网IP地址是指在因特网上全球唯一的IP地址. RFC 1918为私有网络预留出了三个IP地址块,如下: A类:10.0.0.0-10.255.255.255 ...
- Python内置函数(64)——classmethod
英文文档: classmethod(function) Return a class method for function. A class method receives the class as ...
- Python内置函数(2)——divmod
英文文档: divmod(a, b) Take two (non complex) numbers as arguments and return a pair of numbers consisti ...
- istio入门(00)istio的学习资源
官网:https://istio.io/ 理论知识: http://www.uml.org.cn/wfw/201710131.asp 环境搭建: http://dockone.io/article/2 ...
- istio入门(04)istio的helloworld-部署构建
参考链接: https://zhuanlan.zhihu.com/p/27512075 安装Istio目前仅支持Kubernetes,在部署Istio之前需要先部署好Kubernetes集群并配置好k ...