题意:N个数Q次操作。一共两种操作:Q l r :询问[l,r]这个区间里的数字和,C l r c: [l,r]区间里的每个数都加上c。1 ≤ N,Q ≤ 100000.

方法:线段树的成段更新。注意懒惰标记。这只是为了有个模板。易错点在代码中以下划线标注。

//16:06
#include <cstdio>
#include <cstring>
#define N 100010
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 long long sum[N<<];
long long col[N<<];//一直WA,只因这里写成Int。这个和上面那个要同个类型才是。 void pushUp(int rt) {
sum[rt] = sum[rt<<] + sum[rt<<|];
} void pushDown(int len, int rt) {
if (col[rt]) {
col[rt<<] += col[rt];
col[rt<<|] += col[rt];
sum[rt<<] += (len-len/)*col[rt];
sum[rt<<|] += (len/)*col[rt];
col[rt] = ;
}
} void build(int l, int r, int rt) {
col[rt] = ;
if (l==r) {
scanf("%lld", &sum[rt]);
return;
}
int mid = (l+r)/;
build(lson);
build(rson);
pushUp(rt);
} void update(int L, int R, int v, int l, int r, int rt) {
if (L <= l && r <= R) {
col[rt] += v;
sum[rt] += (r-l+1ll)*v;
return;
}
pushDown(r-l+, rt);
int mid = (l+r)/;
if (L <= mid) update(L,R,v,lson);
if (R > mid) update(L,R,v,rson);
pushUp(rt);
} long long query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return sum[rt];
}
pushDown(r-l+,rt);
int mid = (l+r)/;
long long ans = ;
if (L <= mid) ans += query(L,R,lson);
if (R > mid) ans += query(L,R,rson);
return ans;
} int main() {
int n, q;
while (scanf("%d%d", &n, &q) != EOF) {
build(,n,);
for (int i = ; i < q; i++) {
char com[];
scanf("%s", com);
if (com[] == 'Q') {
int l, r = ;
scanf("%d%d", &l, &r);
//printf("query %d %d\n", l, r);
printf("%lld\n", query(l,r,,n,));
} else if (com[] == 'C') {
int l, r, add;
scanf("%d%d%d", &l, &r, &add);
update(l,r,add,,n,);
}
}
}
return ;
}

另外感觉三个函数有很多重复点,写了一个紧凑版本,不过看起来代码量差不多,而且效率低了呢。

//16:06
#include <cstdio>
#include <cstring>
#define N 100010
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 long long sum[N<<];
long long col[N<<]; void pushUp(int rt) {
sum[rt] = sum[rt<<] + sum[rt<<|];
} void pushDown(int len, int rt) {
if (col[rt]) {
col[rt<<] += col[rt];
col[rt<<|] += col[rt];
sum[rt<<] += (len-len/)*col[rt];
sum[rt<<|] += (len/)*col[rt];
col[rt] = ;
}
} long long basicDo(bool isBuild, int L, int R, int v, int l, int r, int rt) {
if (isBuild) col[rt] = ;
if (l == r || L <= l && r <= R) {
if (isBuild) scanf("%lld", &sum[rt]);
col[rt] += v;
sum[rt] += (r-l+1ll)*v;
return sum[rt];
}
pushDown(r-l+, rt);
int mid = (l+r)/;
long long ans = ;
if (isBuild || L<= mid) ans += basicDo(isBuild,L,R,v,lson);
if (isBuild || R > mid) ans += basicDo(isBuild,L,R,v,rson);
pushUp(rt);
return ans;
}
void build(int l, int r, int rt) {
basicDo(true, ,,,l,r, rt);
}
void update(int L, int R, int v, int l, int r, int rt) {
basicDo(false, L, R, v, l, r, rt);
}
long long query(int L, int R, int l, int r ,int rt) {
return basicDo(false, L, R, , l, r, rt);
} int main() {
int n, q;
while (scanf("%d%d", &n, &q) != EOF) {
build(,n,);
for (int i = ; i < q; i++) {
char com[];
scanf("%s", com);
if (com[] == 'Q') {
int l, r = ;
scanf("%d%d", &l, &r);
//printf("query %d %d\n", l, r);
printf("%lld\n", query(l,r,,n,));
} else if (com[] == 'C') {
int l, r, add;
scanf("%d%d%d", &l, &r, &add);
update(l,r,add,,n,);
}
}
}
return ;
}

POJ 3468:A Simple Problem with Integers(线段树[成段更新])的更多相关文章

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

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

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

  4. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  5. 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 ...

  6. 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 ...

  7. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  8. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

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

  10. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

随机推荐

  1. CentOS 编译安装PHP5.6(7以上也通用)

    由于公司有新服务器需要构建一套LNMP平台,且需要编译安装各个部件,所以记录下此文章. 这是安装PHP涉及到的软件包(可以自行决定使用哪个版本): ├── libiconv-1.15.tar.gz ├ ...

  2. NOIP模拟赛 czy的后宫5

    描述 czy要召集他的妹子,但是由于条件有限,可能每个妹子不能都去,但每个妹子都有一个美丽值,czy希望来的妹子们的美丽值总和最大(虽然……). czy有一个周密的电话通知网络,它其实就是一棵树,根结 ...

  3. 自动化运维工具——pssh

    PSSH介绍 pssh是一个python编写可以在多台服务器上执行命令的工具,同时支持拷贝文件,是同类工具中很出色的.它的用法可以媲美ansible的一些简单用法,执行起来速度比ansible快它支持 ...

  4. makedown语法

    文章转载至:https://blog.csdn.net/u014061630/article/details/81359144#1-%E5%BF%AB%E6%8D%B7%E9%94%AE 前言 写过博 ...

  5. jmeter中基于oracle的JDBC Request的使用

    前提条件: 1.有数据库:2.数据库中有表,例如testuser(userid,username,usepwd): 设置如下: 参考自:http://www.linuxidc.com/Linux/20 ...

  6. CDH4 journalnode方式手工安装手册之三

    一.                                启动JournalNode 每台机器都要执行: mkdir -p /smp/hadoop-cdh4/bch/ chmod -R 77 ...

  7. UVa 11552 DP Fewest Flops

    题解 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

  8. MongoDB学习-->命令行增删改查&JAVA驱动操作Mongodb

    MongoDB 是一个基于分布式文件存储的数据库. 由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关 ...

  9. Linux IP怎么设置

    最常用的给网卡配置ip的命令为#ifconfig eth0 192.168.0.1 但是,这样重启后又打回原形.要想永久保存,需要 vim /etc/sysconfig/network-scripts ...

  10. “玲珑杯”线上赛 Round #17 河南专场

    闲来无事呆在寝室打打题,没有想到还有中奖这种操作,超开心的 玲珑杯”线上赛 Round #17 河南专场 Start Time:2017-06-24 12:00:00 End Time:2017-06 ...