POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)
题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = ;
const int INF = 0x3f3f3f3f;
typedef long long LL;
struct node
{
int l,r;
LL inc; //记录这个区间所要增加的值
LL sum;
};
node tree[Max * ];
int num[Max + ];
int n,q;
void buildTree(int left, int right, int k)
{
tree[k].inc = ;
tree[k].tag = INF;
tree[k].l = left;
tree[k].r = right;
if(left == right)
{
tree[k].sum = num[left];
return ;
}
int mid = (left + right) / ;
buildTree(left, mid, k * );
buildTree(mid + , right, k * + );
tree[k].sum = tree[k * ].sum + tree[k * + ].sum;
}
void upDate(int left, int right, int k, int add)
{
if(tree[k].l == left && tree[k].r == right)
{
tree[k].inc += add; //仅仅记录一下该区间所要增加的值而已
//tree[k].tag = add;
return;
}
tree[k].sum += (right - left + ) * add; //对于这样的大区间,就要求出sum
int mid = (tree[k].l + tree[k].r) / ;
if(right <= mid)
{
upDate(left, right, k * , add);
}
else if(mid < left)
{
upDate(left, right, k * + , add);
}
else
{
upDate(left, mid, k * , add);
upDate(mid + , right, k * + , add);
}
}
LL Search(int left, int right, int k)
{
if(right < left)
return ;
if(left == tree[k].l && right == tree[k].r)
{
return tree[k].sum + (right - left + ) * tree[k].inc; //原来的和 + 增加的数的和
}
tree[k].sum += (tree[k].r - tree[k].l + ) * tree[k].inc; //跟新sum的值 ,
int mid = (tree[k].l + tree[k].r) / ;
upDate(tree[k].l, mid, k * , tree[k].inc);
upDate(mid + , tree[k].r, k * + , tree[k].inc);
tree[k].inc = ;
if(right <= mid)
{
return Search(left, right, k * );
}
else if(mid < left)
{
return Search(left, right, k * + );
}
else
{
return Search(left, mid, k * ) + Search(mid + , right, k * + );
}
}
int main()
{
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++)
scanf("%d", &num[i]);
buildTree(, n, );
char opt[];
int a,b,add;
while(q--)
{
scanf("%s", opt);
if(strcmp(opt, "Q") == )
{
scanf("%d%d", &a, &b); printf("%I64d\n", Search(a, b, ));
}
else
{
scanf("%d%d%d", &a, &b, &add);
upDate(a, b, , add);
}
}
return ;
}
POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)的更多相关文章
- POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树
区间更新求和 主要用来练习splay树区间更新问题 //splay树的题解 // File Name: 3468-splay.cpp // Author: Zlbing // Created Time ...
- POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 139191 ...
- POJ-3468A Simple Problem with Integers,线段数区间更新查询,代码打了无数次还是会出错~~
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Case Time L ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- 线段树:CDOJ1597-An easy problem C(区间更新的线段树)
An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- poj3468A Simple Problem with Integers(线段树的区域更新)
http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. ...
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- Poj3468-A Simple Problem with Integers(伸展树练练手)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
随机推荐
- SQL Server使用游标或临时表遍历数据
方法一:使用游标(此方法适用所有情况,对标结构没有特殊要求.) declare @ProductName nvarchar() declare pcurr cursor for select Prod ...
- jQuery问题集锦
[1]阻止提交表单 方法1: $(function () { $("input[type=submit]").click(function (event) { //如果不满足表单提 ...
- void与void之间没有隐式转换(纯属恶搞,请勿在意)
强大的vs弹出了这个提示:.有没有觉得强大的vs不应该出现该提示. 但就是出现了. 看客,您知道怎么让vs弹出这个提示吗^~^
- mysql中insert into select from的使用
如何在mysql从多个表中组合字段然后插入到一个新表中,通过一条sql语句实现.具体情形是:有三张表a.b.c,现在需要从表b和表c中分别查几个字段的值插入到表a中对应的字段.对于这种情况,我们可以使 ...
- 对象关系映射ORM
对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换.从效 ...
- java设计模式(八) 适配器模式
[适配器模式]将一个类的接口,转换成客户期望的另外一个接口.适配器让原本接口不兼容的类可以合作无间. 1,Duck接口 package com.pattern.adapter; public inte ...
- swift 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)
函数 func x(a:Int, b:Int) {} func x(a:Int, b:Int) -> Void {} func x(a:Int, b:Int) ->(Int,Int ...
- android 概述 及四大组件
目录: 概述 四大组件 UI布局 概述 android studio中,gen很bin文件夹合并为built文件夹 四大组件 包括: 活动,服务,内容提供者,广播接收者 活动是一种包含用户界面的组件 ...
- ES6新特性:增加新类型:Symbol
本文所有Demo的运行环境都为nodeJS, 参考:让nodeJS支持ES6的词法----babel的安装和使用 : ES6新增了一种数据类型:Symbol,Symbol是用来定义对象的唯一属性名的不 ...
- java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'XXXXX' bean
今天启动srpingmvc项目的时候出现了这个异常, 原因: 在同个项目中,我复制了其中一个 Controller 作为备份 却忘记修改 @RequestMapping("/xxx&quo ...