善良的王学长竟然亲自打了一遍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标程的更多相关文章

  1. 王学长的AAA树

    让我们响应王学长的号召勇敢的分开写splay和lct吧! 分开写大法好!!!!!!!!!!!杜教的ch[4]弱爆了!!!! #include <stdio.h> #include < ...

  2. [求助][SPOJ MARIOGAM]-高斯消元(内含标程,数据等)

    小蒟蒻开始做概率的题之后,遇到了这道题,然而,他发现自己的程序调试了无数次也无法通过,系统总是返回令人伤心的WA, 于是,他决定把这一天半的时间收集到的资料放在网上, 寻求大家的帮助, 也可以节省后来 ...

  3. hdu6435 Problem J. CSGO标程讲解以及改正标程的一个错误(本来第一个样例过不了2333) 以及 poj2926 五维曼哈顿距离模板

    比赛的时候抄poj2926的模板,但改不来啊orz #include <iostream> #include <cstdio> #include <cstring> ...

  4. [转]关于一些SPFA的标程

    SPFA算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm. 最短路径快速算法-SPFA算法是西南交通大学段凡丁于1994年发表的. 适用范围:给定 ...

  5. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  6. BJOI2019Day1 数据&标程&题解

    链接: https://pan.baidu.com/s/16L5GHvo9WtY20sZoqjuQNQ 提取码: 3iur

  7. HDU4631(标程代码)

    /*将x从小到大排序,每次插入一个点,直接找比这个点的x大的第一个,然后从这个开始向两边找 ,找点的下标用多重容器实现*/ #include<stdio.h> #include<st ...

  8. 【CQ18高一暑假前挑战赛5】标程

    [A:暴力] #include<bits/stdc++.h> using namespace std; ; int a[maxn],vis[maxn],N,M; int main() { ...

  9. 【CQ18高一暑假前挑战赛4】标程

    [二分或者STL] 二分: #include<bits/stdc++.h> using namespace std; ; int a[maxn]; int main() { ,pos; s ...

随机推荐

  1. Firemonkey的旁门左道[五]

    这次讲讲绘制的几种模式吧,不过还是比较浅显,刚接触不久,还实在没这个实力道出个所以来. FMX下,我们可以切换GDI,D2D,GPU这三种模式, 只要通过全局变量就可以轻松搞定. 如何设置 Globa ...

  2. 从零单排c++ primer(17)

    (1)假设一个名字在派生类的作用域内无法正确解析,则编译器将继续在外层的基类作用域中寻找该名字的定义. (2)派生类的成员将隐藏同名的基类成员. (3)除了覆盖继承而来的虚函数之外,派生类最好不要重用 ...

  3. 开启.htaccess重写之前先来看看mod_rewrite(转)

    Apache的Mode Rewrite模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.在大多数情况下,它和.htaccess文件配合使用.就是说,.htaccess文件的一个主要功能 ...

  4. iOS 如何优雅的处理“回调地狱Callback hell”(一) (上)

    前言 最近看了一些Swift关于封装异步操作过程的文章,比如RxSwift,RAC等等,因为回调地狱我自己也写过,很有感触,于是就翻出了Promise来研究学习一下.现将自己的一些收获分享一下,有错误 ...

  5. Android(java)学习笔记230:服务(service)之绑定服务的细节

    绑定服务的细节 1. 如果onbind方法返回值是null,onServiceConnect方法就不会被调用: 2. 绑定的服务,在系统设置界面,正在运行条目是看不到的: 3. 绑定的服务,不求同时生 ...

  6. java之JDBC

    java之JDBC 一.什么是JDBC Java 数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提 ...

  7. Word 查找和替换的通配符

    查找和替换的通配符 若要查找 类型 示例 任意单个字符 ? s?t 可查找“sat”和“set”. 任意字符串 * s*d 可查找“sad”和“started”. 单词的开头 < <(in ...

  8. 基于bootstrap的轮播广告页,带图片和文字

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  9. 13、SQL Server 自定义函数

    SQL Server 自定义函数 在SQL Server中不仅可以使用系统函数(如:聚合函数,字符串函数,时间日期函数等)还可以根据需要自定义函数. 自定义函数分为标量值函数和表值函数. 其中,标量值 ...

  10. jquery live hover事件的替代写法

    HTML中的hover行为通常在样式中定义,利用jquery实现此效果有两种情况. 第一种是常见的针对页面中静态的元素,以改变元素样式中的border-color为例,写法如下: $(function ...