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 ...
随机推荐
- fatal error C1083: Cannot open precompiled header file: 'Debug/xxoo.pch': No such file or directory
fatal error C1083: Cannot open precompiled header file: 'Debug/xxoo.pch': No such file or directory ...
- Dynamics CRM 不同的站点地图下设置默认不同的仪表板
CRM的默认仪表板只能设置一个,也就是说每个引用仪表板的站点地图下点开仪表板后都是看到的默认仪表板,例如我下图中的"日常维修仪表板" 那如果我要在不同的站点地图下看到的默认仪表板不 ...
- How to Collect Bne Log Files for GL Integrators
In this Document Goal Solution APPLIES TO: Oracle General Ledger - Version 11.0 and laterInforma ...
- bitbucket添加ssh key
右上角头像->bitbucket settings->ssh keys 直接点击: https://bitbucket.org/account/user/.../ssh-keys/ 教程: ...
- 详解EBS接口开发之库存事务处理-物料批次导入
库存事务处理-物料批次导入 --系统批次表 SELECT * FROM MTL_LOT_NUMBERS T; --API创建批次 inv_lot_api_pub.create_inv_lot(x_re ...
- SKSpriteNode对象初始化在iPhone 6 plus中显示不正确的分析及解决
一个SpriteKit项目在其他设备上运行都无问题(无论是真机或是模拟器),但是在iPhone6 Plus上会出现精灵对象纹理被过度放大的现象: 从上图中大家可以看到无论是主角或是道具球都过大了. 看 ...
- Java多线程的调度策略
在Java多线程环境中,为保证所有线程的执行能按照一定的规则执行,JVM实现了一个线程调度器,它定义了线程调度的策略,对于CPU运算的分配都进行了规定,按照这些特定的机制为多个线程分配CPU的使用权. ...
- HDFS的写数据过程分析
HDFS的写数据过程分析 我们通过FileSystem类可以操控HDFS, 那我们就从这里开始分析写数据到HDFS的过程. 在我们向 HDFS 写文件的时候,调用的是 FileSystem.creat ...
- 使用std::vector优化点云动画显示一例
1. 准备 使用std::vector应该知道几点: (1)内存连续的容器,有点像数组 (2)与std::list相比,插入和删除元素比较慢- 因为数据迁移 (3)添加元素可能会引发内存分配和数据迁移 ...
- JDBC-数据库的编程(一)
因为我使用的mysql数据库客户端程序是workBench,所以会用workBench来进行讲解. create table tbl_user( id int(11) unsigned not nul ...