(线段树模板)A Simple Problem with Integers --POJ--3468
链接:
http://poj.org/problem?id=3468
代码:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = 1e5+; struct SegmentTree
{
int L, R;
long long sum, e;
int Mid()
{
return (L+R)>>;
}
int len()
{
return R-L+;
}
}a[N<<]; void BuildSegTree(int r, int L, int R)
{
a[r].L=L, a[r].R=R;
a[r].e=; if(L==R)
{
scanf("%lld", &a[r].sum);
return ;
} BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R); a[r].sum = a[Lson].sum + a[Rson].sum;
}
void PushDown(int r)
{
a[Lson].sum += a[r].e*a[Lson].len();
a[Lson].e += a[r].e;
a[Rson].sum += a[r].e*a[Rson].len();
a[Rson].e += a[r].e; a[r].e=;
}
void Update(int r, int L, int R, int e)
{
a[r].sum += (R-L+)*e; if(a[r].L==L && a[r].R==R)
{
a[r].e += e;
return ;
} PushDown(r); if(R<=a[r].Mid())
Update(Lson, L, R, e);
else if(L>a[r].Mid())
Update(Rson, L, R, e);
else
{
Update(Lson, L, a[r].Mid(), e);
Update(Rson, a[r].Mid()+, R, e);
}
}
long long Query(int r, int L, int R)
{
if(a[r].L==L && a[r].R==R)
return a[r].sum; PushDown(r); if(R<=a[r].Mid())
return Query(Lson, L, R);
else if(L > a[r].Mid())
return Query(Rson, L, R);
else
{
long long Lsum = Query(Lson, L, a[r].Mid());
long long Rsum = Query(Rson, a[r].Mid()+, R); return Lsum+Rsum;
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
BuildSegTree(, , n); while(m--)
{
char s[];
int L, R, e; scanf("%s", s); if(s[]=='Q')
{
scanf("%d%d", &L, &R);
printf("%lld\n", Query(, L, R));
}
else
{
scanf("%d%d%d", &L, &R, &e);
Update(, L, R, e);
}
}
}
return ;
}
(线段树模板)A Simple Problem with Integers --POJ--3468的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers~POJ - 3468
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
- 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers
https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
随机推荐
- avalon的常见问题
随着avalon的普及,越来越多人加入avalon的大家庭.随之而来的是各种问题.本文在这里统一解答一下. 使用avalon基本上有几个方针要坚持 数据必须先定义后使用,只能VM中定义,不能V中定义. ...
- div 自动全屏高度
最近做一个页面,需要一个div自动铺满全屏,但是高度总是难以搞定.查资料为:需要从html body到div 需要 设置 高度属性 为100%
- JAVA动态性之一一反射机制reflection
package com.bjsxt.reflection.test.bean; public class User { private int id; private int age; private ...
- Ansible test
[root@localmesos ansible_test]# ansible all -a "/bin/echo hello"192.168.111.111 | SUCCESS ...
- InnoDB FULLTEXT
1.概要 InnoDB引擎对FULLTEXT索引的支持是MySQL5.6新引入的特性,之前只有MyISAM引擎支持FULLTEXT索引.对于FULLTEXT索引的内容可以使用MATCH()…AGAIN ...
- Oracle to_date()函数的用法《转载》
to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明, 原文地址:http://database.51cto.com/art ...
- Error running : Address localhost:1099 is already in use
运行报错: Error running : Address localhost:1099 is already in use 解决方法: 打开任务管理器,将后台的java.exe进程都关掉,再次运行 ...
- phpStudy1——PHP文件获取html提交的参数
示例代码: submit.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- pdf转word在线转换器
昨天大学辅导员联系我让我pdf转word,一番搜索终于找到一款免费好用的在线转换器,亲测好使免费
- 数字三角形 · Triangle
从上到下用DP. [抄题]: 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 比如,给出下列数字三角形: [ [2], [3,4], [6,5,7], [4, ...