链接:

http://poj.org/problem?id=3468

代码:

 #include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = 1e5+; struct SegmentTree
{
int L, R;
long long sum, e;
int Mid()
{
return (L+R)>>;
}
int len()
{
return R-L+;
}
}a[N<<]; void BuildSegTree(int r, int L, int R)
{
a[r].L=L, a[r].R=R;
a[r].e=; if(L==R)
{
scanf("%lld", &a[r].sum);
return ;
} BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R); a[r].sum = a[Lson].sum + a[Rson].sum;
}
void PushDown(int r)
{
a[Lson].sum += a[r].e*a[Lson].len();
a[Lson].e += a[r].e;
a[Rson].sum += a[r].e*a[Rson].len();
a[Rson].e += a[r].e; a[r].e=;
}
void Update(int r, int L, int R, int e)
{
a[r].sum += (R-L+)*e; if(a[r].L==L && a[r].R==R)
{
a[r].e += e;
return ;
} PushDown(r); if(R<=a[r].Mid())
Update(Lson, L, R, e);
else if(L>a[r].Mid())
Update(Rson, L, R, e);
else
{
Update(Lson, L, a[r].Mid(), e);
Update(Rson, a[r].Mid()+, R, e);
}
}
long long Query(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].sum; PushDown(r); if(R<=a[r].Mid())
return Query(Lson, L, R);
else if(L > a[r].Mid())
return Query(Rson, L, R);
else
{
long long Lsum = Query(Lson, L, a[r].Mid());
long long Rsum = Query(Rson, a[r].Mid()+, R); return Lsum+Rsum;
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
BuildSegTree(, , n); while(m--)
{
char s[];
int L, R, e; scanf("%s", s); if(s[]=='Q')
{
scanf("%d%d", &L, &R);
printf("%lld\n", Query(, L, R));
}
else
{
scanf("%d%d%d", &L, &R, &e);
Update(, L, R, e);
}
}
}
return ;
}

(线段树模板)A Simple Problem with Integers --POJ--3468的更多相关文章

  1. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

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

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

  3. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

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

  4. 【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 ...

  5. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  6. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  7. A Simple Problem with Integers~POJ - 3468

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  8. A Simple Problem with Integers POJ - 3468 (分块)

    题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...

  9. 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers

    https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...

  10. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

随机推荐

  1. Object-c 创建按钮

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //动态创建我们自己的按钮 //1.创建按钮(UIB ...

  2. hibernate的异常 Session was already closed

    今天写hibernate时候遇到一些异常 代码: Session session = sessionFactory.getCurrentSession(); session.beginTransact ...

  3. 复制CentOS虚拟机网络配置

    复制出来的CentOS虚拟机,网络需要重新配置.   卸载原来的VMware网卡,重新启用一块新的网卡,网卡网段要匹配.   ifconfig -a 查看当前启用网卡的mac地址 编辑/etc/ude ...

  4. java多线程实例(2)

    public class ThreadDemo05 { public static void main(String args[]) { // 四个售票点应该控制同一个资源 Demo d = new ...

  5. 断开的管道 java.io.IOException: Broken pipe 解决方法

    一.Broken pipe产生原因分析 1.当访问某个服务突然服务器挂了,就会产生Broken pipe; 2.客户端读取超时关闭了连接,这时服务器往客户端再写数据就发生了broken pipe异常! ...

  6. mysql for update语句

    我们都知道for update语句会锁住一张表,锁表的细节很多人却不太清楚,下面我们举例看下. 在表上我们有个索引,如下: 现在在我们通过索引store_id锁表: 我们再开一个客户端,还是锁住同一个 ...

  7. 源码包的解压 .tar.gz /.tar.bz2的解压

    解压方式如下: .tar.gz     格式解压为          tar   -zxvf   xx.tar.gz .tar.bz2   格式解压为          tar   -jxvf    ...

  8. suse11 sp4(虚拟机) 安装程序时报错 找不到iso

    一个可能原因是iso掉了.我用的virtualbox安装的suse,支持不是很好,suse启动后,因为驱动问题强制umount了iso,所以掉了.重启后,不要去动virtualbox插件问题,插件错误 ...

  9. N皇后问题12 · N-Queens

    [抄题]: n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中“Q”和“.”分 ...

  10. 'WebElement' object is not iterable

    checkbox.html源码: <html> <head> <meta http-equiv="content-type" content=&quo ...