浅谈分块: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. Django组件 用户认证,form

    auth模块 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中: 如果用户存在于数据库中,然后再验证用户输入的密码,这样一来就要自己编写大量的代码 ...

  2. jquery click()方法模拟点击事件对a标签不生效

    if(e.keyCode == 13) { $items.eq(index).click(); return; } 搜索框下拉列表模拟点击时间,使用上述代码不能触发链接跳转 1,页面使用了bootst ...

  3. HAproxy 源码包安装

    HAproxy 源码包安装 系统环境:Centos 7 x64位 服务版本:haproxy-1.7.8.tar.gz 编译工具:gcc 下载地址 HAproxy:https://pan.baidu.c ...

  4. CSS3 3D旋转按钮对话框

    在线演示 本地下载

  5. new Date(dateString)

    xxxx-xx-xx xx:xx:xx chrome firefox opera xxxx/xx/xx xx:xx:xx chrome firefox opera safari ios(苹果手机只认此 ...

  6. IIS部署PHP项目并与mysql完美结合

    在上一篇文章中,提到iis与apache共用80端口,但是发现很多问题,例如 IIS网站有支付功能,而微信支付是不支持带有端口的网址的,虽然通过apche代理,在外面看来没有端口,可是内部还是避免不了 ...

  7. Android系统开发--灯光系统之电池灯的流程分析

    Android系统开发--Android灯光系统之电池灯的流程分析 前期系统准备 运行初始化,创建系统服务 创建电池服务,获得电池灯;创建监听者监听上报电池事件: mSystemServiceMana ...

  8. 解决Linux系统在设置alias命令重启后失效的问题

    在使用linux系统的过程中,大多数情况下都是在字符界面下进行的.有些比较长的命令我们不希望每次都重复输入,这样不仅浪费时间而且还容易出错:我们会使用alias命令来解决 比如: alias ll=' ...

  9. java进阶之-Maven,svn,git,maven合拼多个项目

    git的使用介绍(写很容易懂得哦) maven合拼多个项目(写得很好哦) MAVEN作用:统一开发规范与工具:统一管理jar包 1.下载MAVEN  下载绿色版的面安装 2.环境配置 eclipse想 ...

  10. 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树

    package com.test.tree; import java.util.Iterator; /** * 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树. * 在每个节点上添加一 ...