[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各种操作,,, ...
随机推荐
- windows 下 putty 登陆服务器 显示matlab图形界面
本文需要下载 putty.exe 和 pscp.exe :http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html Xming 主 ...
- ie8兼容
最近在做ie8兼容,把遇到的问题整理了一下 1. margin:0 auto; 无法居中 解决方法:1.换成h4的文档类型 <!DOCTYPE html PUBLIC "-//W3C/ ...
- fail to create java virtual machine..
今天打开zend stdio 的时候 出现的错误 fail to create java virtual machine... 然后找度娘了,,都说改xxxxx, 我打开360 ,把内存清理了一遍 ...
- opencv 人脸识别
背景知识 OpenCV 是一个开源的计算机视觉和机器学习库.它包含成千上万优化过的算法,为各种计算机视觉应用提供了一个通用工具包.根据这个项目的关于页面,OpenCV 已被广泛运用在各种项目上,从 ...
- 计算bean的和(java)
bean中的属性过多时,要计算一个bean的list之和是一件非常麻烦的事情,可以用java的反射机制解决这件事情,代码如下: package com.jzzhcs.utils; import jav ...
- Android 中 非对称(RSA)加密和对称(AES)加密
在非对称加密中使用的主要算法有:RSA.Elgamal.背包算法.Rabin.D-H.ECC(椭圆曲线加密算法)等. 优点: 非对称加密与对称加密相比,其安全性更好:对称加密的通信双方使用相同的秘钥, ...
- 激!GSS系列
#include <cstdio> ; ; inline int max(int, int); inline int getint(); inline void putint(int); ...
- 井间数据polarization analysis 相关概念
1. 垂直分量上记录到的数据,无法记录SH波?这个有待考证,先记录于此~ 两点需要注意:1.层状介质中,P波和深度方向(Z轴)组成入射面;2.SH的定义为垂直于入射面的S波分量. 2.VSP的观测方式 ...
- 嵌套循环中break、continue的用法
在循环语句中经常会用到break.continue语句,基本用法如下: 一.break语句为退出当前循环,在嵌套循环中结果如下: var num=0; for(var i=0;i<5;i++){ ...
- 总结.NET 中什么时候用 Static
静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数.当类中没有依赖对象标识的数据或行为时,就可以使用静态类.静态类成员是可独立于任何对象标识的数据和行为,即无论对象发生什么更改,这些数据和函 ...