A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 115624 | Accepted: 35897 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
- 10 5
- 1 2 3 4 5 6 7 8 9 10
- Q 4 4
- Q 1 10
- Q 2 4
- C 3 6 3
- Q 2 4
Sample Output
- 4
- 55
- 9
- 15
Hint
Source
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<sstream>
- #include<algorithm>
- #include<queue>
- #include<deque>
- #include<iomanip>
- #include<vector>
- #include<cmath>
- #include<map>
- #include<stack>
- #include<set>
- #include<fstream>
- #include<memory>
- #include<list>
- #include<string>
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- #define MAXN 100009
- #define L 31
- #define INF 1000000009
- #define eps 0.00000001
- /*
- 线段树 区间更新区间查询
- */
- LL a[MAXN],pre[MAXN];
- struct node
- {
- LL l, r;
- LL data, sum, laz;
- }T[MAXN*];
- void build(LL p, LL l, LL r)
- {
- T[p].data = T[p].sum = T[p].laz = ;
- T[p].l = l, T[p].r = r;
- if (l == r) return;
- LL mid = (l + r) / ;
- build(p * , l, mid);
- build(p * + , mid + , r);
- }
- void update(LL p, LL l, LL r, LL v)
- {
- //cout << p << ' ' << l << ' ' << r << ' ' << v << endl;
- if (T[p].l >= l && T[p].r <= r)
- {
- T[p].data += v;
- T[p].laz = ;
- T[p].sum += (T[p].r - T[p].l + ) * v;
- return;
- }
- LL mid = (T[p].l + T[p].r) / ;
- if (T[p].laz)
- {
- T[p].laz = ;
- update(p * , T[p].l, mid, T[p].data);
- update(p * + , mid + , T[p].r, T[p].data);
- T[p].data = ;
- }
- if (r <= mid)
- update(p * , l, r, v);
- else if (l > mid)
- update(p * + , l, r, v);
- else
- {
- update(p * , l, mid, v);
- update(p * + , mid + , r, v);
- }
- T[p].sum = T[p * ].sum + T[p * + ].sum;
- }
- LL query(LL p, LL l, LL r)
- {
- if (l == T[p].l&&r == T[p].r)
- return T[p].sum;
- LL mid = (T[p].l + T[p].r) / ;
- if (T[p].laz)
- {
- T[p].laz = ;
- update(p * , T[p].l, mid, T[p].data);
- update(p * + , mid + , T[p].r, T[p].data);
- T[p].data = ;
- }
- if (r <= mid)
- return query(p * , l, r);
- else if (l > mid)
- return query(p * + , l, r);
- else
- return query(p * , l, mid) + query(p * + , mid + , r);
- }
- LL n, q;
- int main()
- {
- scanf("%lld%lld", &n, &q);
- for (LL i = ; i <= n; i++)
- scanf("%lld", &a[i]), pre[i] = pre[i - ] + a[i];
- char c[];
- LL a, b, d;
- build(,,n);
- while (q--)
- {
- scanf("%s", c);
- if (c[] == 'Q')
- scanf("%lld%lld", &a, &b), printf("%lld\n", query(, a, b) + pre[b] - pre[a-]);
- else
- scanf("%lld%lld%lld", &a, &b, &d), update(, a, b, d);
- }
- }
A Simple Problem with Integers 线段树 区间更新 区间查询的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- Hadoop的数据采集框架
问题导读: Hadoop数据采集框架都有哪些? Hadoop数据采集框架异同及适用场景? Hadoop提供了一个高度容错的分布式存储系统,帮助我们实现集中式的数据分析和数据共享.在日常应用中我们比如要 ...
- MySql数据库存储emoji表情报错解决办法
异常:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1 解决: ...
- SpringMVC高级课程
requestBody和responseBody requestBody把前台页面传递JSON格式数据强制转换JavaBean responseBody在后台把javabean转换成JSON格式的数据 ...
- jvm内存分区
java内存是由jvm进行管理的,其内存简易模型如下图: jvm管理的内存大体上可分为方法区.堆.程序计数器.线程栈.本地方法区这几部分.方法区:主要存放类的元信息(包括类的名称.修饰符.静态变量.f ...
- HTML空格占位
它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的 ...
- Jmeter各组件介绍 及 使用
本篇主要讲述Jmeter的各个组件及简单使用,其中包括以下内容: 一.线程组二.逻辑控制器三.配置元件四.定时器五.后置处理器六.断言七.监听器 八.参数化 网上大神整理的链接:http://blog ...
- java设计模式之策略模式总结
策略模式的定义:(定义截自http://www.cnblogs.com/whgk/p/6087064.html) 1.策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立 ...
- StyleAI厚积薄发: Android网络图片数据传输
在StyleAI上厚积了这么长时间,憋了这么久,本来想憋个更大的,不过还是薄发一次的好. 三.直接使用别人的工程 文章:Android学习之客户端上传图片到服务器 下载地址:https://downl ...
- Jmeter的属性和变量
jmeter的属性和变量可以简单理解为编程里面的全局变量和局部变量.属性是全局可见,可以跨线程组传递调用,而变量基本上只能存在于一个线程组中(在测试计划定义的变量也是可以跨线程组传递的).同线程组内的 ...
- JavaScript 中实现 sleep
来自推特上 Windows 故障分析的笑话 图片来源:me.me 推上看到的笑话,Windows 故障分析的实现. 然后想起来 JavaScript 中如何实现这个 sleep() 函数让代码暂停指定 ...