Poj3468 A Simple Problem with Integers (分块)
题面
题解
区间求和$+$区间修改板子,这里用分块写的
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;
#define int ll
const int N = 1e5 + 10 , SN = 340;
int n, siz, q, bel[N], val[N];
int sum[SN], add[SN], L[SN], R[SN];
template<typename T>
void read(T &x) {
int flag = 1; x = 0; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}
void modify (int l, int r, int c) {
int fl = bel[l], fr = bel[r];
if(fl == fr) {
for(int i = l; i <= r; ++i)
val[i] += c, sum[fl] += c;
} else {
for(int i = l; i <= R[fl]; ++i)
val[i] += c, sum[fl] += c;
for(int i = fl + 1; i < fr; ++i) add[i] += c;
for(int i = L[fr]; i <= r; ++i)
val[i] += c, sum[fr] += c;
}
}
int query(int l, int r) {
int fl = bel[l], fr = bel[r], ret = 0;
if(fl == fr) {
for(int i = l; i <= r; ++i)
ret += val[i] + add[fl];
} else {
for(int i = l; i <= R[fl]; ++i)
ret += val[i] + add[fl];
for(int i = fl + 1; i < fr; ++i) ret += sum[i] + add[i] * (R[i] - L[i] + 1);
for(int i = L[fr]; i <= r; ++i)
ret += val[i] + add[fr];
} return ret;
}
signed main () {
read(n), read(q), siz = sqrt(n);
for(int i = 1; i <= n; ++i)
read(val[i]), bel[i] = (i - 1) / siz + 1, sum[bel[i]] += val[i];
for(int i = 1; i <= bel[n]; ++i)
L[i] = R[i - 1] + 1, R[i] = i * siz;
R[bel[n]] = n; int l, r, k;
while(q--) {
char opt; scanf("\n%c", &opt);
read(l), read(r);
if(opt == 'Q') printf("%lld\n", query(l, r));
else read(k), modify(l, r, k);
}
return 0;
}
Poj3468 A Simple Problem with Integers (分块)的更多相关文章
- POJ3468 a simple problem with integers 分块
题解:分块 解题报告: 是个板子题呢qwq 没什么可说的,加深了对分块的理解趴还是 毕竟这么简单的板子题我居然死去活来WA了半天才调出来,,,哭了QAQ 还是说下我错在了哪几个地方(...是的,有好几 ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
随机推荐
- cookie与session的区别与应用
通常我们所说的浏览器自动保存密码,下次不用登陆,提示一次就不再出现的内容,大部分通过cookie或者session来实现的. cookie的概念 cookie是浏览器(User Agent)访问一些网 ...
- java 8新特性 instant
Java 8目前已经开始进入大众的视线,其中笔者在写本文之前,留意到其中Java 8预览版中将会出现新的关于日期和时间的API(遵守JSR310规范).在本系列文章中,将对这些新的API进行举例说明. ...
- Spring Web 项目Junit测试报错问题
测试对象是Web项目的Service类,参照网上查到的资料,按如下方式执行时报错, //使用junit4进行单元测试 @RunWith(SpringJUnit4ClassRunner.class) / ...
- js 数组&字符串 去重
Array.prototype.unique1 = function() { var n = []; //一个新的临时数组 for(var i = 0; i < this.length; i++ ...
- Python3.3.3 安装(Linux系统)
1.wget http://www.python.org/ftp/python/3.3.3/Python-3.3.3.tgz //检查http://www.python.org/ftp/python网 ...
- Ribbon的主要组件与工作流程
一:Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接 ...
- windows下常用快捷键(转)
原文转自 https://blog.csdn.net/LJFPHP/article/details/78818696 win+E 打开文件管器 win+D ...
- Python3 xml模块的增删改查
xml数据示例 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <data> < ...
- vue-实现倒计时功能
JavaScript 创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日.时.分.秒数,每隔一秒递归执行一次. msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以 ...
- 工具===代替cmd的conemu设置
conemu设置 Win+Alt+P进入设置界面,字体设置: 隐藏右上角菜单和窗口标题. (Ctrl + ~ 隐藏/显示terminal) 设置背景图片 避免误操作,关闭/新建确认 设置win+w默认 ...