【线段树 && 区间增减】POJ 3468 A Simple Problem with Integers
来源:互联网 发布:小组抽签软件 编辑:程序博客网 时间:2024/06/06 09:01
Problem Description
裸的区间增减的题目,由于数据有点大,得用long long。输入n,m 有n个元素,m次询问。C a b c 将区间a-b的数全部加c,Q a b,求区间a-b的和
代码:
#include<cstdio>using namespace std;struct node{ long long num, lazy;};node tree[400000];#define MID int mid = (l + r) / 2//预处理#define lson root<<1#define rson root<<1|1node merge_(node x, node y)//回潮求和{ node t; t.lazy = 0;//因为这里没有初始化,wrong了 好多遍 t.num = x.num + y.num; return t;}void build(int root, int l, int r)//初始化线段树{ tree[root].lazy = 0;//初始化0 if(l == r) { scanf("%lld", &tree[root].num);//输入 return ; } MID; build(lson, l, mid);//左子树递归 build(rson, mid + 1, r);//右子树递归 tree[root] = merge_(tree[lson], tree[rson]);}void pushdown(int root, int l, int r)//向下更新{ if(tree[root].lazy) {//如果lazy有值的话 tree[lson].lazy += tree[root].lazy;//左孩子lazy 值更新 tree[rson].lazy += tree[root].lazy;//右孩子lazy 值更新 MID; tree[lson].num += (mid - l + 1) * tree[root].lazy;//更新和 tree[rson].num += (r - (mid + 1) + 1) * tree[root].lazy;//更新和 tree[root].lazy = 0;//变为0 }}void updata(int root, int l, int r, int ul, int ur, long long v)//区间ul-ur增加v{ if(ul <= l && r <= ur)//在区间内 { tree[root].lazy += v; tree[root].num += (r - l + 1) * v; return ; } pushdown(root, l, r);//向下更新lazy MID; if(mid >= ul) updata(lson, l, mid, ul, ur, v);//递归 if(mid < ur) updata(rson, mid + 1, r, ul, ur, v);//递归 tree[root] = merge_(tree[lson], tree[rson]);//归并}long long query(int root, int l, int r, int ul, int ur)//求ul-ur的和{ if(ul <= l && r <= ur)//在区间内返回值 { return tree[root].num; } pushdown(root, l, r); MID; long long red = 0; if(mid >= ul) red = query(lson, l, mid, ul, ur);//mid 在 左孩子区间 if(mid < ur) ryou query(rson, mid + 1, r, ul, ur);//mid 在 右孩子区间 return red;}int main(){ int n, m, L, R; long long v; char c; while(~scanf("%d %d", &n, &m)) { build(1, 1, n); while(m--) { scanf("%*c%c", &c); if(c == 'Q') { scanf("%d %d", &L, &R); printf("%lld\n", query(1, 1, n, L, R)); } else { scanf("%d %d %lld", &L, &R, &v); updata(1, 1, n, L, R, v); } } } return 0;}
阅读全文
0 0
- POJ 3468 A Simple Problem with Integers (线段树 区间增减 区间求和)
- poj 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
- POJ 3468 A Simple Problem with Integers(线段树 成段增减,区间求和)
- 【线段树 && 区间增减】POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers (区间增减)
- 【POJ】A Simple Problem with Integers(线段树区间修增减求和)
- POJ 3468——A Simple Problem with Integers(线段树 成端增减,区间求和)
- 区间线段树-poj 3468-A Simple Problem with Integers
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- A Simple Problem with Integers +poj+线段树区间更新
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
- POJ3468 A Simple Problem with Integers 线段树|树状数组BIT(区间增减,求和)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- poj 3468 A Simple Problem with Integers【线段树】区间延迟,区间查询
- jdk 安装和eclipse 安装
- Eclipse对html和js代码自动提示
- Shell 流程控制
- angular2 开发 项目生成详解
- MFC——打开文件对话框
- 【线段树 && 区间增减】POJ 3468 A Simple Problem with Integers
- 平衡树(STL)——Luogu2073 送花
- LogService
- C语言实现List的数据结构(很详细的注释)
- 1:计算机语言的发展史
- Spark中RpcEnv和SparkEnv的区别
- 1009. 说反话 (20) Python 2 编译
- Maven 手动添加 JAR 包到本地仓库
- JSON.parse()与JSON.stringify()的区别