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

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. Docker&Java&Mysql&Python3&Supervisor&Elasticsearch安装

    目录 docker 安装java 安装mysql 安装Mysql8 安装python3 安装supervisor 安装ElasticSearch 打包images docker yum install ...

  2. 写出java.lang.Object类的六个常用方法

    java是面向对象的语言,而Object类是java中所有类的顶级父类(根类). 每个类都使用Object类作为超类,所有对象(包括数组)都实现这个类的方法,即使一个类没有用extends明确指出继承 ...

  3. SpringBoot启动时 提示没有主清单属性 MANIFEST

    SpringBoot启动时 提示没有主清单属性 MANIFEST <?xml version="1.0" encoding="UTF-8"?> &l ...

  4. spring boot-14.集成MyBatis

    1.如何使用注解版Mybatis? (1)引入mybatis ,druid,Mysql 的依赖,环境搭建可以参考第13篇的内容 <dependency> <groupId>or ...

  5. Android Studio 如何获取 text文本内容

    1.找到目录的main先建立assets格式的文件夹 2.再把需要读取的txt 文件放入到该文件夹下(名字随意),这里取 list.txt. 文件内容 格式如下 3.读取文本内容 工具代码 /** * ...

  6. c#HtmlAgilityPack解析html

    通过HtmlAgilityPack实现对html页面解析HtmlDocument doc = new HtmlDocument(); doc.Load(yourStream); var itemLis ...

  7. LNMP完整安装教程

    软件下载地址   https://lnmp.org/install.html 本环境与外网生产环境一致(MySQL 5.6 + PHP 7.1 + CentOS + Nginx 1.12 ) 上图红色 ...

  8. vue项目1-pizza点餐系统2-配置路由跳转

    功能目标:点击导航栏中的菜单.主页.路由跳转到不同的组件,点击谁就在在导航栏下展示谁. 1.在router文件夹中(在用脚手架cli搭建项目时,有个couter的选yes)的index.js中,导入如 ...

  9. GDAL联合OpenCV进行图像处理

    作为一名图像处理方面的工程师,在面对大数据量的遥感影像时,往往会利用到强大的GDAL库,但是GDAL库却没有方面的算法函数进一步进行处理:同时我们看到Opencv库能提供强大的算法支持,却对大数据影像 ...

  10. AIX查看系统日志

    1.查看系统启动日志 在AIX中可以使用alog命令来查看系统日志.   启动日志: /var/adm/ras/bootlog /var/adm/ras/bosinstlog /var/adm/ras ...