1、给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

2、线段树单点更新太费时,所以使用区间更新

3、

#include <cstdio>

#define L(root) ((root) << 1)
#define R(root) (((root) << 1) + 1) const int MAXN = ;
int numbers[MAXN]; struct st
{
// 区间范围
int left, right;
// 更新值、区间总和
long long delta, sum;
} st[MAXN * ]; // 建树代码基本不变
void build(int root, int l, int r)
{
st[root].left = l, st[root].right = r, st[root].delta = ;
if (l == r)
{
st[root].sum = numbers[l];
return;
} int m = l + ((r - l) >> );
build(L(root), l, m);
build(R(root), m + , r);
st[root].sum = st[L(root)].sum + st[R(root)].sum;
} long long query(int root, int l, int r)
{
// 如查询区间恰等于节点区间,直接返回该区间总和即可
if (st[root].left == l && st[root].right == r)
{
return st[root].sum;
} // 否则需将当前区间的“缓冲”值更新下去并修正各节点区间的总和
if (st[root].delta)
{
st[L(root)].delta += st[root].delta;
st[R(root)].delta += st[root].delta;
st[L(root)].sum += st[root].delta * (st[L(root)].right - st[L(root)].left + );
st[R(root)].sum += st[root].delta * (st[R(root)].right - st[R(root)].left + );
st[root].delta = ;
} int m = st[root].left + ((st[root].right - st[root].left) >> );
if (r <= m)
{
return query(L(root), l, r);
}
else if (l > m)
{
return query(R(root), l, r);
}
else
{
return query(L(root), l, m) + query(R(root), m + , r);
}
} void update(int root, int l, int r, long long v)
{
// 如变更区间恰等于节点区间,只修正当前节点区间即可
if (st[root].left == l && st[root].right == r)
{
st[root].delta += v;
st[root].sum += v * (r - l + );
return;
} // 否则需向下修正相关节点区间
if (st[root].delta)
{
st[L(root)].delta += st[root].delta;
st[R(root)].delta += st[root].delta;
st[L(root)].sum += st[root].delta * (st[L(root)].right - st[L(root)].left + );
st[R(root)].sum += st[root].delta * (st[R(root)].right - st[R(root)].left + );
st[root].delta = ;
} int m = st[root].left + ((st[root].right - st[root].left) >> );
if (r <= m)
{
update(L(root), l, r, v);
}
else if (l > m)
{
update(R(root), l, r, v);
}
else
{
update(L(root), l, m, v);
update(R(root), m + , r, v);
}
// 同时一定要记得修正当前节点区间的总和
st[root].sum = st[L(root)].sum + st[R(root)].sum;
} int main()
{
int N, Q;
while (scanf("%d%d", &N, &Q) != EOF)
{
for (int i = ; i <= N; ++i)
{
scanf("%d", &numbers[i]);
} build(, , N); char cmd;
int l, r;
long long v;
while (Q--)
{
scanf(" %c", &cmd);
scanf("%d%d", &l, &r);
switch (cmd)
{
case 'Q':
printf("%lld\n", query(, l, r));
break; case 'C':
scanf("%lld", &v);
if (v)
{
update(, l, r, v);
}
break;
}
}
} 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 , 线段树+区间更新。

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  10. 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. python出现'module' object is not callable错误

  2. CDOJ 1218 Pick The Sticks

    Pick The Sticks Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  3. windows下安装使用WGET

    windows下安装WGET   1. 安装wget    www.2cto.com   网址:http://gnuwin32.sourceforge.net/packages/wget.htm 下载 ...

  4. web应用启动的时候SpringMVC容器加载过程

    <!-- 配置DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> ...

  5. IO与文件读写---Java的IO流架构

    http://www.blogjava.net/pengpenglin/archive/2010/03/03/314239.html#314399 http://www.blogjava.net/jo ...

  6. 打开input输入的时候,css中position:absolute/fixed定位的时候,定位元素上移问题解决

    1.异常代码 <style> .box{ min-height: 100vh; width: 100%; position: relative; } .position{ position ...

  7. 我在使用eclipse配置Tomcat服务器的时候发现,默认情况下Tocmat把我们部署的项目放在了workspaces下面,而不是像Myeclipse默认的那样放在tomcat的安装路径下。

    1.我在使用eclipse配置Tomcat服务器的时候发现,默认情况下Tocmat把我们部署的项目放在了workspaces下面,而不是像Myeclipse默认的那样放在tomcat的安装路径下. 2 ...

  8. Desert King (poj 2728 最优比率生成树 0-1分数规划)

    Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   A ...

  9. Could not find leader nimbus

    运行storm ui, 然后访问storm ui 的网页的时候,死活跑不起来.后面,根据下面这篇文章的说法, 停止zookeeper 之后,删掉zookeeper 上面的storm 节点, 然后再重启 ...

  10. Unity 3D 中动态字体的创建

    原创不易,转载请注明转自: http://blog.csdn.net/u012413679/article/details/26232453 ---- kosion 1.载入NGUI插件包,载入完毕后 ...