一、题目

POJ3468

二、分析

裸的线段树区间查询+修改。

三、AC代码

 #include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; #define ll long long
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int MAXN = 1e5 + ;
ll add[MAXN << ], sum[MAXN << ]; void PushUp(int rt)
{
sum[rt] = sum[rt << ] + sum[rt << | ];
return;
} void PushDown(int ln, int rn, int rt)
{
if(add[rt])
{
sum[rt << ] += (ll) ln * add[rt];
sum[rt << | ] += (ll) rn * add[rt];
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
add[rt] = ;
}
return;
} void Build(int l, int r, int rt)
{
add[rt] = ;
if(l == r)
{
scanf("%lld", &sum[rt]);
return;
}
int mid = (l + r) >> ;
Build(lson);
Build(rson);
PushUp(rt);
return;
} void Update(int L, int R, int c, int l, int r, int rt)
{
if(L <= l && r <= R)
{
add[rt] += c;
sum[rt] += (ll)(r - l + ) * c;
return;
}
int mid = (l + r) >> ;
PushDown(mid - l + , r - mid, rt);
if(L <= mid)
{
Update(L, R, c, lson);
}
if(R > mid)
{
Update(L, R, c, rson);
}
PushUp(rt);
return;
} ll Query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R)
return sum[rt];
int mid = (l + r) >> ;
PushDown(mid - l + , r - mid, rt);
ll ans = ;
if(L <= mid)
{
ans += Query(L, R, lson);
}
if(R > mid)
{
ans += Query(L, R, rson);
}
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
int N, Q;
scanf("%d %d", &N, &Q);
Build(, N, );
char s[];
for(int i = ; i < Q; i++)
{
int L, R, C;
scanf("%s", s);
if(s[] == 'Q')
{
scanf("%d %d", &L, &R);
printf("%I64d\n", Query(L, R, , N, ));
}
else
{
scanf("%d %d %d", &L, &R, &C);
Update(L, R, C, , N, );
}
}
}

POJ_3468 A Simple Problem with Integers 【线段树区间查询+修改】的更多相关文章

  1. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

  2. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  3. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

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

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

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  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 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

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

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

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

随机推荐

  1. arping

    强制交换机刷新MAC arping -I em2(网卡名称) 58.215.88.8(Vip)

  2. linux下两台服务器文件实时同步方案实现-乾颐堂

    假设有如下需求: 假设两个服务器: 192.168.0.1 源服务器  有目录 /opt/test/ 192.168.0.2 目标服务器  有目录 /opt/bak/test/ 实现的目的就是保持这两 ...

  3. 启动Hadoop HDFS时的“Incompatible clusterIDs”错误原因分析

    "Incompatible clusterIDs"的错误原因是在执行"hdfs namenode -format"之前,没有清空DataNode节点的data目 ...

  4. 大型Unity手游《英雄之刃-最后之战》源码分析

    英雄之刃之最后一战是国内首款原创精品MOBA手游,是一款由前暴雪文案亲自操刀世界观,日韩专业团队打造美术场景,新加坡团队精心制作战斗音乐的旷世之作! 超快速的匹配对战.默契的团队协作给你带来意犹未尽的 ...

  5. sheel远程执行

    https://www.cnblogs.com/softidea/p/6855045.html shell远程执行: 经常需要远程到其他节点上执行一些shell命令,如果分别ssh到每台主机上再去执行 ...

  6. [LeetCode 题解]: Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. [Erlang15]“hello world”与<<”hello world”>>的具体区别是什么?

    参见 :http://learnyousomeerlang.com/buckets-of-sockets 为了加深理解,自译如下,若理解有误或更好的建议,请帮忙指出, :) Buckets of So ...

  8. 优化案例--改写IN条件为INNER JOIN

    --====================================== --原始语句 SET STATISTICS IO ON SELECT COUNT(DISTINCT parent_co ...

  9. 解决centOS 本地可以访问 外部主机不能访问的问题

    但是centos中的防火墙规则比较严密 用curl http://localhost:10000 可以看到内容,但是外部无法访问,原因是防火墙没有开启10000端口,需要将10000端口加入到信任规则 ...

  10. 【python】@property装饰器

    Python内置的@property装饰器可以把类的方法伪装成属性调用的方式.也就是本来是Foo.func()的调用方法,变成Foo.func的方式.在很多场合下,这是一种非常有用的机制. class ...