poj 3468【线段树】
题意:给定Q(1<=Q<=100000)个数A1,A2…AQ,以及可能多次进行的两个操作
1)对某个区间Ai……Aj的每个数都加n(n可变)
2)对某个区间Ai……Aj的数求和
分析:
树结点只存和,会导致每次加数时都要更新到叶子节点,速度太慢(O(nlog(n))),这是必须避免的
1.在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新nSum(加上本次增量)
再将增量往下传。这样更新的复杂度就是O(log(n))
2.在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到nSum上后将
Inc清0,接下来再往下查询。Inc往下带的过程也是区间分解过程,复杂度也是O(log(n))
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e3 + 10; struct CNode
{
int L, R;
CNode *pLeft, *pRight;
long long nSum;//原来的和
long long Inc;//增量c的累加
}; CNode Tree[200010];//2倍叶子节点数目就够
int nCount = 0;
int Mid(CNode*pRoot)
{
return (pRoot->L + pRoot->R) / 2;
} void BuildTree(CNode *pRoot, int L, int R)
{
pRoot->L = L;
pRoot->R = R;
pRoot->nSum = 0;
pRoot->Inc = 0;
if (L == R)
return;
nCount++;
pRoot->pLeft = Tree + nCount;
nCount++;
pRoot->pRight = Tree + nCount;
BuildTree(pRoot->pLeft, L, (L + R) / 2);
BuildTree(pRoot->pRight, (L + R) / 2 + 1, R);
} void Insert(CNode *pRoot, int i, int v)
{
if (pRoot->L == i&&pRoot->R == i)
{
pRoot->nSum = v;
return;
}
pRoot->nSum += v;//累加和
if (i <= Mid(pRoot))
Insert(pRoot->pLeft, i, v);
else
Insert(pRoot->pRight, i, v); } void Add(CNode * pRoot, int a, int b, long long c)
{
if (pRoot->L == a&&pRoot->R == b)
{
pRoot->Inc += c;
return;
}
pRoot->nSum += c*(b - a + 1);
if (b <= (pRoot->L + pRoot->R) / 2)
Add(pRoot->pLeft, a, b, c);
else if (a >= (pRoot->L + pRoot->R) / 2 + 1)
Add(pRoot->pRight, a, b, c);
else
{
Add(pRoot->pLeft, a, (pRoot->L + pRoot->R) / 2, c);
Add(pRoot->pRight, (pRoot->L + pRoot->R) / 2 + 1, b, c);
} } long long QuerynSum(CNode * pRoot, int a, int b)
{
if (pRoot->L == a&&pRoot->R == b)
return pRoot->nSum + (pRoot->R - pRoot->L + 1)*pRoot->Inc;
pRoot->nSum += (pRoot->R - pRoot->L + 1)*pRoot->Inc;
Add(pRoot->pLeft, pRoot->L, Mid(pRoot), pRoot->Inc);
Add(pRoot->pRight, Mid(pRoot) + 1, pRoot->R, pRoot->Inc);
pRoot->Inc = 0; if (b <= Mid(pRoot))
return QuerynSum(pRoot->pLeft, a, b);
else if (a >= Mid(pRoot) + 1)
return QuerynSum(pRoot->pRight, a, b);
else
{
return QuerynSum(pRoot->pLeft, a, Mid(pRoot)) +
QuerynSum(pRoot->pRight, Mid(pRoot) + 1, b);
}
} int main()
{
int n, q, a, b, c;
char cmd[10];
scanf("%d%d", &n, &q);
int i, j, k;
nCount = 0;
BuildTree(Tree, 1, n);
for (i = 1; i <= n;i++)
{
scanf("%d", &a);
Insert(Tree, i, a);
}
for (i = 0; i < q; i++)
{
scanf("%s", cmd);
if (cmd[0]=='C')
{
scanf("%d%d%d", &a, &b, &c);
Add(Tree, a, b, c);
}
else
{
scanf("%d%d", &a, &b);
printf("%I64d\n", QuerynSum(Tree, a, b));
}
}
return 0;
}
poj 3468【线段树】的更多相关文章
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- POJ 3468 线段树裸题
这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...
- poj 3468 线段树区间更新/查询
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
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- POJ 3468 线段树+状压
题意:给你n个数,有对区间的加减操作,问某个区间的和是多少. 思路:状压+线段树(要用lazy标记,否则会TLE) //By SiriusRen #include <cstdio> #in ...
随机推荐
- Redis系列-配置文件小结
如果不指定配置文件,Redis也可以启动,此时,redis使用默认的内置配置.不过在正式环境,常常通过配置文件[通常叫redis.conf]来配置redis. redis.conf配置格式如下: ke ...
- We will be discontinuing the Nitrous Development Platform and Cloud IDE on November 14th, 2016.
我表示我很难过 Nitrous We will be discontinuing the Nitrous Development Platform and Cloud IDE on November ...
- SharePoint更改密码
stsadm –o updatefarmcredentials –userlogin DomainName\UserName -password NewPassword –local 1. 通过管理 ...
- default constructor,copy constructor,copy assignment
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- NGUI Table页(UIToggle和UIToggledObjects)
1.添加两个按钮Btn1和Btn2.添加2个Spr1和Spr2 2.给每个Btn添加两个脚本UIToggle和UIToggledObjects 3.将每个UIToggle的Group设置个非0的值 4 ...
- progressBar走马灯设置
初始值Visible = false; 让progressBar1出现时: progressBar1.Visible = true; progressBar1.Style = ProgressBarS ...
- centos6.5kvm虚拟化安装部署
一.走进云计算 云计算:云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问, 进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供, ...
- JS 循环练习
规律 大范围套小范围 循环 分支语句 switch case 嵌套 死循环 while(true) 打破循环 break continue while(true) ...
- ubunto安装pycharm
转载:http://www.cnblogs.com/zhcncn/p/4027025.html 1. 下载 http://www.jetbrains.com/pycharm/download/ 选择L ...
- ArtDialog简单使用示例
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...