用结构体存变量好像确实能提高运行速度,以后就这么写数据结构了

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: 旅游 (结构体存变量)的更多相关文章

  1. [Go] 复合类型(数组、切片、字典、结构体)变量的 初始化 及 注意事项

    Go变量 初始化 对 复合类型(数组.切片.字典.结构体)变量的初始化是,有一些语法限制: 1.初始化表达式必须包含类型标签: 2.左花括号必须在类型尾部,不能另起一行: 3.多个成员初始值以逗号分隔 ...

  2. go语言基础之结构体普通变量初始化

    1.结构体 1.1.结构体类型 有时我们需要将不同类型的数据组合成一个有机的整体,如:一个学生有学号/姓名/性别/年龄/地址等属性.显然单独定义以上变量比较繁琐,数据不便于管理. 结构体是一种聚合的数 ...

  3. C语言根据结构体成员变量的地址,得到结构体的地址

    看nginx代码时发现双链表使用的是这种方法,记录一下 给出一个实例来说明 struct father_t {    int a;    char *b;    double c;}f;char *p ...

  4. BZOJ 2157: 旅游( 树链剖分 )

    树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...

  5. bzoj 2157: 旅游 (LCT 边权)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2157 题面; 2157: 旅游 Time Limit: 10 Sec  Memory Lim ...

  6. BZOJ 2157: 旅游

    2157: 旅游 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1347  Solved: 619[Submit][Status][Discuss] ...

  7. [GO]结构体指针变量初始化

    package main import "fmt" func main() { type student struct { id int name string sex byte ...

  8. 【刷题】BZOJ 2157 旅游

    Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...

  9. BZOJ 2157 旅游(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...

随机推荐

  1. 【神经网络与深度学习】用训练好的caffemodel来进行分类

    现在我正在利用imagenet进行finetune训练,待训练好模型,下一步就是利用模型进行分类.故转载一些较有效的相关博客. 博客来源:http://www.cnblogs.com/denny402 ...

  2. form 校验

    import refrom django.forms import Formfrom django.forms import widgetsfrom django.forms import field ...

  3. PTA(Basic Level)1011.A+B和C

    给定区间 [−231,231] 内的 3 个整数 A.B 和 C,请判断 A+B 是否大于 C. 输入格式: 输入第 1 行给出正整数 T (≤10),是测试用例的个数.随后给出 T 组测试用例,每组 ...

  4. 关于float的小奥秘

    一. float 存储方式 1.1. float 占四个字节 1.2. 浮点数构成 1.2.1. 无论是单精度还是双精度在存储中都分为三个部分: <1>. 符号位(Sign) : 0代表正 ...

  5. linux 内核数据结构之 avl树.

    转载: http://blog.csdn.net/programmingring/article/details/37969745 https://zh.wikipedia.org/wiki/AVL% ...

  6. 虚拟机Vmware-网络配置

    非主业,只做简单介绍 虚拟机安装完毕后,需要进行网络配置. 虚拟机有 3 种网络连接方式: 仅主机模式 Host-only:仅支持 虚拟机与宿主机之间进行通信,无法连接外网 桥接模式 bridge:可 ...

  7. 使用python的kazoo模块连接zookeeper实现最基本的增删改查

    kazoo的官方文档地址:https://kazoo.readthedocs.io/en/latest/index.html #!/usr/bin/env python # -*- coding: u ...

  8. ReactNative: Android与iOS平台兼容处理

    方法一: 创建不同的文件扩展名:*.android.js*.io.js 方法二: import { Platform } from 'react-native'; if (Platform.OS == ...

  9. win10 powershell禁止运行脚本解决

    win10 现在默认策略为 Restricted 该策略情况下是禁止在终端下运行脚本文件的,所以我们想要通过powershell 来运行我们的脚本文件的话就需要我们更改其策略才行,如下命令可以帮助你 ...

  10. luogu P2481 [SDOI2010]代码拍卖会

    luogu 题目中的那个大数一定是若干个1+若干个2+若干个3...+若干个9组成的,显然可以转化成9个\(\underbrace {111...1}_{a_i个1}(0\le a_1\le a_2\ ...