A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 47944   Accepted: 14122
Case Time Limit: 2000MS

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

 
 
 
 
 
 
 
 
 /* ***********************************************
Author :kuangbin
Created Time :2013/8/24 22:13:15
File Name :F:\2013ACM练习\专题学习\splay_tree_2\POJ3468.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//普通的线段树操作,区间增加一个值,查询区间的和
#define Key_value ch[ch[root][1]][0]
const int MAXN = ;
int pre[MAXN],ch[MAXN][],root,tot1;
int size[MAXN];//子树的结点数
int add[MAXN];//增量的延迟标记
int key[MAXN];
long long sum[MAXN];//子树的和
int s[MAXN],tot2;//内存池和容量
int a[MAXN];//初始的数组,建树的时候用,下标从1开始 //debug部分**********************************
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d add = %2d sum = %I64d\n",x,ch[x][],ch[x][],pre[x],size[x],add[x],sum[x]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
//以上是debug部分************************************** void NewNode(int &r,int father,int k)
{
if(tot2)r = tot2--;//取的时候是tot2--,存的时候就是++tot2
else r = ++tot1;
pre[r] = father;
size[r] = ;
add[r] = ;
key[r] = k;
sum[r] = k;
ch[r][] = ch[r][] = ;
}
//给r为根的子树增加值,把当前结点更新掉,加延迟标记
void Update_Add(int r,int ADD)
{
if(r == )return;
add[r] += ADD;
key[r] += ADD;
sum[r] += (long long)ADD*size[r];
}
void push_up(int r)
{
size[r] = size[ch[r][]] + size[ch[r][]] + ;
sum[r] = sum[ch[r][]] + sum[ch[r][]] + key[r];
}
void push_down(int r)
{
if(add[r])
{
Update_Add(ch[r][],add[r]);
Update_Add(ch[r][],add[r]);
add[r] = ;
}
}
//建树
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l+r)/;
NewNode(x,father,a[mid]);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
push_up(x);
}
int n,q;
//初始化
void Init()
{
root = tot1 = tot2 = ;
ch[root][] = ch[root][] = pre[root] = size[root] = add[root] = sum[root] = key[root] = ;
//加两个虚结点
NewNode(root,,-);
NewNode(ch[root][],root,-);
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
Build(Key_value,,n,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
//旋转,0为左旋,1为右旋
void Rotate(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);//先把y的标记下传,在把x的标记下传
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
//Splay调整,将r结点调整到goal下面
void Splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
Rotate(r,ch[pre[r]][]==r);
else
{
int y = pre[r];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == ) root = r;
}
//得到第k个结点
int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][]] + ;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][],k);
else return Get_kth(ch[r][],k-t);
} //区间增加一个值
//因为加了个空结点,所以将第l个点旋转到根结点,第r+2个点旋转到根结点的右孩子
//那么Key_value就是需要修改的区间[l,r]
void ADD(int l,int r,int D)
{
Splay(Get_kth(root,l),);
Splay(Get_kth(root,r+),root);
Update_Add(Key_value,D);
push_up(ch[root][]);
push_up(root);
}
//查询区间的和
long long Query_sum(int l,int r)
{
Splay(Get_kth(root,l),);
Splay(Get_kth(root,r+),root);
return sum[Key_value];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&q) == )
{
Init();
//debug();
int x,y,z;
char op[];
while(q--)
{
scanf("%s",op);
if(op[] == 'Q')
{
scanf("%d%d",&x,&y);
printf("%I64d\n",Query_sum(x,y));
}
else
{
scanf("%d%d%d",&x,&y,&z);
ADD(x,y,z);
}
}
}
return ;
}
 
 

POJ 3468 A Simple Problem with Integers (splay tree入门)的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  6. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  7. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  8. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

随机推荐

  1. gc overhead limit exceeded内存问题

    gc overhead limit exceeded 当出现这种问题的时候一般有两种思路 一.修改idea或者eclipse中的配置文件,将内存调到1024即可 二.在代码中通过system.gc 手 ...

  2. 利用CSS函数calc(...)实现Web页面左右布局

    前言 因为自己的网站需要,想要做一个左右布局的页面: 左边是导航菜单之类的东西.右边是文档内容(因为最近看的一些软件的文档页面都是这么布局的): 左边固定宽度——300像素.右边使用剩余的宽度: 左边 ...

  3. Mac ssh

    mac的终端默认在打开一个新的tab/window的时候需要重新输入ssh的密码, 很不方便.本文完成在mac中设置,实现secureCRT/xshell里的克隆会话功能, 即新开一个terminal ...

  4. Oracle 中count(1) 、count(*) 和count(列名) 函数的区别

    1)count(1)与count(*)比较: 1.如果你的数据表没有主键,那么count(1)比count(*)快2.如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快3. ...

  5. HTML5 Differences from HTML4

    Abstract "HTML5 Differences from HTML4" describes the differences of the HTML5 specificati ...

  6. 20165333 实验二 Java面向对象程序设计

    姓名:陈国超 学号:20165333 班级:1653 实验课程:JAVA程序设计 实验名称:Java面向对象程序设计 实验时间:2018.4.14 指导老师:娄家鹏 实验内容及步骤 (一) " ...

  7. GUC-13 生产者和消费者案例-旧

    /* * 生产者和消费者案例 */ public class TestProductorAndConsumer { public static void main(String[] args) { C ...

  8. CRLF LF CR

    The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without ...

  9. CF455D. Serega and Fun

    D. Serega and Fun time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  10. Java 初相识

    Java是如何出现的呢?这就要回到1991年,那时候随着单片机的发展,出现了很多微型的系统,Sun公司在这个时候就成立的一个项目组,成员就有我们熟知的“Java之父” 詹姆斯·高斯林,起初的目标是为了 ...