POJ(3468)
A Simple Problem with Integers
http://poj.org/problem?id=3468
#include <stdio.h>
struct node
{
int l,r;
__int64 sum;
__int64 add;
}tree[*];
void build(int left,int right,int root)
{
int mid=(left+right)/;
if(left==right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
scanf("%I64d",&tree[root].sum);
return ;
}
tree[root].l=left;
tree[root].r=right;
tree[root].add=;
build(left,mid,*root);
build(mid+,right,*root+);
tree[root].sum=tree[*root].sum+tree[*root+].sum;
}
void modify(int left,int right,__int64 add,int root)
{
int mid=(tree[root].l+tree[root].r)/;
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].sum+=add*(tree[root].r-tree[root].l+);
tree[root].add+=add;
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(right<=mid)
modify(left,right,add,*root);
else if(left>=mid+)
modify(left,right,add,*root+);
else
{
modify(left,mid,add,*root);
modify(mid+,right,add,*root+);
}
tree[root].sum=tree[*root].sum+tree[*root+].sum;
}
__int64 search(int left,int right,int root)
{
int mid=(tree[root].l+tree[root].r)/;
__int64 sum;
if(left==tree[root].l&&right==tree[root].r)
return sum=tree[root].sum;
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(right<=mid)
return sum=search(left,right,*root);
else if(left>=mid+)
return sum=search(left,right,*root+);
else
return sum=search(left,mid,*root)+search(mid+,right,*root+);
}
int main()
{
int n,q,i;
char str[];
int a,b;
__int64 c;
scanf("%d%d",&n,&q);
build(,n,);
for(i=;i<=q;i++)
{
scanf("%s%d%d",str,&a,&b);
if(str[]=='Q')
{
printf("%I64d\n",search(a,b,));
}
else
{
scanf("%I64d",&c);
modify(a,b,c,);
}
}
return ;
}
POJ(3468)的更多相关文章
- 区间dp模型之括号匹配打印路径 poj(1141)
题目链接:Brackets Sequence 题目描写叙述:给出一串由'(')'' [ ' ' ] '组成的串,让你输出加入最少括号之后使得括号匹配的串. 分析:是区间dp的经典模型括号匹配.解说:h ...
- POJ(2187)用凸包求最远点对
Beauty Contest http://poj.org/problem?id=2187 题目描述:输入n对整数点,求最距离远的点对,输出他们距离的平方和 算法:拿到这个题,最朴素的想法就是用2层循 ...
- POJ(1195)(单点修改,区间查询)(二维)
题目大意 给定一个N*N的网格,刚开始每个网格的值都是0,接下来会对这些网格进行操作,有一下两种操作: 1."X Y A"对网格C[x][y]增加A 2."L B R T ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- 算法手记 之 数据结构(堆)(POJ 2051)
一篇读书笔记 书籍简评:<ACM/ICPC 算法训练教程>这本书是余立功主编的,代码来自南京理工大学ACM集训队代码库,所以小编看过之后发现确实很实用,适合集训的时候刷题啊~~,当时是听了 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
随机推荐
- 修改FastColoredTextBox控件完成选择
//判断是否是中文 public bool IsChina(char c) { bool BoolValue = false; if (Convert ...
- ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...
- iOS红马甲项目Bug总结(3)
这里是一些小总结 1.使用图片缓存之后,新添加的图像一直不能显示 2.项目打包通过appliction loader上传成功了,可是itunes 上面的构建版本项,一直没显示出来 3.界面加载之后,t ...
- linux C之access函数(转-追梦的小鸟)
access():判断是否具有存取文件的权限 相关函数 stat,open,chmod,chown,setuid,setgid表头文件 #include<unistd.h>定义 ...
- gdb使用心得
启用调试gdb gdb 路径到prog_1dray 然后就直接进去gdb了 进去后输入r *.par 参数文件就行了 暂时学到这,用到这!日后再学习更新
- 20160113 JS中CheckBox如何控制全选
//判断CheckBoxzong他的点击事件 $("#CheckBoxzong").click(function () { //取出所有class为选中的集合 var query ...
- spring 3 mvc hello world + mavern +jetty
Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutori ...
- python---Memcached
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- PHP 开发的 API 多版本管理实践
遇到的情况 本文针对移动互联网客户端需要兼容旧版的情况,强制升级到最新版本的 app 不在讨论之列. 在 bugtags.com 项目中,我们的版本遵循下面规范.1.0.1大功能.小更新.bug 修正 ...
- winXP下安装opensshd服务
1)参考如下帖子:http://blog.chinaunix.net/uid-23187544-id-2527564.html http://wuhuizhong.iteye.com/blog/122 ...