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 ...
随机推荐
- PHP练习4 留言板
一.要求 二.示例页面 三.网页代码及网页显示 1.denglu.php 登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- Vuejs - 组件式开发
初识组件 组件(Component)绝对是 Vue 最强大的功能之一.它可以扩展HTML元素,封装可复用代码.从较高层面讲,可以理解组件为自定义的HTML元素,Vue 的编译器为它添加了特殊强大的功能 ...
- Shuffle Cards(牛客第三场+splay)
题目: 题意:将1~n的数进行m次操作,每次操作将第pi位到pi+si-1位的数字移到第一位,求最后的排列. 思路:现在还没不会写splay,在知道这是splay模板题后找了一波别人的模板,虽然过了, ...
- LCA入门题集小结
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目: How far away ? Time Limit: 2000/1000 MS (Jav ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- 小程序var that=this
小程序的js函数中,一般第一句就是var that=this,那么此语句的必要性是什么呢?下面用一段代码来解释这个问题 Page({ //页面的初始数据 loadUsers: function () ...
- hdu 1150 Machine Schedule(二分匹配,简单匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150 Machine Schedule Time Limit: 2000/1000 MS (Java/ ...
- Java 中的方法内部类
方法内部类就是内部类定义在外部类的方法中,方法内部类只在该方法的内部可见,即只在该方法内可以使用. 一定要注意哦:由于方法内部类不能在外部类的方法以外的地方使用,因此方法内部类不能使用访问控制符和 s ...
- Intel MKL(Math Kernel Library)
1.Intel MKL简介 Intel数学核心函数库(MKL)是一套高度优化.线程安全的数学例程.函数,面向高性能的工程.科学与财务应用.英特尔 MKL 的集群版本包括 ScaLAPACK 与分布式内 ...
- Unix/Linux Command Reference