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 ...
随机推荐
- 去除html标记和替换script标记
1: /// <summary> 2: /// 去除HTML标记 3: /// </summary> 4: /// <param name="NoHTML&qu ...
- IF判断条件说明
在Python中,任何非零整数都为true,0是false:判断条件也可以是任何序列(列表.元组.字符串):所有长度不为零的为true,否则为false,比如:空序列为false.简而言之:非0非空为 ...
- 【python标准库模块四】Json模块和Pickle模块学习
Json模块 原来有个eval函数能能够从字符串中提取出对应的数据类型,比如"{"name":"zhangsan"}",可以提取出一个字典. ...
- SpringMVC 教程 - Handler Method
原文链接:https://www.codemore.top/cates/Backend/post/2018-04-21/spring-mvc-handler-methods 由注解@RequestMa ...
- LintCode题解之斐波纳契数列
直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @p ...
- ICL Auto Vectorization
简介 此文简单介绍如何使用intel c++编译器实现向量化加速. 全文如下安排: base : 待优化的源代码. vectorization : 第一个向量化版本. aligned : 内存对其对向 ...
- 用Codility测试你的编码能力
没有宏观的架构设计,没有特定的框架语言.在Codility提出的一些小问题上,用最纯粹的方式测试你最基本的编码能力. Codility第一课:算法复杂度 各种算法书的开篇大多是算法分析,而复杂度(co ...
- 阻塞IO服务器模型之多线程服务器模型
针对单线程服务器模型的特点,我们可以对其进行改进,使之能对多个客户端同时进行响应.最简单的改进即是使用多线程(或多进程)服务器模型,在应用层级别,我们一般采用多线程模式.多线程能让多个客户端同时请求, ...
- FFmpeg源代码简单分析:avformat_close_input()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- UNIX网络编程——通过UNIX域套接字传递描述符和 sendmsg/recvmsg 函数
在前面我们介绍了UNIX域套接字编程,更重要的一点是UNIX域套接字可以在同一台主机上各进程之间传递文件描述符. 下面先来看两个函数: #include <sys/types.h> #in ...