题目链接:http://poj.org/problem?id=3468 http://poj.org/problem?id=3468 http://poj.org/problem?id=3468

思路:这是一个区间改动区间查询的题,因为题目中的给的数据比較大,那么用单个改动和查询肯定不行,所以。

。。。注意数据可能比較大,应该用__int64或long long存数据。

。。

code:

#include<stdio.h>
#include<math.h>
#define L(u) (u<<1)
#define R(u) (u<<1|1) const int M=100010; struct Node
{
__int64 l,r;
__int64 add;
long long sum;
}node[M*4]; __int64 a[M]; void pushup(__int64 u)
{
node[u].sum=node[L(u)].sum+node[R(u)].sum;
return ;
} void pushdown(__int64 u)
{
node[L(u)].add+=node[u].add;
node[L(u)].sum+=(node[L(u)].r-node[L(u)].l+1)*node[u].add;
node[R(u)].add+=node[u].add;
node[R(u)].sum+=(node[R(u)].r-node[R(u)].l+1)*node[u].add;
node[u].add=0;
} void build(__int64 u,__int64 left,__int64 right)
{
node[u].l=left;
node[u].r=right;
node[u].add=0;
if(left==right)
{
node[u].sum=a[left];
return ;
}
__int64 mid=(node[u].l+node[u].r)/2;
build(L(u),left,mid);
build(R(u),mid+1,right);
pushup(u); } void update(__int64 u,__int64 left,__int64 right,__int64 v)
{
if(left<=node[u].l&&node[u].r<=right)
{
node[u].add+=v;
node[u].sum+=(node[u].r-node[u].l+1)*v;
return ;
}
//node[u].sum+=(right-left+1)*v; //当前节点表示的区间不是查询区间的子区间
if(node[u].add) pushdown(u); //分析当前节点懒惰标记是否为0,不为0则要给他的子节点更新数据
__int64 mid=(node[u].l+node[u].r)/2;
if(right<=mid) update(L(u),left,right,v);
else if(left>mid) update(R(u),left,right,v);
else
{
update(L(u),left,mid,v);
update(R(u),mid+1,right,v);
}
node[u].sum=node[L(u)].sum+node[R(u)].sum;
} __int64 query(__int64 u,__int64 left,__int64 right)
{
if(left<=node[u].l&&node[u].r<=right)
{
return node[u].sum;
}
if(node[u].add) pushdown(u); //分析当前节点懒惰标记是否为0,不为0则要给他的子节点更新数据
__int64 mid=(node[u].l+node[u].r)/2;
if(right<=mid) return query(L(u),left,right);
else if(left>mid) return query(R(u),left,right);
else
{
return (query(L(u),left,mid)+query(R(u),mid+1,right));
}
} int main()
{
__int64 n,m,i,x,y,z;
while(scanf("%I64d%I64d",&n,&m)==2)
{
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
build(1,1,n);
char str[5];
for(i=0;i<m;i++)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%I64d%I64d%I64d",&x,&y,&z);
update(1,x,y,z);
}
else
{
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",query(1,x,y));
}
}
}
return 0;
}

poj 3466 A Simple Problem with Integers的更多相关文章

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

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

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

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

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

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

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

  8. [ACM] poj 3468 A Simple Problem with Integers(段树,为段更新,懒惰的标志)

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

  9. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

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

随机推荐

  1. IT第二天 - JAVA环境的配置、Hello的编写

    IT第二天 上午 HTML的一些讲解 下午 JDK的安配置 JAVA语法的注意事项 Hello的编写 晚上 作业 对println的应用 笔记 1.Classpath环境变量的配置:因为DOS对于文件 ...

  2. POJ 1741 Tree【Tree,点分治】

    树上的算法真的很有意思……哈哈. 给一棵边带权树,问两点之间的距离小于等于K的点对有多少个. 将无根树转化成有根树进行观察.满足条件的点对有两种情况:两个点的路径横跨树根,两个点位于同一颗子树中. 如 ...

  3. POJ输出状态的逻辑。

    实測POJ应该是採取一个一个点測.哪个点fail了就输出哪个点的状态,但接下来的点貌似还是要測. 測试方法,1000先測出有6个測点1,2,3,4,6.15,然后交了下面代码. #include &l ...

  4. splice()函数的使用方法

    splice()函数的使用方法,这是一个拗口的函数.用起来有点麻烦.图3所看到的是splice函数的功能.将一个列表插入到还有一个列表其中.list容器类定义了splice()函数的3个版本号: sp ...

  5. 在Update Panel 控件里面添加 File Upload 控件 上传文件

    Detail Information:http://www.codeproject.com/Articles/482800/FileplusUploadplusinplusUpdateplusPane ...

  6. UIApplication相关

    1,显示应用图标右上角的红色提示 application.applicationIconBadgeNumber = 10; 2.修改状态栏的类型 在当前控制器下设置 - (UIStatusBarSty ...

  7. myeclipse8.5如何注册,转自他出

    Step: 1.建立一个任意名称的Java Project 2.在该工程中建立一个名文MyEclipseGen的Java文件(MyEclipseGen.java) 3.运行下面的代码,会在控制台出现& ...

  8. USACO The Tamworth Two 模拟

    一道模拟题不过要担心的是牛或者人在转弯的时候,另一方如果能走,那么要走,不能停留. 还是蛮简单的. 调试输出的话可以看到具体追击过程 Source Code: /* ID: wushuai2 PROG ...

  9. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  10. qt5_qml_Opengl_shader 第一弹----------------------openglunderqml的简化及介绍

    最近得知opengl可以通过纹理贴图来渲染yuv的数据,这就免去了yuv-rgb,这个过程在arm上还是很耗时间的,于是就接触了opengl. 写这篇文章的目的是方便初学者使用qml来调用opengl ...