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 ...
随机推荐
- 【leetcode刷题笔记】Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- ES6 随记(2)-- 解构赋值
上一章请见: 1. ES6 随记(1)-- let 与 const 3. 解构赋值 a. 数组的解构赋值 let [a1, b1, c1] = [1, 2, 3]; console.log(a1, b ...
- AngularJs 的一则错误 [$INJECTOR:MODULERR]
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:modu ...
- 原生javasxript获取浏览器的滚动距离和可视窗口的高度
原生javasxript获取浏览器的滚动距离和可视窗口的高度 //封装兼容性方法获取滚动的距离 function getScrollOffset(){ if(window.pageXOffset){ ...
- uboot无法引导uImage错误及其解决方法
先编译友善提供的linux内核: make ARCH=arm mini2440_defconfigmake CROSS_COMPILE=arm-linux- uImage 在arch/arm/boot ...
- Python 集合set概念和操作
# 集合 # 概念 # 无序的, 不可随机访问的, 不可重复的元素集合 # 与数学中集合的概念类似,可对其进行交.并.差.补等逻辑运算 # 分为可变集合和非可变集合 # set # 为可变集合 # 增 ...
- Android SDK组件:webview笔记
1.安卓手机中内置了一款webkit内核的浏览器,在SDK中封装为WebView组件. 2.该组件可以在自己的应用程序中显示本地或者Internet上的网页,也可以把它当作一个浏览器来时用. 3.We ...
- freemarker空值的处理
FreeMarker的变量必须赋值,否则就会抛出异常.而对于FreeMarker来说,null值和不存在的变量是完全一样的,因为FreeMarker无法理解null值.FreeMarker提供两个运算 ...
- 算法总结之 数组的partition调整 三个值的升序
给定一个数组arr, 其中只可能有 0,1,2三个值,请实现arr排序 另一种问法: 有一个数组,只有红 蓝 黄 球,请事先红球全放在数组的左边,蓝球放中间,黄球放右边 另一种问法: 有一个数组,再给 ...
- skynet中动态库的处理
skynet中的.so动态库由service-src中的c文件编译完后生成,其中最重要的是snlua.c. 源码地址:https://github.com/cloudwu/skynet/service ...