【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 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...
随机推荐
- HDOJ/HDU 2547 无剑无我(两点间的距离)
Problem Description 北宋末年,奸臣当道,宦官掌权,外侮日亟,辽军再犯.时下战火连连,烽烟四起,哀鸿遍野,民不聊生,又有众多能人异士群起而反,天下志士云集响应,景粮影从. 值此危急存 ...
- codevs 1421 秋静叶&秋穣子(树上DP+博弈)
1421 秋静叶&秋穣子 题目描述 Description 在幻想乡,秋姐妹是掌管秋天的神明,作为红叶之神的姐姐静叶和作为丰收之神的妹妹穰子.如果把红叶和果实联系在一 起,自然会想到烤红薯 ...
- Docker官方文档翻译之入门
转自:http://www.cnblogs.com/vikings-blog/p/3958091.html Docker学习总结之docker入门 Understanding Docker 以下均翻译 ...
- JetBrains发布了一款免费的.NET反编译器dotPeek
Free .NET decompiler :: JetBrains dotPeek 主要的功能: Decompiling .NET 1.0-4.5 assemblies to C# Exporting ...
- Tomcat配置多个端口号或多个应用
一.在Tomcat下配置一个应用服务(service)中,配置多个端口号. 即一个service配置多个端口,项目可以通过多个端口访问. 修改tomcat-home\conf下的server.xml, ...
- Nunit中文文档
NUnit中文文档:http://www.36sign.com/nunit 关于特性的使用:http://www.36sign.com/nunit/attributes.html
- windows下 tomcat7 配置成服务
最简单方法:下载windows安装版,下一步下一步搞定! 非安装版: 1.下载tomcat7 windows版 2.首先找到F:\apache\bin\service.bat(不同的计算机Tomcat ...
- Redhat 官方Performance_Tuning_Guide
https://access.redhat.com/documentation/zh-CN/Red_Hat_Enterprise_Linux/6/html/Performance_Tuning_Gui ...
- spring mvc DispatcherServlet详解之一--request通过HandlerMaping获取控制器Controller过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappi ...
- jQuery 插件开发 笔记
JQuery 插件开发: 类级别开发,开发新的全局函数 对象级别开发,给Jquery对象开发新方法 一.类级别开发 -定义全局方法 jQuery.foo = function() { alert('T ...