PKU 3468 A Simple Problem with Integers
题目大意:
有N,M两个数
Q 表示查询, 后面两个数a b,表示查询从a 到b计算它们的和
C 表示增加 后面三个数a,b,c 表示从a开始,一直到b,每个数都增加c
除了查询要进行输出,增加不要输出
#include<iostream>
using namespace std;
#define LL(x)((x)<<1)
#define RR(x)((x)<<1|1)
struct_Seg_Tree{
int left,right;
long long sum;
int add;
int calmid(){
return (left+right)/2;
}
int caldis(){
return right-left+1;
}
}tt[1000000];
int val[1000002];
long long build(int l,int r,int idx){
tt[idx].left=l;
tt[idx].right=r;
tt[idx].add=0;
if(l==r){
return tt[idx].sum=val[l];
}
return tt[idx].sum=build(l,mid,LL(idx)+build(mid+1,r,RR(idx)));
}
void update(int l,int r,int add,int idx){
if(l<=tt[idx].left&&r>=tt[idx].right){
tt[idx].add+=add;
tt[idx].sum+=(long long)tt[idx].caldis()*add;//通过乘以后来添加的数
return ;
}
if(tt[idx].add){
tt[LL(idx).sum]+=(long long)tt[LL(idx)].caldis()*tt[idx].add;
tt[RR(idx).sum]++(long long)tt[RR(idx)].caldis()*tt[idx].add;
tt[LL(idx)].add+=tt[idx].add;
tt[RR(idx)].add+=tt[idx].add;
tt[idx].add=0;
}
int mid=tt[idx].calmid();
if(l<=mid) update(l,r,add,LL(idx));
if(mid<r) update(l,r,add,RR(idx));
tt[idx].sum = tt[LL(idx)].sum + tt[RR(idx)].sum;
}
long long query(int l,int r,int idx){
if(l==tt[idx].left&&r==tt[idx].right){
return tt[idx].sum;
}
if(tt[idx].add){
tt[LL(idx)].sum += (long long )tt[LL(idx)].caldis() * tt[idx].add;
tt[RR(idx)].sum += (long long)tt[RR(idx)].caldis() * tt[idx].add;
tt[LL(idx)].add += tt[idx].add;
tt[RR(idx)].add += tt[idx].add;
tt[idx].add = 0;
}
int mid=tt[idx].calmid();
if(r<=mid){
return query(l,r,LL(idx));
} else if(mid<1){
return query(l,r,RR(idx));
} else{
return query(l,mid,LL(idx)+query(mid+1,r,RR(idx));
}
}
int main(){
int n,m;
while(scanf("%d %d",&n,&m)==2){
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
build(1,n,1);
while(m--){
char com[2];
scanf("%s",com);
if(com[0]=='Q'){
int a,b;
scanf("%d %d",&a,&b);
pirntf("lld\n",query(a,b,1));
}else{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
update(a,b,c,1);
}
}
}
}
PKU 3468 A Simple Problem with Integers的更多相关文章
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- 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 ...
- 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 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- Python中and(逻辑与)计算法则
在程序设计中,and称为逻辑与运算,也称布尔运算:1.and是在布尔上下文中从左到右计算表达式的值:2.0.''.[].().{}.None.False在布尔上下文中为假:其它任何东西都为真:3.如果 ...
- JavaScript 对象JavaScript 对象
JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... 此外,JavaScript 允许自定义对象. 所有事物都是对象 JavaScript 提供多个内建对象,比如 String. ...
- [Matlab+C/C++] 读写二进制文件
introduction 因为Matlab操作简单.方便,它被应用于很多领域:音频处理,图像处理,数值计算等.尽管MATLAB容易操作,但受限于他的语言解释机制,MATLAB的执行速度通常较低.C/C ...
- gdb不知为何显示2次析构
gdb不知为何显示2次析构 (金庆的专栏 2016.11) gdb 显示2次 A::~A(): (gdb) bt #0 A::~A (this=0x602010, __in_chrg=<opti ...
- 在Spring Boot中输出REST资源
前面我们我们已经看了Spring Boot中的很多知识点了,也见识到Spring Boot带给我们的各种便利了,今天我们来看看针对在Spring Boot中输出REST资源这一需求,Spring Bo ...
- HDFS基本原理及数据存取实战
---------------------------------------------------------------------------------------------------- ...
- 通过JNDI从服务器容器中获取资源_Spring JNDI+Mysql+Tomcat
通过JNDI从服务器容器中获取DataSource资源 (由容器管理,不要关闭它,容器自己会处理)上一篇我们使用的是dbcp,这里使用JNDI: 使用JNDI连接数据: 在Spring可以注释 < ...
- 20 ViewPager Demo4自动轮播
MainActivity.java 思想:才用非常大的数 让其看起来可以循环轮播图片并且用户可以从尽头滑到首图的特点 . package com.qf.day20_viewpager_demo4; i ...
- Swift变量名的一种玩法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 是的,Swift的变量名可以用任何合法的Unicode字符,这 ...
- Xcode中的变量模板(variable template)的用法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 你可能经常会写一些小的代码片段,里面自然少不了一些关键的变量. ...