题目:

id=3468" target="_blank">poj 3468 A Simple Problem with Integers

题意:给出n个数。两种操作

1:l -- r 上的全部值加一个值val

2:求l---r 区间上的和

分析:线段树成段更新,成段求和

树中的每一个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每一个值要加的值

对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val 。

以下的区间标记num += val

对于求和操作。每次进行延迟更新。把num值分别更新到两个子区间。sum值更新,然后num值变为0

注意:这个题目会超int 。

所以

AC代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
const long long N = 110000;
const long long inf = 0x3f3f3f3f;
struct Node
{
long long l,r;
long long num,sum;
};
Node tree[5*N];
long long a[N];
long long cnt ;
void build(long long o,long long l,long long r)
{
tree[o].l = l,tree[o].r = r;
tree[o].num = 0;
if(l==r)
{
tree[o].sum = a[cnt++];
return ;
}
long long mid = (l+r)/2;
build(o+o,l,mid);
build(o+o+1,mid+1,r);
tree[o].sum = tree[o+o].sum + tree[o+o+1].sum;
}
void push_update(long long o)
{
if(tree[o].num!=0)
{
tree[o].sum += tree[o].num * (tree[o].r-tree[o].l+1);
tree[o+o+1].num += tree[o].num;
tree[o+o].num += tree[o].num;
tree[o].num = 0;
}
}
void update(long long o,long long l,long long r,long long val)
{
if(l==tree[o].l && r == tree[o].r)
{
tree[o].num += val;
return ;
}
tree[o].sum += (val*(r-l+1)); //维护前面的
long long mid = (tree[o].l+tree[o].r) / 2;
if(r<=mid)
update(o+o,l,r,val);
else if(l>mid)
update(o+o+1,l,r,val);
else
{
update(o+o,l,mid,val);
update(o+o+1,mid+1,r,val);
}
}
long long query(long long o,long long l,long long r)
{
if(tree[o].l==l && tree[o].r == r)
{
return tree[o].sum+(r-l+1)*tree[o].num;
}
push_update(o); //维护后面的
long long mid = (tree[o].l+tree[o].r)/2;
if(r<=mid)
return query(o+o,l,r);
else if(l>mid)
return query(o+o+1,l,r);
else
return query(o+o,l,mid) + query(o+o+1,mid+1,r);
}
int main()
{
//freopen("Input.txt","r",stdin);
long long n,m;
while(~scanf("%lld%lld",&n,&m))
{
cnt = 0;
for(long long i=0;i<n;i++)
scanf("%lld",&a[i]);
build(1,1,n);
while(m--)
{
getchar();
char c;
long long x,y;
scanf("%c",&c);
scanf("%lld%lld",&x,&y);
//printf("*****************************************\n");
if(c=='Q')
{
printf("%lld\n",query(1,x,y));
}
else
{
long long val;
scanf("%lld",&val);
update(1,x,y,val);
}
}
}
return 0;
}

poj 3468 A Simple Problem with Integers 【线段树-成段更新】的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  3. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

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

  4. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

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

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

  7. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

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

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

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

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

  10. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

随机推荐

  1. HDU 6336 子矩阵求和

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  2. CF987C Three displays【一维DP/类似最大子序列和】

    [链接]:CF987C [分析]:先求出每个s[i]后面比s[i]大的c[i]的最小值,然后枚举前两个数c(i),c(j)以及 j 后面递增且存在最小值的dp(j) [代码]: #include< ...

  3. 2017 CCPC 湘潭邀请赛

    都tm快一年了我还没补这套题……再不补怕是要留给退役后乐 Problem A 把$n * (n + 1)$的矩阵补成$(n + 1) * (n + 1)$的,然后高斯消元. Problem B 一看题 ...

  4. 洛谷 P2183 [国家集训队]礼物

    题目描述 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店中购买了n件礼物 ...

  5. 八. 输入输出(IO)操作5.面向字节流的应用

    文件输入输出流 文件输入输出流 FileInputStream 和 FileOutputStream 负责完成对本地磁盘文件的顺序输入输出操作. [例 10-5]通过程序创建一个文件,从键盘输入字符, ...

  6. LAMP----linux+apache+mysql+php详细安装步骤之一APACHE篇(openldap等)

    LAMP----linux+apache+mysql+php详细安装步骤之一APACHE篇(openldap等) linux详细版本为RHEL5.3 [root@localhost mail]# un ...

  7. SQL使用链接服务器执行远程数据库上的存储过程

    原文:SQL使用链接服务器执行远程数据库上的存储过程 --创建链接服务器 exec sp_addlinkedserver'server_tmp','','SQLOLEDB','远程服务器名或ip地址' ...

  8. UBIFS 术语

    B+ tree: base head: budgeting: 空闲空间评估 bud: 一个日志使用的eraseblock cnode: commit: 更新index到flash上的过程 commit ...

  9. lock参数变化吗

    多线程应用中经常使用lock,在使用这个关键字的时候,经常有个疑问,如果更改了当时的入参,那么是否会变化呢,下面通过代码实例测试一把 class Program { static void Main( ...

  10. SilverLight-3:目录

    ylbtech-SilverLight-Index: 1.A,返回顶部 Layout The Layout Containers - The Panel Background Borders   Si ...