王学长的LCT标程
善良的王学长竟然亲自打了一遍QAQ好感动QAQ
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
struct node {
node *ch[],*fa;
bool rev;
int x,sum,siz,mul,add;
inline void add_rev_tag(){
swap(ch[],ch[]);rev^=;return;
}
inline void add_plus_tag(int a){
sum+=siz*a;x+=a;add+=a;return;
}
inline void add_mul_tag(int m){
sum*=m;x*=m;mul*=m;add*=add*m;return;
}
inline void down(){
if(rev){
if(ch[]) ch[]->add_rev_tag();
if(ch[]) ch[]->add_rev_tag();
rev=;
}
if(add){
if(ch[]) ch[]->add_plus_tag(add);
if(ch[]) ch[]->add_plus_tag(add);
add=;
}
if(mul>){
if(ch[]) ch[]->add_mul_tag(mul);
if(ch[]) ch[]->add_mul_tag(mul);
mul=;
} return;
}
inline void update(){
sum=x;siz=;
if(ch[]) sum+=ch[]->sum,siz+=ch[]->siz;
if(ch[]) sum+=ch[]->sum,siz+=ch[]->siz;
return;
}
}lct[maxn];
inline int get_parent(node *x,node *&fa){return (fa=x->fa)?fa->ch[]==x?:fa->ch[]==x?:-:-;}
inline void rotate(node *x){
int t1,t2;
node *fa,*gfa;
t1=get_parent(x,fa);
t2=get_parent(fa,gfa);
if ((fa->ch[t1]=x->ch[t1^])) fa->ch[t1]->fa=fa;
x->ch[t1^]=fa;fa->fa=x;x->fa=gfa;
if (t2!=-) gfa->ch[t2]=x;
fa->update();return;
}
inline void pushdown(node *x){
static node *stack[maxn];
int cnt=;
while(){
stack[cnt++]=x;
node *fa=x->fa;
if (!fa || (fa->ch[]!=x && fa->ch[]!=x)) break;
x=fa;
}
while(cnt--) stack[cnt]->down();
return;
}
inline node * splay(node *x){
pushdown(x);
while(){
int t1,t2;
node *fa,*gfa;
t1=get_parent(x,fa);
if(t1==-) break;
t2=get_parent(fa,gfa);
if(t2==-){
rotate(x);break;
} else if (t1==t2){
rotate(fa);rotate(x);
} else{
rotate(x);rotate(x);
}
}
x->update();
return x;
}
inline node * access(node *x){
node *ret=NULL;
while (x) splay(x)->ch[]=ret,(ret=x)->update(),x=x->fa;
return ret;
}
inline void makeroot(int x){access(lct+x)->add_rev_tag();}
inline void link(int u,int v){
makeroot(u);splay(lct+u)->fa=lct+v;return;
}
inline void cut(int u,int v){
makeroot(u);
node *p=(access(lct+v),splay(lct+v));
p->ch[]->fa=NULL;
p->ch[]=NULL;
p->update();
}
int n,q;
int main(){
n=read();q=read();
int i;
for(i=;i<=n;i++) {
lct[i].x=lct[i].sum=;
lct[i].siz=;
lct[i].mul=;
}
for(i=;i<n;i++){
int u,v;
u=read();v=read();
link(u,v);
}
while(q--){
char ch=getchar();
while(ch<=) ch=getchar();
int u,v,x,y,c;
if(ch=='+'){
u=read();v=read();c=read();
makeroot(u);
access(lct+v)->add_plus_tag(c);
}else if(ch=='-'){
u=read();v=read();x=read();y=read();
cut(u,v);link(x,y);
}else if(ch=='*'){
u=read();v=read();c=read();
makeroot(u);
access(lct+v)->add_mul_tag(c);
}else if(ch=='/'){
u=read();v=read();
makeroot(u);
printf("%u\n",access(lct+v)->sum);
}
}
return ;
}
搜索
复制
王学长的LCT标程的更多相关文章
- 王学长的AAA树
让我们响应王学长的号召勇敢的分开写splay和lct吧! 分开写大法好!!!!!!!!!!!杜教的ch[4]弱爆了!!!! #include <stdio.h> #include < ...
- [求助][SPOJ MARIOGAM]-高斯消元(内含标程,数据等)
小蒟蒻开始做概率的题之后,遇到了这道题,然而,他发现自己的程序调试了无数次也无法通过,系统总是返回令人伤心的WA, 于是,他决定把这一天半的时间收集到的资料放在网上, 寻求大家的帮助, 也可以节省后来 ...
- hdu6435 Problem J. CSGO标程讲解以及改正标程的一个错误(本来第一个样例过不了2333) 以及 poj2926 五维曼哈顿距离模板
比赛的时候抄poj2926的模板,但改不来啊orz #include <iostream> #include <cstdio> #include <cstring> ...
- [转]关于一些SPFA的标程
SPFA算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. 最短路径快速算法-SPFA算法是西南交通大学段凡丁于1994年发表的. 适用范围:给定 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- BJOI2019Day1 数据&标程&题解
链接: https://pan.baidu.com/s/16L5GHvo9WtY20sZoqjuQNQ 提取码: 3iur
- HDU4631(标程代码)
/*将x从小到大排序,每次插入一个点,直接找比这个点的x大的第一个,然后从这个开始向两边找 ,找点的下标用多重容器实现*/ #include<stdio.h> #include<st ...
- 【CQ18高一暑假前挑战赛5】标程
[A:暴力] #include<bits/stdc++.h> using namespace std; ; int a[maxn],vis[maxn],N,M; int main() { ...
- 【CQ18高一暑假前挑战赛4】标程
[二分或者STL] 二分: #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int main() { ,pos; s ...
随机推荐
- eclipse+axis2+webservice开发实例
myeclipse10安装axis2插件 第一步:下载axis2-1.6的插件压缩包,axis2-eclipse-codegen-plugin-1.6.2.zip 和 axis2-eclipse-se ...
- 通过扩展RandomAccessFile类使之具备Buffer改善I/O性能--转载
主体: 目前最流行的J2SDK版本是1.3系列.使用该版本的开发人员需文件随机存取,就得使用RandomAccessFile类.其I/O性能较之其它常用开发语言的同类性能差距甚远,严重影响程序的运行效 ...
- HTML中常见的各种位置距离以及dom中的坐标讨论
最近在学习JavaScript,特意买了一本犀牛角书来看看,尼玛一千多页,看的我头昏脑涨,翻到DOM这章节,突然记起平常在使用DOM时,碰到了好多的这个dom里面的各种宽度,高度,特意在此写一写,写的 ...
- [转] C++11带来的move语义
PS: 通过引入接收右值的函数形参,可以通过接收右值来实现高效 PS在C++11中,标准库在<utility>中提供了一个有用的函数std::move,这个函数的名字具有迷惑性,因为实际上 ...
- 转载:IntelliJ Idea 常用快捷键列表
IntelliJ Idea 常用快捷键列表 (http://www.open-open.com/lib/view/open1396578860887.html) Ctrl+Shift + Enter, ...
- Apache POI解析excel文件
这里需要用到poi.jar和poi-ooxml.jar 没有的可以去http://mvnrepository.com/下载 import org.apache.poi.POIXMLDocument; ...
- 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>
前台datagrid数据绑定 #region 导出到excel中 /// <summary> /// 2014-6-6 /// </summary> / ...
- 自定义鼠标Cursor转变成图片
今天无意做到项目遇到一个好玩的事情,就是当我鼠标移到一个链接上面,并不是像正常那样出现一个小手,而是变成一个小十字架, 下面看图当时第一眼看到总感觉哪里不对,噢噢噢噢 这样的 小手没了,居然是一个图片 ...
- c#将Excel数据导入到数据库的实现代码(OleDb)
sing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web ...
- Android-adb相关
最近做android开发遇到无法通过usb链接设备的情况,通过wifi连接设备调试也颇为方便 1.android 要root , 下载终端app 比如 BetterTerminal 2.通过以下命令 ...