题目链接:https://cn.vjudge.net/problem/POJ-3468

题目大意:区间加减+区间查询操作。

具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧。

分块真的好暴力,,,(但是还是挺优美的)。

说一下各个数组的作用。

a数组,代表每一个点的值。

sum数组,代表分块后每一段的值。

add数组,相当于线段树中的lazy标记,记录的数当前这一整段添加的值。

l数组,r数组,记录每一段的左端点和右端点。

belong数组,记录每一个数属于哪一个分块中。

AC代码:

 #include<iostream>
#include<stack>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<math.h>
using namespace std;
# define ll long long
const int maxn = 1e6+;
ll a[maxn],sum[maxn],add[maxn];
int l[maxn],r[maxn];
int belong[maxn];
void buildblock(int t){
int tmp=(int)sqrt(t*1.0);
int tot=t/tmp;
if(t%tmp)tot++;
for(int i=;i<=t;i++){
belong[i]=(i-)/tmp+;
}
for(int i=;i<=tot;i++){
l[i]=(i-)*tmp+;
r[i]=i*tmp;
}
r[tot]=t;
for(int i=;i<=tot;i++){
for(int j=l[i];j<=r[i];j++){
sum[i]+=a[j];
}
}
}
void update(int st,int ed,ll val){
if(belong[st]==belong[ed]){
for(int i=st;i<=ed;i++){
a[i]+=val;
sum[belong[st]]+=val;
}
return ;
}
for(int i=st;i<=r[belong[st]];i++){
a[i]+=val;
sum[belong[st]]+=val;
}
for(int i=l[belong[ed]];i<=ed;i++){
a[i]+=val;
sum[belong[ed]]+=val;
}
for(int i=belong[st]+;i<belong[ed];i++){
add[i]+=val;
}
}
ll ask(int st,int ed){
ll ans=;
if(belong[st]==belong[ed]){
for(int i=st;i<=ed;i++){
ans+=a[i]+add[belong[st]];
}
return ans;
}
for(int i=st;i<=r[belong[st]];i++){
ans+=a[i]+add[belong[st]];
}
for(int i=l[belong[ed]];i<=ed;i++){
ans+=a[i]+add[belong[ed]];
}
for(int i=belong[st]+;i<belong[ed];i++){
ans+=sum[i]+add[i]*(r[i]-l[i]+);
}
return ans;
}
int main(){
//freopen("hqx.in","r",stdin);
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
buildblock(n);
char str[];
int st,ed;
ll cost;
while(m--){
scanf("%s",str);
if(str[]=='Q'){
scanf("%d %d",&st,&ed);
printf("%lld\n",ask(st,ed));
}
else if(str[]=='C'){
scanf("%d %d %lld",&st,&ed,&cost);
update(st,ed,cost);
}
}
return ;
}

A Simple Problem with Integers POJ - 3468 (分块)的更多相关文章

  1. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

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

  2. A Simple Problem with Integers~POJ - 3468

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  3. C - A Simple Problem with Integers - poj 3468(区间更新)

    题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...

  4. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  5. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  6. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  7. C - A Simple Problem with Integers

    C - A Simple Problem with Integers POJ - 3468   思路:线段树区间修改区间查询.又出现了 C++ WA    G++ AC的尴尬局面. #include& ...

  8. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

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

随机推荐

  1. 在gitlab新建空项目,将本地的git仓库的内容上传

    gitlab新建了这个项目. 按照官网的步骤上传代码 一:将本地代码上传到本地仓库 1.进入项目文件夹 git init 2.项目代码添加到本地git git add . 3.提交到stage区域 g ...

  2. tomcat部署-手动启动tomcat部署,添加网页,

    公司的内网什么都不能往外传,于是自己用公司的网络搭了一个网页,在网上抄了一堆upload,用来来回传输数据.... 但是每次用ideaJ启动服务器太费时. 研究了一下怎么手动启动tomcat,部署网页 ...

  3. decorator 装饰页面,根据不同设备自动切换移动和pc站

    package com.thinkgem.jeesite.modules.sys.interceptor; import javax.servlet.http.HttpServletRequest; ...

  4. http协议中的请求方式

    get:获取url传的查询字符串(action=show)表单和连接的url中传的值.容量2K左右. post:以post方式提交,获取表单和连接的url中传的值.容量8M左右. delete:删除某 ...

  5. mariadb-5.5安装

    mariadb-5.5 Windows10安装 1.官网下载:https://downloads.mariadb.org/ 2.解压mariadb-5.5.58-winx64.zip,目录C:\mar ...

  6. 集成学习算法汇总----Boosting和Bagging(推荐AAA)

     sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  7. docker 基础之镜像加速

    国内访问 Docker Hub 有时会遇到困难,此时可以配置镜像加速器 对于使用 systemd 的系统,用 systemctl enable docker 启用服务后,编辑 /etc/systemd ...

  8. SpringBoot实战一:发送邮件

    目录 邮件协议 引入邮件包 创建邮件类和测试类,写yml文件 文本邮件,HTML邮件,附件邮件,图片邮件 模板邮件 异常处理 来进行一个SpringBoot项目的实战,发送一下邮件,这里我们先了解一下 ...

  9. python 面向对象(二)成员

    ##################################总结########################### 类的成员: 变量: 实例变量      对象.属性=xxx 类变量    ...

  10. Dubbo优雅关机原理

    Dubbo是通过JDK的ShutdownHook来完成优雅停机的 所以如果用户使用 kill -9 PID 等强制关闭命令,是不会执行优雅停机的 只有通过 kill PID时,才会执行 原理: · 服 ...