[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 ...
随机推荐
- mybatis04 根据用户名称模糊查询用户信息
根据用户名称模糊查询用户信息可能返回多条记录. 1.1.1User.xml 编码 如果用%进行模糊查询,#{}表示一个占位符会被翻译为一个?号(SELECT * FROM USER WHERE id= ...
- View事件分发机制
所谓的事件分发,其实就是对MotionEvent事件的分发过程,即当一个MotionEvent产生后,系统需要把这个事件传递给一个具体的View,而这个传递的过程就是分发过程. 点击事件的分发由3个方 ...
- MKServerBuilder.psm1
MKServerBuilder.psm1 function Test-ElevatedShell { $user = [Security.Principal.WindowsIdentity]::Get ...
- 4G上网卡NIDS拨号之Rmnet驱动
4G上网卡一般为双对外通讯口,一个是串口.一个是USB. 但是基于串口的常用波特率为115200,速度过于底下,所以大多使用USB. 1)一般来说常用ppp拨号方式,ppp拨号方式分为应用层pppd与 ...
- Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致
Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致,如果参数不一致,传过去为null
- Linux中Curl命令couldn't connect to host解决方案 php操作Curl(http,https)无法获取远程数据解决方案
本人在做百度账户第三方登录接口,获取百度token,利用php操作curl post方式发送请求token,出现couldn't connect to host错误.经过调试测试,最后终于成功.回头写 ...
- php递归创建目录
/** * 递归创建目录 * @param [string] $path [创建的目录] * @return [type] [description] */ function mk_Dir($path ...
- PHP 发邮件不换行
Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据 MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件 ...
- MITMF使用import error
安装问题: 1.ubuntu 14.04.安装使用capstone时候,提示出现import error:ERROR: fail to load the dynamic library. 解决方法:将 ...
- LINUX nohup命令输入输出深浅进出
无论是否将 nohup命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中.如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中 ...