[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各种操作,,, ...
随机推荐
- 解析C语言结构体对齐(内存对齐问题)
C语言结构体对齐也是老生常谈的话题了.基本上是面试题的必考题.内容虽然很基础,但一不小心就会弄错.写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的 ...
- python 获取html源代码里标签之间的文本用get_text()
例: 输出<span class="w-txt">分享</span>中的文本"分享" contents = bsObj.find_all ...
- apply 伴生对象 单例对象
apply(): 当类或者对象有一个主要用途时,apply方法提供了很好语法机制 scala> class Foo {} defined class Foo scala> object F ...
- Qt MainWindow结构
(图自:FinderCheng 的 Qt 学习之路(11): MainWindow)
- java的三大框架(一)
现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2,Spring,Hibernate.似乎这些框架成为了一个人是否精通Java,是否会写J2EE程序的唯一事实标准和 ...
- Bash Shell内建命令和保留字
Bash Shell内建命令和保留字命令含义!保留字,逻辑非:不做任何事,只做参数展开.读取文件并在shell中执行它alias设置命令或命令行别名bg将作业置于后台运行bind将关键字序列与read ...
- OpenCV MAT基本图像容器
参考博客: OpenCv中cv::Mat和IplImage,CvMat之间的转换 Mat - 基本图像容器 Mat类型较CvMat和IplImage有更强的矩阵运算能力,支持常见的矩阵运算(参照Mat ...
- 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 ...
- spring加载bean实例化顺序
问题来源: 有一个bean为 A,一个bean为B.想要A在容器实例化的时候的一个属性name赋值为B的一个方法funB的返回值. 如果只是在A里单纯的写着: private B b;private ...
- 读取其他软件listview控件的内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...