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 ...
随机推荐
- ETL应用:使用shell实现文件级校验的方法
BI应用中,对接口规范性约束很重要,接口文件提供需要配套提供该文件的校验文件,校验文件格式如下: 序号 信息内容 数据类型及长度 说明 1 接口数据文件名称 CHAR(50) 2 文件的大小(字节数) ...
- gstreamer——文档/资源/使用
http://gstreamer.freedesktop.org/src/ http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gs ...
- RocketMq入坑指南
报错信息Caused by: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to <172.1 ...
- P4755 Beautiful Pair
题目 洛谷 做法 \(i≤x≤j,a[i]<\frac{a[x]}{a[j]}\) 考虑\(a[x]\)的贡献,单调栈预处理\(L,R\)能作为最大值的区间 枚举一端点,仅需另一端点满足条件即可 ...
- 使用Java实现八种基本排序
插入排序.选择排序.冒泡排序.希尔排序.堆排序.快速排序.归并排序.基数排序 import java.util.ArrayList; import java.util.List; /** * 排序算法 ...
- INSPIRED启示录 读书笔记 - 第37章 大众网络服务产品
十大要点 1.可用性:大众网络服务产品必须具备良好的用户体验 2.人物角色:按典型特征将用户分类,抽象出有代表性的用户类型(人物角色) 3.扩展性:应该不间断地考虑扩展性问题,永远留有余地,不到万不得 ...
- Anaconda创建环境、删除环境、激活环境、退出环境
Anaconda创建环境: //下面是创建python=3.6版本的环境,取名叫py36 conda create -n py36 python=3.6 删除环境(不要乱删啊啊啊) conda re ...
- 常用java开发工具快捷键
在这里列举一些开发中常用的快捷键 常用的idea的快捷键: 1.删除当前行:Ctrl+X 2.格式化代码:Ctrl+Alt+L 3.查看本页里面的内容:Ctrl+F 4.查看类的继承方式:Ctrl+H ...
- spring mvc 对象型参数的传递(遇到坑了)
直接来个列子: 这里设置了,contenType="application/json" 这里post 接收的参数对象. 但是问题来了: <html> <head& ...
- JMeter ——Test fragment
fragment 为片段,可以是一个不完整的用例.比如你可以把一个http请求保存为fragment,如果不这样做的话,你是必须先要添加一个测试计划-线程组-http请求的.你可以把某步骤一系列的请求 ...