【POJ2761】【fhq treap】A Simple Problem with Integers
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
Source
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#define LOCAL
const int MAXN = + ;
const int INF = 0x3f3f3f3f;
const int SIZE = ;
const int maxnode = ;
using namespace std;
typedef long long ll;
ll n, m;
struct fhqTreap{
struct Node{
Node *l, *r;
ll delta, sum;
ll size, fix, val;
}*root, *null, mem[];
ll tot; ll BIG_RAND(){return (ll)rand()*RAND_MAX + (ll)rand();}
void init(){
tot = ;
NEW(null, );
null->size = ;
root = null;
insert(, n);//插入
}
void update(Node *t){
if (t == null) return;
t->size = t->l->size + t->r->size + ;
t->sum = t->val;
if (t->l != null) t->sum += t->l->sum;
if (t->r != null) t->sum += t->r->sum;
}
//标记下传=-=
void push_down(Node *t){
if (t->delta){
t->val += t->delta; if (t->l != null) {t->l->delta += t->delta, t->l->sum += t->l->size * t->delta;}
if (t->r != null) {t->r->delta += t->delta, t->r->sum += t->r->size * t->delta;} t->delta = ;
}
} void NEW(Node *&t, ll val){
t = &mem[tot++];
t->size = ;
t->fix = BIG_RAND();
t->sum = t->val = val;
t->delta = ;
t->l = t->r = null;
}
//将t分裂为大小p和t->size - p的两个子树
void split(Node *t, Node *&a, Node *&b, int p){
if (t->size <= p) a = t, b = null;
else if (p == ) a = null, b = t;
else {
push_down(t);
if (t->l->size >= p){
b = t;
split(t->l, a , b->l, p);
update(b);
}else{
a = t;
split(t->r, a->r, b, p - t->l->size - );
update(a);
}
}
}
//将a,b树合并为t
void merge(Node *&t, Node *a, Node *b){
if (a == null) t = b;
else if (b == null) t = a;
else {
if (a->fix > b->fix){//按fix值排序
push_down(a);
t = a;
merge(t->r, a->r, b);
}else{
push_down(b);
t = b;
merge(t->l, a, b->l);
}
update(t);
}
}
void add(ll l, ll r, ll val){
Node *a, *b, *c;
split(root, a, b, l - );
split(b, b, c, r - l + );
b->delta += val;
b->sum += val * b->size;
merge(a, a, b);
merge(root, a, c);
}
ll query(ll l, ll r){
Node *a, *b, *c;
split(root, a, b, l - );
split(b, b, c, r - l + );
ll ans = b->sum;
merge(b, b, c);
merge(root, a, b);
return ans;
}
//把b挤出去
void Delete(ll p){
Node *a, *b, *c;
split(root, a, b, p - );
split(b, b, c, );
merge(root, a, c);
}
Node *Insert(ll x){//不可思议的插入方式
if (x == ) return null;
if (x == ){
ll tmp;
scanf("%lld", &tmp);
Node *a;
NEW(a, tmp);
return a;
}
Node *a, *b;
int mid = x / ;
a = Insert(mid);
b = Insert(x - mid);
merge(a, a, b);
return a;
} //在pos处插入x个数字
void insert(ll pos, ll x){
Node *a, *b, *c, *t;
//跟块状链表的有点像,分裂再合并
split(root, a, b, pos);
c = Insert(x);//插入一个值为x的树
merge(a, a, c);
merge(root, a, b);
}
}A; void work(){
char str[];
while (m--){
scanf("%s", str);
if (str[] == 'Q'){
ll l, r;
scanf("%lld%lld", &l, &r);
printf("%lld\n", A.query(l, r));
}else{
ll l, r, k;
scanf("%lld%lld%lld", &l, &r, &k);
A.add(l, r, k);
}
}
} int main(){ while( scanf("%lld%lld", &n, &m) != EOF){
A.init();
work();
}
return ;
}
【POJ2761】【fhq treap】A Simple Problem with Integers的更多相关文章
- 一本通1548【例 2】A Simple Problem with Integers
1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...
- 【fhq Treap】bzoj1500(听说此题多码上几遍就能不惧任何平衡树题)
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 15112 Solved: 4996[Submit][Statu ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 【POJ3468】【树状数组区间修改】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers
http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...
- 【POJ 3468】 A Simple Problem with Integers
[题目链接] 点击打开链接 [算法] 本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍 我们不妨引入差分数组c, 则sum(n) = c[1] + (c[1] + c[2] ...
- 【poj3468】A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97008 Accepted: 30285 Case Time Limi ...
- 【poj3468】 A Simple Problem with Integers
http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...
随机推荐
- SolrCloud应用简介
1.windows下启动实例工程 官网下载solr后解压到本地硬盘,在sol/bin目录下打开命令行窗口,然后测试以下命令: solr -e cloud 启动例子中的solrcould,进入交互界面, ...
- DIP开放计算平台介绍
随着平台业务的发展,依赖于Portal(Web)构建的服务架构已逐渐不能满足现有的一些复杂需求(如:使用Hive SQL无法完成计算逻辑),而且对于一些具备编程能力的程序员或数据分析师而言,能够自主控 ...
- 命令行利器Tmux
Tmux是一个优秀的终端复用软件,类似GNU Screen,但是对两个软件评价已经是一边倒了,大多数人认为tmux功能更加强大,使用更加方便. Tmux不仅可以提高终端工作效率,是服务器管理工作必不可 ...
- ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction差别
使用方法:@Html.Action(action, controller)加载局部页面.例如在模板页中使用:@Html.Action("Contact", "Compan ...
- 字符串、十六进制、byte数组互转
import java.io.ByteArrayOutputStream; public class HexUtil { /** * @param args */ public static void ...
- dvwa+xampp搭建显示乱码的问题:解决办法
如图,dvwa显示乱码,解决办法有两个:
- CSS3 新增属性
1Css3概述 从2010年开始,HTML5与CSS3就一直是互联网技术中最受关注的两个话题. 从前端技术的角度可以把互联网的发展分为三个阶段:第一阶段是web1.0以内容为主的网络 前端主流技术是H ...
- JS:九宫格抽奖转盘实例
工作需要,所以做了个抽奖转盘的插件,当然这里只做最简单的演示.可以用于取代一些flash抽奖程序. 机制说明: 1.通过定义lottery-unit来控制节点的个数及索引: 2.通过设置lottery ...
- 屏幕录制:SCR Screen Recorder Pro v0.14.3汉化破解版
应用概览 <ignore_js_op> 软件名称:屏幕录制:SCR Screen Recorder Pro 软件版本:v0.14.3汉化破解版软件语言:中文软件大小:3.5M软件包名:co ...
- UVA - 10785 The Mad Numerologist
题目链接 这个题又犯了省题不清的错误.导致不停 wa.唉. 题目意思是给你一个长度L,然后和一张表相应每一个大写字母的value值.你须要依照一定规则找出长度为L的序列. 注意 序列的value值要 ...