[POJ3468] A Simple Problem with Integers (Treap)
题目链接:http://poj.org/problem?id=3468
这题是线段树的题,拿来学习treap。
不旋转的treap。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <cmath>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstdlib>
#include <functional>
#include <queue>
#include <stack>
#include <list>
#include <ctime>
using namespace std; const int inf = ~0U >> ; typedef long long LL; struct TreapNode{
int id,sz,key;
LL val,sum,delta;
TreapNode* ch[];
TreapNode(): key(rand()),sz(),id(),val(),sum(),delta() {ch[]=ch[]=NULL;}
void rz(){
sz = ;
sum = val;
if(ch[]) { sz+=ch[]->sz; sum+=ch[]->sum; }
if(ch[]) { sz+=ch[]->sz; sum+=ch[]->sum; }
}
void pd(){
if(delta){
if(ch[]) {
ch[]->delta+=delta;
ch[]->val+=delta;
ch[]->sum+=delta*ch[]->sz;
}
if(ch[]) {
ch[]->delta+=delta;
ch[]->val+=delta;
ch[]->sum+=delta*ch[]->sz;
}
delta = ;
}
}
~TreapNode(){
if(ch[]) delete ch[];
if(ch[]) delete ch[];
}
}; typedef pair<TreapNode*,TreapNode*> DTreap; TreapNode* Merge(TreapNode* A,TreapNode* B) {
if(!A) return B;
if(!B) return A;
A->pd();B->pd();
if(A->key<B->key) {
A->ch[] = Merge(A->ch[],B);
A->rz();
return A;
} else {
B->ch[] = Merge(A,B->ch[]);
B->rz();
return B;
}
} DTreap Split(TreapNode* rt,int id) {
if(!rt) return DTreap(NULL,NULL);
DTreap ret;
rt->pd();
if(rt->id>id) {
ret = Split(rt->ch[],id);
rt->ch[] = ret.second;
ret.second = rt;
rt->rz();
return ret;
} else {
ret = Split(rt->ch[],id);
rt->ch[] = ret.first;
ret.first = rt;
rt->rz();
return ret;
}
} void Insert(TreapNode*& rt,int id,LL val) {
DTreap dt = Split(rt,id-);
TreapNode* lch = dt.first;
TreapNode* rch = dt.second;
TreapNode* nd = new TreapNode;
nd->id = id;
nd->val = val;
nd->sum = val;
lch = Merge(lch,nd);
rt = Merge(lch,rch);
} void AddInterval(TreapNode*& rt,int l,int r,LL val) {
if(!rt) return;
DTreap dt = Split(rt,l-);
TreapNode* lch = dt.first;
dt = Split(dt.second,r);
TreapNode* mid = dt.first;
TreapNode* rch = dt.second;
if(mid){
mid->val += val;
mid->sum += val*mid->sz;
mid->delta += val;
}
lch = Merge(lch,mid);
rt = Merge(lch,rch);
} LL QueryInterval(TreapNode*& rt,int l,int r) {
if(!rt) return ;
LL ret = ;
DTreap dt = Split(rt,l-);
TreapNode* lch = dt.first;
dt = Split(dt.second,r);
TreapNode* mid = dt.first;
TreapNode* rch = dt.second;
if(mid) ret = mid->sum;
lch = Merge(lch,mid);
rt = Merge(lch,rch);
return ret;
} int n,m;
LL val;
char cmd[]; int main() {
srand(time(NULL));
while(~scanf("%d%d",&n,&m)) {
TreapNode* root = NULL;
for(int i = ; i < n ; i++ ) {
scanf("%lld",&val);
Insert(root,i+,val);
}
for(int i = ; i < m ; i++ ) {
scanf("%s",cmd);
if(cmd[] == 'Q' ) {
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",QueryInterval(root,l,r));
} else {
int l,r;
LL val;
scanf("%d%d%lld",&l,&r,&val);
AddInterval(root,l,r,val);
}
}
if(root) delete root;
}
return ;
}
[POJ3468] A Simple Problem with Integers (Treap)的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- POJ-3468 A Simple Problem with Integers Splay Tree区间练习
题目链接:http://poj.org/problem?id=3468 以前用线段树做过,现在用Splay Tree A了,向HH.kuangbin.cxlove大牛学习了各种Splay各种操作,,, ...
随机推荐
- Activity的Launch Mode
ANDROID四种启动模式: 1.standard:默认的启动模式,每次新建一个实例对象. 2.singleTop:如果在任务栈顶发现了相同的实例则复用该实例,否则新建一个实例并压入栈顶. 3.sin ...
- 20160405互联网新闻<来自涛涛大产品>
1.滴滴或将收购腾讯地图,打造“滴滴地图”(滴滴与神州.uber之间的争斗,归根到底还是BAT的代理人之战)2.优信二手车否认合并传言 并谴责58同城仿冒优信网站(商战无所不用其极)3.京东旗下的拍拍 ...
- to_date & to_char
在oracle数据库中,有这么两个数据格式转换函数, to_date & to_char 这两个函数都可以将日期格式更改,但是一般都用前者,百度搜索中有个例子: select * from T ...
- pyqt4:连接的一个带有参数的方法
一般在pyqt4中的信号连接很少连接带参数的方法,很多时候连接带参数的方法节约不少代码量. self.s5_thread=scene.Worker5() self.log_get=QtCore.QTi ...
- 在as3中使用protobuf
在最近参与开发的adobe air项目中,前后端的通信协议从XML.JSON再到protobuf,最后选择protobuf原因,主要是前后端维护protobuf协议就行了,同时还可以利用IDE做一些编 ...
- pwnable echo2
pwnable echo2 linux 32位程序 涉及FSB和UAF漏洞的利用,先%x泄露地址,然后利用UAF漏洞进行利用 code:
- LINQ 联表查询 取count 值
linq to sql 实现左外部连接:var query=from a in A join b in B on a.ID equals b.aID into ab from a1 in ab.Def ...
- TD Rigging Demo Reel 性感美女绑定展示
161455520158189 这是一个充满回忆的Demo,非常怀念之前的工作生活,也特别感谢我长春的老哥张总对我的帮助与指导,不光是工作中,在生活上也让我有很大的收获.这个一直都觉得做的不够好,也从 ...
- C#模拟键盘事件
public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("USER3 ...
- 040. asp.netWeb中TreeView控件绑定XML文件
xml文件格式: <?xml version="1.0" encoding="utf-8" ?> <sitemap title="进 ...