A Simple Problem with Integers BZOJ3212 线段树
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.
Sample Output4
55
9
15
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 Input10 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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n;
ll a[maxn]; struct node {
ll l, r;
ll lazy;
ll sum;
}tree[maxn<<1]; void pushup(int rt) {
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
} void build(int l, int r, int rt) {
tree[rt].l = l; tree[rt].r = r; tree[rt].lazy = 0;
if (l == r) {
tree[rt].lazy = 0;
tree[rt].sum = a[l]; return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1);
pushup(rt);
} void pushdown(int rt) {
if (tree[rt].lazy) {
tree[rt << 1].sum += (ll)tree[rt].lazy*(tree[rt << 1].r - tree[rt << 1].l + 1);
tree[rt << 1 | 1].sum += (ll)tree[rt].lazy*(tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt].lazy = 0;
}
} void upd(int l, int r, int rt,ll val) {
if (l <= tree[rt].l&&tree[rt].r <= r) {
tree[rt].sum += (tree[rt].r - tree[rt].l + 1)*val;
tree[rt].lazy += val; return;
}
pushdown(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if (l <= mid)upd(l, r, rt << 1, val);
if (mid < r)upd(l, r, rt << 1 | 1, val);
pushup(rt);
} ll query(int l, int r, int rt) {
if (l <= tree[rt].l&&tree[rt].r <= r) {
return tree[rt].sum;
}
pushdown(rt);
int mid = (tree[rt].r + tree[rt].l) >> 1;
ll ans = 0;
if (l <= mid)ans += query(l, r, rt << 1);
if (mid < r)ans += query(l, r, rt << 1 | 1);
return ans;
} int main()
{
//ios::sync_with_stdio(0);
rdint(n); int q; rdint(q);
for (int i = 1; i <= n; i++)rdllt(a[i]);
build(1, n, 1);
while (q--) {
char op; int a, b;
cin >> op;
if (op == 'C') {
ll v; rdint(a); rdint(b); rdllt(v);
upd(a, b, 1, v);
}
else {
rdint(a); rdint(b);
cout << query(a, b, 1) << endl;
}
}
return 0;
}
A Simple Problem with Integers BZOJ3212 线段树的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
- POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 140120 ...
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
- POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 139191 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
随机推荐
- Ubuntu中vi命令的使用
指今说明: 进入 vi 的命令vi filename: 打开或新建文件,并将光标置于第一行首 vi +n filename: 打开文件,并将光标置于第n行首 vi + filename: 打开文件,并 ...
- python中引号中有双引号
#/usr/bin/python import os name = "ABC" #ABC是具体的模块名,产品经理每一次给的模块名字都不一样 os.environ['name'] = ...
- javascript——事件处理模型(DOM 和 IE)
javascript的事件处理模型分为 DOM事件处理模型和 IE事件处理模型. 一.DOM事件流模型 DOM事件流分为三个阶段:捕获阶段.目标阶段.冒泡阶段. 捕获阶段:自上而下,由document ...
- leetcode559
class Solution { public: int maxDepth(Node* root) { ; if (root != NULL) { queue<Node> Q; Q.pus ...
- C++中cin.get(),cin.getline(),cin>>,gets(),cin.clear()使用总结
1.cin.get() 实质:类istream所定义对象cin的重载成员函数 用于读取单字符 istream& get(char&) int get(void) 用于读取字符 ...
- Ros学习——roslaunch
roslaunch:启动定义在launch文件中的多个节点 1.launch文件解析 <launch> #以launch标签开头以表明这是一个launch文件 #两个节点分组并以'命名空间 ...
- java web前端发送请求的4种方式
表单 action, 链接href,js 绑定按钮 ajax绑定标签 <h1>通过表单提交参数</h1> <form action="/day46_v1/Ser ...
- 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示
1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...
- Python 网络爬虫 002 (入门) 爬取一个网站之前,要了解的知识
网站站点的背景调研 1. 检查 robots.txt 网站都会定义robots.txt 文件,这个文件就是给 网络爬虫 来了解爬取该网站时存在哪些限制.当然了,这个限制仅仅只是一个建议,你可以遵守,也 ...
- c# 新中新二代身份证阅读,包含头像,支持华视
需要用到dll和文件: 其中3个dll文件是需要调用的dll,license.dat文件为解压图片的授权文件 以下是需要用到的dll里面的方法: /************************端口 ...