Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

Source

【分析】
这个其实是个水题。我只是拿来fhq treap的...
Orzzzzzz范大爷
这种数据结构真是...能保证treap的性质同时也能保证合并后的树还能原封不动的切出来...
说它是treap,我觉得就一个fix像treap而已。
这个数据结构的精髓在合并和分裂(和打标记?)。有注释.
 #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的更多相关文章

  1. 一本通1548【例 2】A Simple Problem with Integers

    1548:[例 2]A Simple Problem with Integers 题目描述 这是一道模板题. 给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类: 1 ...

  2. 【fhq Treap】bzoj1500(听说此题多码上几遍就能不惧任何平衡树题)

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 15112  Solved: 4996[Submit][Statu ...

  3. 【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 ...

  4. 【POJ3468】【树状数组区间修改】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  5. 【成端更新线段树模板】POJ3468-A Simple Problem with Integers

    http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...

  6. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

  7. 【POJ 3468】 A Simple Problem with Integers

    [题目链接] 点击打开链接 [算法] 本题用线段树很容易写,但是,笔者为了练习树状数组,就用树状数组的方法做了一遍 我们不妨引入差分数组c, 则sum(n) = c[1] + (c[1] + c[2] ...

  8. 【poj3468】A Simple Problem with Integers

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97008   Accepted: 30285 Case Time Limi ...

  9. 【poj3468】 A Simple Problem with Integers

    http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...

随机推荐

  1. BZOJ4195 [Noi2015]程序自动分析(离散化+并查集)

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 689  Solved: 296 [Submit][Sta ...

  2. [转]让程序在崩溃时体面的退出之SEH

    原文地址:http://blog.csdn.net/starlee/article/details/6636723 SEH的全称是Structured Exception Handling,是Wind ...

  3. 在PHP网页中,如何把$_session["yyy"]赋值到一个文本框中?

    echo '<input type="text" id="text1" name="text1" value="'.$_SE ...

  4. [转]linux的ulimit各种限制之深入分析

    这是一篇非常好的文章,对ulimit的各个限制参数讲得非常透彻.原文链接:http://home.lupaworld.com/home-space-uid-56821-do-blog-id-23281 ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. 干货!如何正确使用Git Flow

    我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确使用. Git的优点 Git的优点很多,但是这里只列出我认为 ...

  7. 352. Data Stream as Disjoint Intervals

    Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...

  8. linux —— 学习笔记(文件、文件夹操作)

    目录:1.常用的文件文件夹操作 2.文件属性的设置 1.常用的文件文件夹操作 mkdir  创建文件夹 -p 如果指定 a/b/c 时 a .b 不存在,一起创建出来 cp       复制文件或文件 ...

  9. chrome浏览器打开网页,总是跳转到2345主页的解决方法 2345.com 绑架主页

    昨晚装了一个wifi共享精灵,原本以为这下好了,全宿舍都可以上网了,但是,确实噩梦的开始啊. 遇到问题:不小心在安装wifi共享精灵的时候,点到了设置2345.com为主页,后来,每次使用chrome ...

  10. 下载的chm手册打不开的解决方法?

    用ie或者chrome等浏览器下载文件的时候,都会给文件加上一个默认的保护,右键这个文件,打开属性对话框,然后在第一个的选项卡的安全的部分,有个解除这个保护的按钮点下然后确定保存,再打开chm文件就不 ...