A Simple Problem with Integers POJ - 3468 (分块)
题目链接: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 (分块)的更多相关文章
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- 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 ...
- C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- C - A Simple Problem with Integers
C - A Simple Problem with Integers POJ - 3468 思路:线段树区间修改区间查询.又出现了 C++ WA G++ AC的尴尬局面. #include& ...
- 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 ,别的没什么大 ...
随机推荐
- bash 6
1)如果在开发过程中,遇到大段的代码需要临时注释起来,过一会儿又取消注释,怎么办呢? 每一行加个#符号太费力了,可以把这一段要注释的代码用一对花括号括起来,定义成一个函数, 没有地方调用这个函数,这块 ...
- Mybatis项目中不使用代理写法【我】
首先 spring 配置文件中引入 数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- postman charles设置代理
1.首先现在证书 2.确保手机和电脑是同一网络,ifconfig查看电脑ip 3.设置手机->配置代理-手动-服务器(电脑的ip)-端口8888 4.设置charles代理 5.重新启动char ...
- ElasticSearch6.5.0 【Rejecting mapping update to [posts] as the final mapping would have more than 1 type】
今天想在一个Index上增加一个type,结果报错 java.lang.IllegalArgumentException: Rejecting mapping update to [posts] as ...
- bzoj2243 树链剖分
https://www.lydsy.com/JudgeOnline/problem.php?id=2243 新学的树剖,在维护的时候线段树维护区间内颜色数量以及左右两端的颜色.统计的时候区间合并时判断 ...
- 使用Redis模拟简单分布式锁,解决单点故障的问题
需求描述: 最近做一个项目,项目中有一个功能,每天定时(凌晨1点)从数据库中获取需要爬虫的URL,并发送到对应的队列中,然后客户端监听对应的队列,然后执行任务.如果同时部署多个定时任务节点的话,每个节 ...
- MySQL复制相关参数详解
MySQL复制相关参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.复制相关系统变量 1>.server_id 是必须设置在master和每个slave上的唯一标 ...
- nginx的rewrite指令
需求:我们在接收到一个/api/upload 开头的url的时候 要转发到另外一个地址.但是又不想追加/api/upload 只想追加/upload 地址. Nginx提供了rewrite指令,用于 ...
- Java 微信公众号导出所有粉丝(openId)
由于公众号换了公司主体,需要做迁移,玩家的openId数据需要做处理. (我是按我要的json格式,将粉丝导成了1万条数据的一个json文件) 文件格式: { "info":[ { ...
- expdp和impdp导入导出用法【转】
关于expdp和impdp exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.expdp和impdp是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用.i ...