浅谈分块: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的更多相关文章

  1. POJ3468:A Simple Problem with Integers(线段树模板)

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

  2. 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 ...

  3. POJ3468:A Simple Problem with Integers——题解

    http://poj.org/problem?id=3468 实现一个线段树,能够做到区间修改和区间查询和. 明显板子题. #include<cstdio> #include<cma ...

  4. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  5. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

  6. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

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

  7. 【poj3468】 A Simple Problem with Integers

    http://poj.org/problem?id=3468 (题目链接) 题意 给出一个序列,要求维护区间修改与区间求和操作. Solution 多年以前学习的树状数组区间修改又忘记了→_→. 其实 ...

  8. 【poj3468】A Simple Problem with Integers

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97008   Accepted: 30285 Case Time Limi ...

  9. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

随机推荐

  1. HttpUtils工具类

    HttpUtils工具类 package net.hs.itn.teng.common.util; import java.io.IOException; import java.io.Unsuppo ...

  2. CSS3透明背景表单

    在线演示 本地下载

  3. PHP类的变量与成员,及其继承、访问与重写要注意的问题

    PHP的类及其实例: <?php ?>  后期静态绑定:为了避免子类重写静态属性后,使用继承来的方法仍然方法父类的静态属性,PHP5.3增加了一个新的语法,后期静态绑定,使用static关 ...

  4. 20145240《Java程序设计》第一周学习总结

    20145240 <Java程序设计>第一周学习总结 教材学习内容总结 第一周学习Java首先从最基本的下载.配置环境.了解基本人机命令行方式等基础知识,以及编写简单的"Hell ...

  5. RDLC 微软报表 自定义函数

    报表的空白处点右键,报表属性,CODE,按下面的格式输入自定义函数: Shared Function ShowDate(value as DateTime) as string if value< ...

  6. mysql里的ibdata1文件

    mysql大多数磁盘空间被 InnoDB 的共享表空间 ibdata1 使用.而你已经启用了 innodb_file_per_table,所以问题是: ibdata1存了什么? 当你启用了innodb ...

  7. Python httpServer服务器(初级)

    使用原生的python开发的web服务器,入门级! #!/usr/bin/python # -*- coding: UTF-8 -*- import os #Python的标准库中的os模块包含普遍的 ...

  8. BZOJ3669 [Noi2014]魔法森林(SPFA+动态加边)

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  9. JMeter正则表达式提取器说明

    Apply to:应用范围 要检查的响应字段:样本数据源. 引用名称:其他地方引用时的变量名称,引用方法:${引用名称} 正则表达式:数据提取器,如上图的 "sysNo":&quo ...

  10. (python)循环中动态产生变量

    >>> for i in xrange(5): exec 'a'+str(i)+' = '+str(i)+'' >>> a0 0 >>> a1 1 ...