[BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631
题目分析
LCT,像线段树区间乘,区间加那样打标记。
这道题我调了一下午。
提交之后TLE了,我一直以为是写错了导致了死循环。
于是一直在排查错误。直到..
直到我看了hzwer的博客,就一句话:“其实这题不需要开long long。。。只要unsigned int,不然可能会T”。
纳尼?!TLE是因为常数问题?于是我将 long long 改成了 unsighed int ,然后...AC。
我................!
long long 不能随便用啊...
代码
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; inline void Read(int &Num)
{
char c = getchar();
bool Neg = false;
while (c < '0' || c > '9')
{
if (c == '-') Neg = true;
c = getchar();
}
Num = c - '0'; c = getchar();
while (c >= '0' && c <= '9')
{
Num = Num * 10 + c - '0';
c = getchar();
}
if (Neg) Num = -Num;
} const int MaxN = 100000 + 5, Mod = 51061; typedef unsigned int LL; int n, q;
int Father[MaxN], Son[MaxN][2], Size[MaxN]; LL T[MaxN], Mul[MaxN], Plus[MaxN], A[MaxN]; bool Rev[MaxN], isRoot[MaxN]; inline void Update(int x)
{
T[x] = (T[Son[x][0]] + T[Son[x][1]] + A[x]) % Mod;
Size[x] = Size[Son[x][0]] + Size[Son[x][1]] + 1;
} inline void Reverse(int x)
{
Rev[x] = !Rev[x];
swap(Son[x][0], Son[x][1]);
} inline void Paint_Mul(int x, LL Num)
{
Mul[x] = Mul[x] * Num % Mod;
Plus[x] = Plus[x] * Num % Mod;
T[x] = T[x] * Num % Mod;
A[x] = A[x] * Num % Mod;
} inline void Paint_Plus(int x, LL Num)
{
Plus[x] = (Plus[x] + Num) % Mod;
T[x] = (T[x] + Num * Size[x] % Mod) % Mod;
A[x] = (A[x] + Num) % Mod;
} inline void PushDown(int x)
{
if (Rev[x])
{
Rev[x] = false;
if (Son[x][0]) Reverse(Son[x][0]);
if (Son[x][1]) Reverse(Son[x][1]);
}
if (Mul[x] != 1)
{
if (Son[x][0]) Paint_Mul(Son[x][0], Mul[x]);
if (Son[x][1]) Paint_Mul(Son[x][1], Mul[x]);
Mul[x] = 1;
}
if (Plus[x] != 0)
{
if (Son[x][0]) Paint_Plus(Son[x][0], Plus[x]);
if (Son[x][1]) Paint_Plus(Son[x][1], Plus[x]);
Plus[x] = 0;
}
} void Rotate(int x)
{
int y = Father[x], f;
PushDown(y); PushDown(x);
if (x == Son[y][0]) f = 1;
else f = 0;
if (isRoot[y])
{
isRoot[y] = false;
isRoot[x] = true;
}
else
{
if (y == Son[Father[y]][0]) Son[Father[y]][0] = x;
else Son[Father[y]][1] = x;
}
Father[x] = Father[y];
Son[y][f ^ 1] = Son[x][f];
if (Son[x][f]) Father[Son[x][f]] = y;
Son[x][f] = y;
Father[y] = x;
Update(y);
Update(x);
} void Splay(int x)
{
int y;
while (!isRoot[x])
{
y = Father[x];
if (isRoot[y])
{
Rotate(x);
break;
}
if (y == Son[Father[y]][0])
{
if (x == Son[y][0])
{
Rotate(y);
Rotate(x);
}
else
{
Rotate(x);
Rotate(x);
}
}
else
{
if (x == Son[y][1])
{
Rotate(y);
Rotate(x);
}
else
{
Rotate(x);
Rotate(x);
}
}
}
} int Access(int x)
{
int y = 0;
while (x != 0)
{
Splay(x);
PushDown(x);
if (Son[x][1]) isRoot[Son[x][1]] = true;
Son[x][1] = y;
if (y) isRoot[y] = false;
Update(x);
y = x;
x = Father[x];
}
return y;
} void Make_Root(int x)
{
int t = Access(x);
Reverse(t);
} int main()
{
scanf("%d%d", &n, &q);
int a, b;
for (int i = 1; i <= n; ++i)
{
A[i] = T[i] = 1;
Mul[i] = 1; Plus[i] = 0;
isRoot[i] = true;
Size[i] = 1;
}
for (int i = 1; i <= n - 1; ++i)
{
Read(a); Read(b);
Make_Root(a);
Splay(a);
Father[a] = b;
}
char Str[5];
int x, y, Num, t;
for (int i = 1; i <= q; ++i)
{
scanf("%s", Str);
switch (Str[0])
{
case '+' :
Read(x); Read(y); Read(Num);
Make_Root(x);
t = Access(y);
Paint_Plus(t, (LL)Num);
break; case '-' :
Read(x); Read(y);
Make_Root(x);
Access(y);
Splay(y);
PushDown(y);
isRoot[Son[y][0]] = true;
Father[Son[y][0]] = 0;
Son[y][0] = 0;
Update(y);
Read(x); Read(y);
Make_Root(x);
Splay(x);
Father[x] = y;
break; case '*' :
Read(x); Read(y); Read(Num);
Make_Root(x);
t = Access(y);
Paint_Mul(t, (LL)Num);
break; case '/' :
Read(x); Read(y);
Make_Root(x);
t = Access(y);
printf("%d\n", (int)(T[t] % Mod));
break;
}
}
return 0;
}
[BZOJ - 2631] tree 【LCT】的更多相关文章
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- BZOJ2631 tree 【LCT】
题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...
- 3282. Tree【LCT】
Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
- bzoj 3159: 决战【LCT】
只是想复健一下LCT没想到做了不得了的题--调了两天QAQ 题解是这么说的: 但是果然还不太理解--因为swap的前后问题调了好久,(所以一开始养成的习惯后面就不要再改啦-- 总之大概就是把对位置lc ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- BZOJ 2631: tree( LCT )
LCT...略麻烦... -------------------------------------------------------------------------------- #inclu ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
- BZOJ 2631: tree [LCT splay区间]
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 3854 Solved: 1292[Submit][Status][Discus ...
随机推荐
- Android开发_字符串处理类-TextUtils类
对于字符串处理Android为我们提供了一个简单实用的TextUtils类,如果处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类,主要的功能如下: ...
- linux 进程综合指令
1. 查询当前机器运行的进程总数: ps -ef | wc -l ps -ef | grep httpd | wc -l 2. ulimit命令 表 1. ulimit 参数说明 选项 [option ...
- iOS-SQLite数据库使用介绍
iOS-SQLite数据库使用介绍 SQLite是MySQL的简化版,更多的运用与移动设备或小型设备上.SQLite的优点是具有可移植性,它不需要服务器就能运行,同时,它也存在一些缺陷,首先,没有提供 ...
- hdu2015java
偶数求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...
- android使用属性动画代替补间动画
本文参考Android属性动画完全解析(上),初识属性动画的基本用法 android3.0之前一共有两种动画,分别是frame动画和tween动画,关于这两种动画如果不了解可以查看我之前的文章andr ...
- Linux下搭建Oracle11g RAC(9)----创建RAC数据库
接下来,使用DBCA来创建RAC数据库. ① 以oracle用户登录图形界面,执行dbca,进入DBCA的图形界面,选择第1项,创建RAC数据库: ② 选择创建数据库选项,Next: ③ 选择创建通用 ...
- CentOS7上GitLab的使用
生成SSH Keys 生成root账号的ssh key # ssh-keygen -t rsa -C "admin@example.com" 显示pub key的值 # cat ~ ...
- svn出错问题(用户名密码有修改以及资源url改变时)
用eclipse 同步SVN服务器宛然无法访问了: org.tigris.subversion.javahl.ClientException: RA layer request failed svn: ...
- js - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data jquery-1.9.1.min.js:3:4315
FF中时不时报这个错, 就近段做项目来看, 一般是我通过 jquery获取form中的参数(或直接获取参数,并通过ajax进行异步请求的时候,如果有错,就抱该错误! 而对应的, 如果在 Google ...
- Android - 服务器json数据交互.
一,服务器端 服务器端使用的是Servlet,封装json对象使用的 'json-lib-2.2.2-jdk15.jar,ezmorph-1.0.4.jar,commons-logging-1.1.j ...