Hihocoder 1333 (splay)
Problem 平衡树 splay2
题目大意
维护一个序列,支持四种操作:
操作1:添加一个数,编号为x,权值为y。
操作2:删除编号在区间【x,y】内的数。
操作3:将编号在区间【x,y】内的数的权值增加为z。
操作4:询问编号在区间【x,y]内的数的权值和。
解题分析
由于增加了区间加和区间查询,所以要给每个增加一个lazy标记。
在每次搜索树的时候要对每个经过的节点进行一次pushdown,当树的形态或树的结点信息改变时要进行一次pushup。
参考程序
#include <bits/stdc++.h>
using namespace std; int tag=; struct node{
int id,num;
long long lazy,val,sum;
node *left,*right,*father;
node(int id_=,long long val_=,int num_=,long long lazy_=,long long sum_=,node* father_=NULL,node* left_=NULL,node* right_=NULL)
{
id=id_; val=val_; num=num_; lazy=lazy_; sum=sum_;
father=father_; left=left_; right=right_;
}
}*rt,*t1,*t2; void search(node *now)
{
cout<<now->id; cout<<" val= "<<now->val<<" sum= "<<now->sum<<" num= "<<now->num<<" lazy= "<<now->lazy;
if (now->left) cout<<" lson: "<<now->left->id;
if (now->right) cout<<" rson: "<<now->right->id;
cout<<endl;
if (now->left) search(now->left);
if (now->right) search(now->right);
} void pushup(node* x)
{
x->sum=x->val; x->num=;
if (x->left) x->sum+=x->left->sum,x->num+=x->left->num;
if (x->right) x->sum+=x->right->sum,x->num+=x->right->num;
} void pushdown(node* x)
{
if (x->lazy)
{
node *l = x->left,*r = x->right;
if (l)
{
l->lazy += x->lazy;
l->val += x->lazy;
l->sum += x->lazy * l->num;
}
if (r)
{
r->lazy += x->lazy;
r->val += x->lazy;
r->sum += x->lazy * r->num;
}
x->lazy = ;
}
} void right(node* x,node* &rt)
{
node *y=x->father,*z=y->father;
if (y==rt) rt=x;
else if (z->left==y) z->left=x; else z->right=x; //需要判断是左右孩子
x->father=z; y->father=x; if (x->right) x->right->father=y; //防止对空指针进行操作
y->left=x->right; x->right=y;
pushup(y); pushup(x);
} void left(node* x,node* &rt)
{
node *y=x->father,*z=y->father;
if (y==rt) rt=x;
else if (z->left==y) z->left=x; else z->right=x;
x->father=z; y->father=x; if (x->left) x->left->father=y;
y->right=x->left; x->left=y;
pushup(y); pushup(x);
} void splay(node* x,node* &rt)
{
while (x!=rt)
{
node *y=x->father, *z=y->father;
if (y==rt)
{
if (x==y->left) right(x,rt);
else left(x,rt);
}
else
{
if (y==z->left)
if (x==y->left) { right(y,rt); right(x,rt); }
else { left(x,rt); right(x,rt); }
else
if (x==y->right) { left(y,rt); left(x,rt); }
else { right(x,rt); left(x,rt); }
}
}
} void insert(int id,int val,node* &now,node *last)
{
if (now==NULL)
{
now=new node(id,val,,,val,last);
splay(now,rt);
return;
}
pushdown(now);
if (id < now->id) insert(id,val,now->left,now); else insert(id,val,now->right,now);
//else还是要加的 返回的时候树的形态已经改变了
} void find_1(int id,node *x)
{
if (x==NULL) return;
pushdown(x);
if (x->id>=id) find_1(id,x->left);
else {t1=x; find_1(id,x->right);}
} void find_2(int id,node *x)
{
if (x==NULL) return;
pushdown(x);
if (x->id<=id) find_2(id,x->right);
else {t2=x; find_2(id,x->left);}
} void del(int l,int r)
{
t1=t2=NULL;
find_1(l,rt); splay(t1,rt);
find_2(r,rt->right); splay(t2,rt->right);
rt->right->left=NULL;
pushup(rt->right); pushup(rt);
} void add(int l,int r,int val)
{
t1=t2=NULL;
find_1(l,rt); splay(t1,rt);
find_2(r,rt->right); splay(t2,rt->right);
if (rt->right->left)
{
rt->right->left->lazy += val;
rt->right->left->sum += 1ll* val * rt->right->left->num;
rt->right->left->val += val;
}
pushup(rt->right); pushup(rt);
} long long query(int l,int r)
{
t1=t2=NULL;
find_1(l,rt); splay(t1,rt);
find_2(r,rt->right); splay(t2,rt->right);
if (rt->right->left)
return rt->right->left->sum;
else return ;
} int main()
{
int n;
rt=NULL;
scanf("%d",&n);
insert(<<,,rt,NULL); insert(-<<,,rt,NULL);
for (int i=;i<=n;i++)
{
char s[]; int x,y,z;
scanf("%s%d%d",s,&x,&y);
if (s[]=='I') insert(x,y,rt,NULL);
if (s[]=='D') del(x,y);
if (s[]=='M')
{
scanf("%d",&z);
add(x,y,z);
}
if (s[]=='Q') cout<<query(x,y)<<endl;
}
}
Hihocoder 1333 (splay)的更多相关文章
- Hihocoder 1325 (splay)
Problem 平衡树 Treap 题目大意 维护一个数列,支持两种操作. 操作1:添加一个数x. 操作2:询问不超过x的最大的数. 解题分析 尝试了一下用指针来写splay,感觉写起来还是比较流畅的 ...
- Hihocoder 1329(splay)
Problem 平衡树 Splay 题目大意 维护一个数列,支持三种操作. 操作1:添加一个数x. 操作2:询问不超过x的最大的数. 操作三:删除大小在区间[a,b]内的数. 解题分析 和上一题相比, ...
- 【BZOJ3506】排序机械臂(Splay)
[BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...
- 【BZOJ1500】【NOI2005】维修数列(Splay)
[BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...
- 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)
[BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题
- 【BZOJ1056】[HAOI2008]排名系统(Splay)
[BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...
- 【BZOJ2329】括号修复(Splay)
[BZOJ2329]括号修复(Splay) 题面 BZOJ 洛谷 题解 本来想着用线段树来写 但是有一个区间翻转 所以不能用线段树了,就只能用平衡树 然后直接\(Splay\)就好了 注意一下几个标记 ...
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
随机推荐
- SpringBoot2.x版本整合SpringSecurity、Oauth2进行password认证
很多人在进行项目开发时都会用到Oauth2.0结合SpringSecurity或者Shiro进行权限拦截以及用户验证,网上也有很多的案例,前几天项目里边需要用到,顺便整合了进来,特此写篇博客,记录下过 ...
- Linux学习系列八:操作网口
一些相对高性能的单片机会带以太网接口,网口在MCU里算是比较复杂的外设了,因为它涉及到网络协议栈,通常情况下网络协议栈会运行在一个RTOS中,所以对普通单片机开发者来说网口使用起来相对难度较大一些.在 ...
- thinkphp 5 常用的助手函数
load_trait:快速导入Traits,PHP5.5以上无需调用 /** * 快速导入Traits PHP5.5以上无需调用 * @param string $class t ...
- keystore找回密码
昨天准备给自己的应用发布一个新版本,在apk打包时,发现之前的用的keystore密码忘了. 蛋碎了一地,我把我所能想到的密码都试了一遍(注:我平常在各个门户网站注册基本上用的都是那几个字母和数字组合 ...
- 上传txt文件编码格式判断(文本乱码解决方法)
说明 通过ajax或者浏览上传文本文件,上传时候c#处理时候因为文本格式的创建不同,在获取内容时候会出现中文乱码. 解决方法 通过上传的文件流,判断文件的编码格式,在使用对应的编码格式获取文本内容 # ...
- Android 解决RecyclerView瀑布流效果结合Glide使用时图片变形的问题
问题描述:使用Glide加载RecyclerView的Item中的图片,RecyclerView使用了瀑布流展示图片,但是滚动时图片会不断的加载,并且大小位置都会改变,造成显示错乱. 解决方法:使用瀑 ...
- Visual Studio TFS
Overview:Active Directory环境下搭建TFS(一个domain内,with Domain Controller): 1)最简单的环境(这俩拓扑是从TFSAdmin文档中截取的,从 ...
- Bootstrap Datatable 简单的基本配置
$(document).ready(function() { $('#example').dataTable({ "sScrollX": "100%", ...
- JVM 参数含义
JVM参数的含义 实例见实例分析 参数名称 含义 默认值 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,J ...
- spring思想分析
摘要: EveryBody in the world should learn how to program a computer...because it teaches you how to th ...