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 ...
随机推荐
- UVA 580 Critical Mass
https://vjudge.net/problem/UVA-580 题意:一堆U和L,用n个排成一排,问至少有3个U放在一起的方案数 f[i] 表示 至少有3个U放在一起的方案数 g[i] 表示没有 ...
- 51Nod 1009 数字1的个数 | 数位DP
题意: 小于等于n的所有数中1的出现次数 分析: 数位DP 预处理dp[i][j]存 从1~以j开头的i位数中有几个1,那么转移方程为: if(j == 1) dp[i][j] = dp[i-1][9 ...
- Redis 键值数据类型及基本操作
到目前为止,Redis 支持的键值数据类型如下: 字符串(String) 哈希(Map) 列表(list) 集合(sets) 有序集合(sorted sets) 1. String 字符串类型 s ...
- 【51NOD】1201 整数划分
[题意]将n划分成不同正整数的和的方案数. [算法]动态规划 [题解] 暴力:f[i][j]:只用前1..i的数字,总和为j的方案数 本质上是01背包,前i个物体,总质量为j的方案数 f[i][j]= ...
- 逃生(HDU4857 + 反向拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...
- Python 下调用C动态链接库 -- (转)
在linux开发的动态链接库需要被python调用,首先需要生成.so文件. 生成动态链接库的方法网上有很多,主要就是首先根据源文件编译生成.o,然后链接这些.o文件-shared生成.so.需要注意 ...
- python碎片记录(一)
1.python中求幂运算 2**31-1 2的31次方减一(32位整型数据范围为-2^31~2^31-1,最高位为符号位,负向值比正向值绝对值大1) 2.python中整数除法 x=x//1 ...
- 【C语言】Coursera课程《计算机程式设计》台湾大学刘邦锋——Week6 String课堂笔记
Coursera课程 <计算机程式设计>台湾大学 刘邦锋 Week6 String 6-1 Character and ASCII 字符变量的声明 char c; C语言使用一个位元组来储 ...
- [转载]关于python字典类型最疯狂的表达方式
一个Python字典表达式谜题 让我们探究一下下面这个晦涩的python字典表达式,以找出在python解释器的中未知的内部到底发生了什么. # 一个python谜题:这是一个秘密 # 这个表达式计算 ...
- bzoj 1179 Atm
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1179 题解: 一道比较综合的图论题 直接讲正解: 如果这个图G中存在某个强连通分量,那么这 ...