题意:给定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【线段树】的更多相关文章

  1. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

  2. hdu 1698+poj 3468 (线段树 区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...

  3. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

  4. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  5. POJ 3468 (线段树 区间增减) A Simple Problem with Integers

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

  6. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  7. 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 ...

  8. POJ 3468 线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  9. POJ 3468 线段树区间修改查询(Java,c++实现)

    POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...

  10. POJ 3468 线段树+状压

    题意:给你n个数,有对区间的加减操作,问某个区间的和是多少. 思路:状压+线段树(要用lazy标记,否则会TLE) //By SiriusRen #include <cstdio> #in ...

随机推荐

  1. 【GoLang】golang中可以直接返回slice吗?YES

    结论: 可以,slice本质是结构体,返回slice时返回的是结构体的值,结构体的指针.len.cap等信息也全部返回了. 如下: type slice struct { start *uintptr ...

  2. How do I get ASP.NET Web API to return JSON instead of XML using Chrome

    public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Ro ...

  3. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  4. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. Please see the 'svn upgrade' command

    svn: E155036: Please see the 'svn upgrade' command svn: E155036: Working copy '/home/easwy/dev' is t ...

  6. 创建Oracle10g ASM数据库

    概述      在真正的海量数据库环境中,DBA可能会花费很多的时间来作磁盘管理,比如一个表空间将占满整个磁盘,DBA就需要再添加一块磁盘到操作系统中,然后再在新的磁盘上创建新的数据文件,如果是单个磁 ...

  7. android bitmap的放大缩小

    private static Bitmap big(Bitmap bitmap) { Matrix matrix = new Matrix(); matrix.postScale(1.5f,1.5f) ...

  8. Android 实现类似微信客户端朋友圈更新提示的小红点&栏目订阅

    用到的类: com.jauker.widget.BadgeView 实现代码: BadgeView imageBadge = new BadgeView(getContext()); imageBad ...

  9. poj1611(并查集)

    题目链接:http://poj.org/problem?id=1611 题意: SARS(非典型肺炎)传播得非常厉害,其中最有效的办法是隔离那些患病.和患病者接触的人.现在有几个学习小组,每小组有几个 ...

  10. Tomcat在Linux上的安装与配置

    以下使用的Linux版本为: Redhat Enterprise Linux 6.5 x86_64,Tomcat版本为tomcat-7.0.54. 1.下载JDK与Tomcat.    jdk下载地址 ...