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

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. [转帖].NET Core 项目指定SDK版本

    .NET Core 项目指定SDK版本 https://www.cnblogs.com/stulzq/p/9503121.html 一. 版本里的坑 自从 .NET Core 2.1.0版本发布以后, ...

  2. spark 常用设置

    1.spark.hadoop.validateOutputSpecs 若设置为true,saveAsHadoopFile会验证输出目录是否存在.虽然设为false可直接覆盖文件路径

  3. void *与id类型的相互转换

    void *与id类型相互转换 在MRC下,void *与id类型相互转换完全没问题. id obj = [[NSObject alloc] init]; void *p = (void *)p; o ...

  4. 初步学习jquery学习笔记(一)

    什么是jquery? Jquery是javascript的一个函数库包含以下功能: html元素选取 html元素的操作 css操作 html事件的函数 javacript的特效 html的遍历和修改 ...

  5. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  6. MLS(移动最小二乘)

    https://blog.csdn.net/weixin_41484240/article/details/81204113 https://blog.csdn.net/baidu_38127162/ ...

  7. js 计算倒计时

    第一个是我自己写的 // t等于时间戳差 function changeTime(t) { let timeStr = '' let tmp = t/(24* 3600 * 1000) if (tmp ...

  8. web框架-(五)Ajax

    Ajax即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术,AJAX = 异步 JavaScript和X ...

  9. Okhttp拦截器统一异常处理并多次读取response.body().string()

    参考:https://blog.csdn.net/a624806998/article/details/73863606 引言: 写这篇文章,因为在自己编写实现Http日志拦截器的时候,在拦截器中使用 ...

  10. 对请求的request添加一些参数

    - (NSURLRequest *)addHeaderRequestWithUrl:(NSString *)urlStr{    NSMutableURLRequest *mutableRequest ...