题目连接:

  http://poj.org/problem?id=3468

题目大意:

  给出n个数,有两种操作:

    1:"C a b c",[a,b]中的每一个数都加上c。

    2:"Q a b",求[a,b]中每个数相加的和。

解题思路:

  线段树更新到每一个节点的话,由于节点数目和查询次数原因会tle,所以在每一个节点内定义一个标志变量表示当前节点的下一层为更新,每次查询时候有需要的话在更新到下一层。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
#define LL __int64
struct node
{
LL L, R;
LL sum, add;
LL Mid()
{
return (L + R) / ;
}
};
node tree[maxn];
LL res; void build(LL root, LL l, LL r)
{
tree[root].L = l;
tree[root].R = r;
tree[root].sum = tree[root].add = ; if (l == r)
return ;
build (*root+, l, tree[root].Mid());
build (*root+, tree[root].Mid()+, r);
}
void insert (LL root, LL s, LL e, LL x)
{
tree[root].sum += x * (e - s + );
if (tree[root].L == s && e == tree[root].R)//更新到区间
{
tree[root].add += x;
return ;
}
if (e <= tree[root].Mid())
insert (*root+, s, e, x);
else if (tree[root].Mid() < s)
insert (*root+, s, e, x);
else
{
insert (*root+, s, tree[root].Mid(), x);
insert (*root+, tree[root].Mid()+, e, x);
}
}
void query (LL root, LL s, LL e)
{ if (tree[root].L == s && e == tree[root].R)
{
res += tree[root].sum;
return ;
}
if (tree[root].add)
{//向下继续更新
tree[*root+].add += tree[root].add;
tree[*root+].add += tree[root].add;
tree[*root+].sum += tree[root].add * (tree[*root+].R - tree[*root+].L + );
tree[*root+].sum += tree[root].add * (tree[*root+].R - tree[*root+].L + );
tree[root].add = ;
}
if (e <= tree[root].Mid())
query (*root+, s, e);
else if (tree[root].Mid() < s)
query (*root+, s, e);
else
{
query (*root+, s, tree[root].Mid());
query (*root+, tree[root].Mid()+, e);
}
}
int main ()
{
LL n, m, num;
while (scanf ("%I64d %I64d", &n, &m) != EOF)
{
build (, , n);
for (int i=; i<=n; i++)
{
scanf ("%I64d", &num);
insert (, i, i, num);
}
char str[];
LL s, e;
while (m --)
{
scanf ("%s %I64d %I64d", str, &s, &e);
if (str[] == 'Q')
{
res = ;
query (, s, e);
printf ("%I64d\n", res);
}
else
{
scanf ("%I64d", &num);
insert (, s, e, num);
}
}
}
return ;
}

暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  2. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  3. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  4. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  5. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  6. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  7. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

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

  8. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. eclipse提速03 - 禁用动画

  2. Word 2013安裝字典

    不必從內建的字典中開始,Word 2013 可將您連結到 Office 市集,方便您挑選免費的字典,或從包括多語字典的字典集合中購買. 若要選擇並安裝您想要的字典,請以滑鼠右鍵按一下任何單字,並按一下 ...

  3. HDU 5301 Buildings(2015多校第二场)

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  4. D广搜

    <span style="color:#330099;">/* D - 广搜 基础 Time Limit:1000MS Memory Limit:30000KB 64b ...

  5. Codeforces Round #221 (Div. 2) D

    有点郁闷的题目,给了2000ms,可是n,m的范围已经是5000了.5000 * 5000一般在别的OJ已经是超了2000ms,一開始不敢敲.看了下别人有n*m的潜逃循环,原来CF的机子如此的强大,一 ...

  6. C语言-- static 全局使用示例

    C语言-- static 全局使用示例  前言:看到很多使用Objective-C开发IOS的大牛,有时候会使用static全局变量,相比之下,我却很少用这个,从而很少对其有着比较有实质意义的理解,甚 ...

  7. c++学习笔记之基础---类内声明线程函数的调用

    近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数exec_task中调用pthread_create去启动线程执行例程thread_rounter.编译之后报错如下: spf ...

  8. 列表和元组的基本操作,for遍历,range

    1,list(增删改查):列表可以装大量数据,不限制数据的类型(int,str,bool, list,tuple,dict,set),表示方法用[  ],list和sttr有区别,list可以直接在原 ...

  9. spring的PROPAGATION_REQUIRES_NEW事务,下列说法正确的是(D)

    A:内部事务回滚会导致外部事务回滚 B:内部事务回滚了,外部事务仍可以提交 C:外部事务回滚了,内部事务也跟着回滚 D:外部事务回滚了,内部事务仍可以提交 PROPAGATION_REQUIRES_N ...

  10. command 'gcc' failed with exit status 1

    https://stackoverflow.com/questions/11094718/error-command-gcc-failed-with-exit-status-1-while-insta ...