一、题目

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. Django常见出错解决方案汇总-乾颐堂

    一.模板类型错误: 错误原因:在models中漏掉了return: class UserProfile(models.Model): """ 功能说明:        扩 ...

  2. Part2_lesson4---ARM寻址方式

    所谓寻址方式就是处理器根据指令中给出的信息来找到指令所需操作数的方式. 1.立即数寻址 ADD R0,R0,#0x3f; R0<-R0+0x3f 在以上指令中,第二个源操作数即为立即数,要求以“ ...

  3. Oracle Nested table、Record

    1.如何在PL/SQL中创建和使用Nested table;2.如何在PL/SQL中创建和使用Record; 1.如何在PL/SQL中创建和使用Nested table DECLARE /**创建一个 ...

  4. springboot之JdbcTemplate单数据源使用

    本文介绍在Spring Boot基础下配置数据源和通过JdbcTemplate编写数据访问的示例. 数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式. ...

  5. 4619 Warm up 2

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ][]; ...

  6. 设计模式16:Mediator 中介者模式(行为型模式)

    Mediator 中介者模式(行为型模式) 依赖关系的转化 动机(Motivation) 在软件构建过程中,经常出现多个对象互相关联交互的情况,对象之间经常会维持一种复杂的应用关系,如果遇到一些需求的 ...

  7. 设计模式20:Memento 备忘录模式(行为型模式)

    Memento 备忘录模式(行为型模式) 对象状态的回溯 对象状态的变化无端,如何回溯.恢复对象在某个点的状态? 动机(Motivation) 在软件构建过程中,某些对象的状态在转换过程中,可能由于某 ...

  8. 测试驱动开发 VS 行为驱动开发

    测试驱动开发(Test Driven Development,英文缩写TDD)是极限编程的一个重要组成部分,它的基本思想就是在开发功能代码之前,先编写测试代码.也就是说在明确要开发某个功能后,首先思考 ...

  9. 【LeetCode C++】Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  10. Json字符串转对象和转List集合操作

    对象POJO和JSON互转 public class JsonUtil { /** * JSON 转 POJO */ public static <T> T getObject(Strin ...