POJ3468:A Simple Problem with Integers
浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html
题目传送门:http://poj.org/problem?id=3468
分块裸题。对于每个块记录权值和与加标记即可。详情见代码。
时间复杂度:\(O(n\sqrt{n})\)
空间复杂度:\(O(n)\)
代码如下:
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
char s[5];
ll sum[320];
int n,m,block;
int tag[320],val[maxn];
int bel[maxn],l[320],r[320];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int main() {
n=read(),m=read();block=sqrt(n);
for(int i=1;i<=n;i++) {
val[i]=read();
bel[i]=(i-1)/block+1,sum[bel[i]]+=val[i];
if(bel[i]!=bel[i-1])r[bel[i-1]]=i-1,l[bel[i]]=i;
}r[bel[n]]=n;
for(int i=1;i<=m;i++) {
scanf("%s",s+1);
int L=read(),R=read();
if(s[1]=='Q') {
ll res=0;
if(bel[L]==bel[R]) {
for(int j=L;j<=R;j++)
res+=val[j]+tag[bel[L]];
}
else {
for(int j=bel[L]+1;j<bel[R];j++)
res+=sum[j]+1ll*tag[j]*(r[j]-l[j]+1);
for(int j=L;j<=r[bel[L]];j++)
res+=val[j]+tag[bel[L]];
for(int j=l[bel[R]];j<=R;j++)
res+=val[j]+tag[bel[R]];
}
printf("%lld\n",res);
}
else {
int x=read();
if(bel[L]==bel[R]) {
for(int j=L;j<=R;j++)
val[j]+=x,sum[bel[j]]+=x;
}
else {
for(int j=bel[L]+1;j<bel[R];j++)
tag[j]+=x;
for(int j=L;j<=r[bel[L]];j++)
val[j]+=x,sum[bel[L]]+=x;
for(int j=l[bel[R]];j<=R;j++)
val[j]+=x,sum[bel[R]]+=x;
}
}
}
return 0;
}
POJ3468:A Simple Problem with Integers的更多相关文章
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
- POJ3468:A Simple Problem with Integers (线段树||树状数组||Splay解决基本问题的效率对比)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- POJ3468:A Simple Problem with Integers——题解
http://poj.org/problem?id=3468 实现一个线段树,能够做到区间修改和区间查询和. 明显板子题. #include<cstdio> #include<cma ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...
- POJ 3468:A Simple Problem with Integers(线段树区间更新模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 141093 ...
- 【poj3468】 A Simple Problem with Integers
http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...
- 【poj3468】A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97008 Accepted: 30285 Case Time Limi ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
随机推荐
- 交叉编译Mesa,X11lib,Qt opengl
记录Mesa配置文件如下: Mesa版本:Mesa-10.2.3 CC=/usr/local/arm-4.8.1/bin/arm-none-linux-gnueabi-gcc CXX=/usr/loc ...
- vue项目的webpack设置请求模拟数据的接口方法
最近在跟着视频写饿了吗vue项目,其中模拟数据由于webpack版本变化,跟视频中不一致,下方博客有解决方案,其实视频里面的还能看懂,现在webpack的服务都在插件包里了,好难找. 请参考:http ...
- Eclipse 换主题、皮肤、配色,换黑色主题护眼
Eclipse写android代码时,默认的文本和框架都是白色,长时间使用,显得过于刺眼.这里介绍三种方法换黑色护眼配色. 1.系统设置里更改 2.从Eclipse Marketplace里下载主题 ...
- 多校hdu-5775 Bubble sort(线段树)
题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...
- centos下无法使用lsof命令"-bash: lsof: command not found"
1.问题描述 : 在CentOS下,使用lsof命令,报错如下: 2.解决方法: #yum install lsof 若输入y不能安装成功,通过yum install 包 -y 进行安装: # yum ...
- 关于 Java正则表达式中的Possessive数量修饰词的理解
关于 Java正则表达式中的Possessive数量修饰词的理解 正则表达式对于数量限定符如 ?, + , *, {n, m} 的匹配默认是贪婪模式,比如: a.*b 匹配 acbab 的结果是 ...
- C#使用Sockets操作FTP
http://blog.csdn.net/foart/article/details/6824551 using System; using System.Collections; using Sys ...
- JavaScriptSerializer 日期处理 JSON.Net
[WebMethod(Description = "取得所有人员 自带json")] [SoapHeader("key")] [ScriptMethod(Res ...
- Generator函数介绍
Generator函数 基本概念 英文意思为 "生成器". generator函数是es6提供的一种异步编程解决方案,语法行为与传统函数完全不同.从状态上,首先我们把他理解成一种状 ...
- Dom节点操作常用方法
1.访问/获取节点 document.getElementById(id); //返回对拥有指定id的第一个对象进行访问 document.getElementsByName(name); //返回带 ...