POJ 3468 A Simple Problem with Integers (splay tree入门)
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 47944 | Accepted: 14122 | |
| Case Time Limit: 2000MS | ||
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
/* ***********************************************
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入门)的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- 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 ...
- 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 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
随机推荐
- KVM和远程管理工具virt-manager
kvm在server端的部署(针对rhel6系统,可以构建本地更新源) 注意:如果只是安装管理工具,可以试试直接执行8步骤 1.对服务器实行kvm虚拟化首先需要确认服务器的物理硬件是否支持 cat / ...
- 奇妙的CSS之伪类与伪元素
我们都知道,在CSS中有很多选择器,例如id(#), class(.),属性[attr],这些虽然可以满足一些需要,但有时候还力有未逮.伪类和伪元素就提供了一个有益的补充,可以使我们更高效编码.伪类和 ...
- H5新特性:video与audio的使用
HTML5 DOM 为 <audio> 和 <video> 元素提供了方法.属性和事件. 这些方法.属性和事件允许您使用 JavaScript 来操作 <audio> ...
- GreenPlum学习笔记:create or replace function创建函数
原始表数据如下: 需求:现要求按分号“;”将rate_item列进行分割后插入到新的数据表中. CREATE OR REPLACE FUNCTION fun_gp_test_xxx_20181026( ...
- MySQL学习笔记:循环生成5万行id连续的数据
# ---- mysql循环生成5万行id连续的数据 ---- /* id 1 2 3 4 …… */ CREATE TABLE tb( id ) NOT NULL AUTO_INCREMENT, V ...
- Fix Valgrind's must-be-redirected error in Gentoo
Last week, I tried to use Valgrind to identify potential memory related bugs, since segmentation fau ...
- MAC下安装MAMP后,mysql server无法启动
用MAC下载安装了MAMP,之前使用是很好没问题的,但是突然无法启动mysql server,检查日志,提示InnDB出错,然后删掉了/Application/MAMP/db/mysql56目录下的i ...
- KAFKA随机产生JMX 端口指定的问题
https://blog.csdn.net/weixin_40209426/article/details/82217987
- IE6 验证码刷新失败显示空白解决办法
原因:点击a标签看不清?换图片 结果验证码显示的空白! 解决办法:在对应的点击事件最后加上return false 即可解决问题. 下面是HTML源码: <p class="regis ...
- 使用linux mysql客户端建立表时遇到格式解析的问题
发现在notepad++写好的建表脚本,粘贴到linux客户端后,执行时总是报我的脚本有问题. 我看了又看,发现建表脚本本身是没有问题,问题出在"Tab"键上和注释上边了. 解决办 ...