P6357 题解
题目描述
给定一串长度为 \(n\) 的数字,数字为 \(0 \sim 9\) 之间的任意一个,下标从 \(1\) 记起。
然后进行 \(m\) 次区间查询,每次查找区间 \([l, r]\) 的区间和,并在查询结束后将区间里的每一个数都 $ + 1$。特殊地,如果 $ + 1$ 前的数字为 \(9\),那么 $ + 1$ 之后就变成了 \(0\)。
输出每次查询的区间和。
题目分析
使用支持区间加和区间求和的数据结构即可。在这里使用分块和线段树。
分块
跑的飞快。
\(9 + 1 \to 0\) 这个限制非常的难搞。然而我们发现一共只有十个数字,因此不管怎么暴力搞都是没问题的(大不了乘个大常数)。
在每个块内维护以下信息:当前块内 \(1 \sim 9\) 每个数出现的次数(\(cnt_i\)),当前块的懒标记(\(add\))。那么一个块内的区间和就是:
\]
代码示例如下:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
const int N = 250010, M = (int)sqrt(N) + 20;
const int INF = 0x3f3f3f3f;
int n, m, len, w[N];
struct Block {
int l = INF, r = -INF, add;
int cnt[10];
}b[M];
int get(int x) {
return (int)x / len;
}
void change(int k, int &x) {
b[k].cnt[x] -- ;
b[k].cnt[(x + 1) % 10] ++ ;
x = (x + 1) % 10;
}
void modify(int l, int r) {
int lc = get(l), rc = get(r);
if (lc == rc) {
for (int i = l; i <= r; i ++ )
change(lc, w[i]);
return;
}
for (int i = l; i <= b[lc].r; i ++ ) change(lc, w[i]);
for (int i = r; i >= b[rc].l; i -- ) change(rc, w[i]);
for (int i = lc + 1; i <= rc - 1; i ++ )
b[i].add = (b[i].add + 1) % 10;
}
int query_block(int k) {
int ans = 0;
for (int i = 0; i <= 9; i ++ )
ans += b[k].cnt[i] * ((i + b[k].add) % 10);
return ans;
}
int query(int l, int r) {
int lc = get(l), rc = get(r);
int ans = 0;
if (lc == rc) {
for (int i = l; i <= r; i ++ )
ans += (w[i] + b[lc].add) % 10;
return ans;
}
for (int i = l; i <= b[lc].r; i ++ ) ans += (w[i] + b[lc].add) % 10;
for (int i = r; i >= b[rc].l; i -- ) ans += (w[i] + b[rc].add) % 10;
for (int i = lc + 1; i <= rc - 1; i ++ )
ans += query_block(i);
return ans;
}
int main() {
scanf("%d%d", &n, &m); len = (int)sqrt(n);
for (int i = 1; i <= n; i ++ )
scanf("%1d", &w[i]);
for (int i = 1; i <= n; i ++ ) {
b[get(i)].l = min(b[get(i)].l, i);
b[get(i)].r = max(b[get(i)].r, i);
b[get(i)].cnt[w[i]] ++ ;
}
while (m -- ) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(l, r));
modify(l, r);
}
return 0;
}
单次询问复杂度为 \(O(\sqrt{n})\),空间复杂度 \(O(\sqrt{n})\)
线段树
思路与分块相同,即在每一个节点维护每个数出现的次数。注意 pushup 和 pushdown 的处理。
单次询问复杂度 \(O(\log n)\) 明显快于分块。空间复杂度 \(O(n)\) 略劣。
代码示例如下:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 250010;
int w[N], n, m;
struct Tree {
int l, r;
int sum, add;
int cnt[10];
}tr[N << 2];
#define ls u << 1
#define rs u << 1 | 1
void pushup(int u) {
tr[u].sum = tr[ls].sum + tr[rs].sum;
for (int i = 0; i <= 9; i ++ )
tr[u].cnt[i] = tr[ls].cnt[i] + tr[rs].cnt[i];
}
void push(int u, int k) { // 这里需要注意
tr[u].add += k;
while (k -- ) {
for (int i = 9; i; i -- )
swap(tr[u].cnt[i], tr[u].cnt[i - 1]);
}
tr[u].sum = 0;
for (int i = 1; i <= 9; i ++ )
tr[u].sum += tr[u].cnt[i] * i;
}
void pushdown(int u) {
if (tr[u].add) {
tr[u].add %= 10;
push(ls, tr[u].add);
push(rs, tr[u].add);
tr[u].add = 0;
}
}
void build(int u, int l, int r) {
tr[u] = {l, r};
if (l == r) {
tr[u].sum = w[r];
tr[u].cnt[w[r]] ++ ;
return;
}
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
pushup(u);
}
void modify(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r)
return (void)push(u, 1);
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(ls, l, r);
if (r > mid) modify(rs, l, r);
pushup(u);
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r)
return tr[u].sum;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1, sum = 0;
if (l <= mid) sum += query(ls, l, r);
if (r > mid) sum += query(rs, l, r);
return sum;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ )
scanf("%1d", &w[i]);
build(1, 1, n);
while (m -- ) {
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(1, l, r));
modify(1, l, r);
}
return 0;
}
P6357 题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
- JSOI2016R3 瞎BB题解
题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...
随机推荐
- Vue 搭配 Spring MVC 创建一个 web 项目
Vue 搭配 Spring MVC 创建一个 web 项目 想要写一个登录的web应用程序.页面使用Vue,后端使用Spring MVC,最终打成war包,放在tomcat下启动. 1.创建Sprin ...
- Teamcenter RAC 开发之《Excel模版导出》
背景 在做 Teamcenter RAC客制化表单后,TMD肯定有一个需求要导出表单,毕竟所谓的客制化表单就是从纸质表单中出来的,那么写代码必不可少......... 那么问题来了,对于一个Excel ...
- ORA-01008:并非所有变量都已绑定-解决办法
近期批量处理数据,后台用JAVA编写,连接Oracle数据库,程序运行报ORA-01008问题.解决这个问题时遇见的坑较多,下面复盘现象.问题提出解决办法,希望能帮到遇见同类问题的你. 调试问题: 后 ...
- python - networker api
Getting Started,概述 NetWorker REST API is an interface that provides programmatic access to the Net ...
- 从零用VitePress搭建博客教程(2) –VitePress默认首页和头部导航、左侧导航配置
2. 从零用VitePress搭建博客教程(2) –VitePress默认首页和头部导航.左侧导航配置 接上一节: 从零用VitePress搭建博客教程(1) – VitePress的安装和运行 四. ...
- bootstrap响应式原理
Bootstrap 框架的网格系统工作原理如下:1 .数据行 (.row) 必须包含在容器( .container )中,以便为其赋予合适的对齐方式和内距 (padding) . 如: <div ...
- 数据结构与算法 | 深搜(DFS)与广搜(BFS)
深搜(DFS)与广搜(BFS) 在查找二叉树某个节点时,如果把二叉树所有节点理理解为解空间,待找到那个节点理解为满足特定条件的解,对此解答可以抽象描述为: 在解空间中搜索满足特定条件的解,这其实就是搜 ...
- 1. JVM内存区块
本篇文章主要讲解Java(JVM)在运行期间,其运行时数据区域的作用.职责与划分.包括堆内存.栈内存--虚拟机栈.本地方法栈.方法区.常量池.程序计数器等概念. 采集可以使用JavaMXBean(采集 ...
- [Python急救站课程]无角正方形
无角正方形 from turtle import * penup() fd(-100) pendown() pensize(10) penup() seth(0) fd(20) pendown() f ...
- 神经网络入门篇:详解多样本向量化(Vectorizing across multiple examples)
多样本向量化 与上篇博客相联系的来理解 逻辑回归是将各个训练样本组合成矩阵,对矩阵的各列进行计算.神经网络是通过对逻辑回归中的等式简单的变形,让神经网络计算出输出值.这种计算是所有的训练样本同时进行的 ...