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> ...
随机推荐
- Nginx(一) 安装基于centos7
1. nginx介绍 1.1. 什么是nginx Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开 ...
- Winform执行CMD命令
1.首先分享CmdHelper类: using System; using System.Collections.Generic; using System.Text; using System.Di ...
- 洛谷 P1582 倒水
题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...
- 258 Add Digits 各位相加
给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字.例如:设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返回它.进阶:你可 ...
- SpringMVC高级课程
requestBody和responseBody requestBody把前台页面传递JSON格式数据强制转换JavaBean responseBody在后台把javabean转换成JSON格式的数据 ...
- 转载pcb设计详细版
http://www.51hei.com/bbs/dpj-52438-1.html 详细的altium designer制作PCB步骤,按照步骤一步步的学习就会自己制作PCB模型 目 录 实验三 层 ...
- Slow HTTP Denial of Service Attack 漏洞解决办法
编辑 删除 问题名称: Slow HTTP Denial of Service Attack 问题URL http://10.238.*.*:58*** 风险等级: 高 问题类型: 服务器配置类 漏洞 ...
- [Windows Server 2008] Windows防火墙设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:如何开启W ...
- Ajax异步刷新省市级联
省市级联在web前端用户注册使用非常广泛.Ajax异步刷新省市级联.如图:选择不同的区,自动加载相应的街. <TD class=field>位 置:</TD> <TD&g ...
- LockDemo 锁对象
class Resource { private boolean flag = false; private String name; private int count; //资源锁 Lock lo ...