BZOJ 2157: 旅游 (结构体存变量)
用结构体存变量好像确实能提高运行速度,以后就这么写数据结构了
Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 200003
#define inf 100000
#define ll long long
#define lson t[x].ch[0]
#define rson t[x].ch[1]
#define setIO(s) freopen(s".in" , "r" , stdin)
using namespace std;
int sta[N], n;
struct Node
{
int ch[2], f , siz , rev ;
ll val , sum , mul , min , max;
}t[N << 1];
inline int get(int x)
{
return t[t[x].f].ch[1] == x;
}
inline int isrt(int x)
{
return (!(t[t[x].f].ch[0] == x || t[t[x].f].ch[1] == x)) || (!x);
}
inline void mark_rev(int x)
{
swap(lson , rson), t[x].rev ^= 1;
}
inline void mark_mul(int x, ll y)
{
t[x].sum *= y, t[x].val *= y, t[x].mul *= y;
t[x].min *= y, t[x].max *= y, swap(t[x].min, t[x].max);
}
inline void pushdown(int x)
{
if(t[x].rev)
{
if(lson) mark_rev(lson);
if(rson) mark_rev(rson);
t[x].rev = 0;
}
if(t[x].mul != 1)
{
if(lson) mark_mul(lson , t[x].mul);
if(rson) mark_mul(rson , t[x].mul);
t[x].mul = 1;
}
}
inline void pushup(int x)
{
t[x].siz = t[lson].siz + t[rson].siz + 1;
t[x].sum = t[lson].sum + t[rson].sum + t[x].val ;
t[x].max = max(t[lson].max , t[rson].max) ;
t[x].min = min(t[lson].min , t[rson].min) ;
if(x > n)
{
t[x].max = max(t[x].max , t[x].val);
t[x].min = min(t[x].min , t[x].val);
}
}
inline void rotate(int x)
{
int old = t[x].f , fold = t[old].f , which = get(x);
if(!isrt(old)) t[fold].ch[t[fold].ch[1] == old] = x;
t[old].ch[which] = t[x].ch[which ^ 1], t[t[old].ch[which]].f = old;
t[x].ch[which ^ 1] = old, t[old].f = x, t[x].f = fold;
pushup(old), pushup(x);
}
inline void splay(int x)
{
int v = 0, u = x, i ;
for(sta[++ v] = u ; !isrt(u) ; sta[++ v] = t[u].f , u = t[u].f);
for(u = t[u].f, i = v ; i ; -- i) pushdown(sta[i]);
for(int fa ; (fa = t[x].f) ^ u ; rotate(x))
if(t[fa].f ^ u)
rotate(get(fa) == get(x) ? fa : x);
}
inline void Access(int x)
{
int y = 0;
while(x)
{
splay(x), rson = y, pushup(x), y = x, x = t[x].f;
}
}
inline void makeroot(int x)
{
Access(x), splay(x), mark_rev(x);
}
inline void split(int x, int y)
{
makeroot(x), Access(y), splay(y);
}
inline void link(int x, int y)
{
makeroot(x), t[x].f = y;
}
int main()
{
int i, j;
scanf("%d", &n);
for(i = 0; i < N ; ++i) t[i].mul = 1;
for(i = 0; i <= n ; ++i) t[i].min = inf, t[i].max = -inf;
for(i = 1; i < n ; ++i)
{
int u , v , c;
scanf("%d%d%d", &u , &v , &c);
++ u , ++ v;
t[i + n].val = (ll)c, pushup(i + n), link(u, i + n), link(i + n, v);
}
int T , x, y;
char str[9];
scanf("%d", &T);
for(int cas = 1; cas <= T ; ++cas)
{
scanf("%s%d%d", str, &x, &y);
if(str[1] == 'I') split(x + 1, y + 1), printf("%lld\n", t[y + 1].min);
if(str[1] == 'A') split(x + 1, y + 1), printf("%lld\n", t[y + 1].max);
if(str[0] == 'S') split(x + 1, y + 1), printf("%lld\n", t[y + 1].sum);
if(str[0] == 'N') split(x + 1, y + 1), mark_mul(y + 1, -1);
if(str[0] == 'C') makeroot(x + n), t[x + n].val = (ll)y, pushup(x + n);
}
return 0;
}
BZOJ 2157: 旅游 (结构体存变量)的更多相关文章
- [Go] 复合类型(数组、切片、字典、结构体)变量的 初始化 及 注意事项
Go变量 初始化 对 复合类型(数组.切片.字典.结构体)变量的初始化是,有一些语法限制: 1.初始化表达式必须包含类型标签: 2.左花括号必须在类型尾部,不能另起一行: 3.多个成员初始值以逗号分隔 ...
- go语言基础之结构体普通变量初始化
1.结构体 1.1.结构体类型 有时我们需要将不同类型的数据组合成一个有机的整体,如:一个学生有学号/姓名/性别/年龄/地址等属性.显然单独定义以上变量比较繁琐,数据不便于管理. 结构体是一种聚合的数 ...
- C语言根据结构体成员变量的地址,得到结构体的地址
看nginx代码时发现双链表使用的是这种方法,记录一下 给出一个实例来说明 struct father_t { int a; char *b; double c;}f;char *p ...
- BZOJ 2157: 旅游( 树链剖分 )
树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...
- bzoj 2157: 旅游 (LCT 边权)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec Memory Lim ...
- BZOJ 2157: 旅游
2157: 旅游 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1347 Solved: 619[Submit][Status][Discuss] ...
- [GO]结构体指针变量初始化
package main import "fmt" func main() { type student struct { id int name string sex byte ...
- 【刷题】BZOJ 2157 旅游
Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...
- BZOJ 2157 旅游(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...
随机推荐
- 未能加载文件或程序集“microsoft.Build.Engine, Version=3.5.0.0,...”或它的摸一个依赖项。
今天想打开IIS服务,然后点错了,不小心关掉了.net组件,结果vs就一直打不开项目,最后在网上查到了原因,打开 控制面板->程序和功能->打开或关闭功能 在里面勾选Microsoft . ...
- Excel透视表进阶之计算字段、计算项、切片器、页面布局
计算字段 在透视表的字段列表中通过函数.公式等方式构建一个新的字段 又称虚拟字段,因为计算字段不会出现在数据源中,对于普通字段的操作,都可以对计算字段进行操作 计算字段只能出现在值区域,不能出现在筛选 ...
- Maven - Maven3实战学习笔记(1)Maven使用入门
1.maven安装 1>http://maven.apache.org/download.cgi下载apache-maven-3.6.1 2>解压缩安装包到指定的文件夹,如C:\fyliu ...
- 【2019CSP-S游记】咕了好久了撒
对,证书已经发下来了,我才想起来写游记(虽然我个蒟蒻明明就是在写反思) 终于和父母商议好了以后怎么办,顺带找了一下班主任,在机房的电脑敲出来的(我来找教练,然后完全没找着,淦) 79分,众所周知CCF ...
- C语言 --- 函数指针(初级)
1.函数指针:指向函数的指针变量. 函数在内存中也是有地址的,函数名代表函数的内存地址. 例子:函数:int sum(int a,int b); int ...
- win10+vs2013+pcl1.8.0(x86) 环境配置遇到的各种小问题解决
1.PCL提供了各自的PDB调试文件(解压后放入pcl安装目录的bin下) 2.OpenNI的安装需同其余在pcl第三方库文件夹下 3.添加附加依赖项的.lib文件请按照网上对应版本添加,另外需要每行 ...
- gcc数据对齐之: howto 1.
GCC支持用__attribute__为变量.类型.函数.标签指定特殊属性.这些不是编程语言标准里的内容,而属于编译器对语言的扩展. 本文介绍其中的两个属性:aligned和packed. align ...
- 生成url的二维码图片
<?php require_once "./phpqrcode.php"; //生成二维码 $img = \QRcode::png("https://www.bai ...
- Python自学
print("\u4e2d\u56fd\") 报错,语法错误 修改,去掉尾部的\,正确 import datetimeprint("now:"+datetime ...
- C语言中将二维数组作为函数参数来传递
c语言中经常需要通过函数传递二维数组,有三种方法可以实现,如下: 方法一, 形参给出第二维的长度. 例如: #include <stdio.h> void func(int n, char ...