题目链接

题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和

参考斌神的代码

 #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(区间加数求和 + 线段树)的更多相关文章

  1. POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树

    区间更新求和 主要用来练习splay树区间更新问题 //splay树的题解 // File Name: 3468-splay.cpp // Author: Zlbing // Created Time ...

  2. POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)

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

  3. POJ-3468A Simple Problem with Integers,线段数区间更新查询,代码打了无数次还是会出错~~

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K                Case Time L ...

  4. poj------(3468)A Simple Problem with Integers(区间更新)

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

  5. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  6. 线段树:CDOJ1597-An easy problem C(区间更新的线段树)

    An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  7. poj3468A Simple Problem with Integers(线段树的区域更新)

    http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. ...

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

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

  9. Poj3468-A Simple Problem with Integers(伸展树练练手)

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

随机推荐

  1. 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题

    在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...

  2. suggest插件实现下拉选择筛选

    实现的效果展示: 代码如下: 1.需要引入jquery,bootstrap-suggest.js,bootstrap.min.css 2.html页面代码: <!DOCTYPE html> ...

  3. Spearman Rank(斯皮尔曼等级)相关系数及MATLAB实现

    转自:http://blog.csdn.net/wsywl/article/details/5859751 Spearman Rank(斯皮尔曼等级)相关系数 1.简介 在统计学中,斯皮尔曼等级相关系 ...

  4. Linux 使用 iptables屏蔽IP段

    netfilter/iptables IP 信息包过滤系统是一种功能强大的工具,可用于添加.编辑和除去规则,这些规则是在做信息包过滤决定时,防火墙所遵循和组成的规则.这些规则存储在专用的信息包过滤表中 ...

  5. 十天冲刺---Day7

    站立式会议 站立式会议内容总结: 燃尽图 照片 两个人编码其实效率挺高的.但是在一些方面,比如说页面UI的编写,会非常吃力,很难达到自己的效果. 由于埋头在编码,所以issues的增加随之停止. 有点 ...

  6. CEPH浅析”系列之三——CEPH的设计思想

    Ceph针对的目标应用场景 理解Ceph的设计思想,首先还是要了解Sage设计Ceph时所针对的目标应用场景,换言之,"做这东西的目的是啥?" 事实上,Ceph最初针对的目标应用场 ...

  7. C++中的重载隐藏覆盖&&JAVA中的重载覆盖&&多态

    class 类继承默认是private, struct 默认继承是public C++中的隐藏: 只要派生类中出现和基类一样的函数名,基类中的函数就会被派生类中的函数给隐藏(如果派生类和基类中的函数名 ...

  8. svg是什么

    SVG,指可缩放矢量图形(Scalable Vector Graphics),是用于描述二维矢量图形的一种图形格式,是由万维网联盟制定的开放标准.SVG 使用 XML 格式来定义图形,除了 IE8 之 ...

  9. linux centos中使用yum安装tomcat

    在linux下部署java开发的web应用,一般采用Tomact+jre环境(可不需要apache),在RHEL和CentOS下,可以采用yum在线自动安装方式安装,具体操作如下: 可以先查看tomc ...

  10. Jquery-pagination.js分页处理

    首先提供pagination.js的插件源码 /** * This jQuery plugin displays pagination links inside the selected elemen ...