博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[BZOJ - 2631] tree 【LCT】
阅读量:4583 次
发布时间:2019-06-09

本文共 3632 字,大约阅读时间需要 12 分钟。

题目链接:

 

题目分析

LCT,像线段树区间乘,区间加那样打标记。

这道题我调了一下午。

提交之后TLE了,我一直以为是写错了导致了死循环。

于是一直在排查错误。直到..

直到我看了hzwer的博客,就一句话:“其实这题不需要开long long。。。只要unsigned int,不然可能会T”。

纳尼?!TLE是因为常数问题?于是我将 long long 改成了 unsighed int ,然后...AC。

我................!

 

long long 不能随便用啊...

 

代码

#include 
#include
#include
#include
#include
#include
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;}

  

转载于:https://www.cnblogs.com/JoeFan/p/4448199.html

你可能感兴趣的文章
ajax跨域请求 小栗子 jsonP
查看>>
第三章-数据的图表展示
查看>>
源码安装cmake(或者叫升级cmake)
查看>>
maven的pom.xml样例
查看>>
python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别...
查看>>
使用numpy的routines函数创建
查看>>
欧拉回路的典型应用
查看>>
Sciter TIScript KeyEvent
查看>>
stm32工程模板的创建
查看>>
Oracle行转列和列转行
查看>>
linux software
查看>>
Jquery的一些简单使用记录
查看>>
Python学习之路【第二篇】-pyc简介、Python常用的数据类型及其用法和常用运算符...
查看>>
hdu 2680 Choose the best route
查看>>
老李分享:锁定客户的六大策略:教你如何将切换成本嵌入商业模式 2
查看>>
iframe滚动条的一些方法
查看>>
09_传智播客iOS视频教程_@property
查看>>
MySQL学习记录一
查看>>
多线程理论———— threading
查看>>
PLSA及EM算法
查看>>