Codeforces 750 E New Year and Old Subsequence
E. New Year and Old Subsequence
思路:线段树维护矩阵乘法。
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb emplace_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
struct Matrix {
int a[5][5];
inline void init() {
memset(a, INF, sizeof a);
}
inline Matrix operator * (const Matrix & rhs) const {
Matrix res;
res.init();
for (int k = 0; k < 5; ++k) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
res.a[i][j] = min(res.a[i][j], a[i][k]+rhs.a[k][j]);
}
}
}
return res;
}
}tree[N<<2];
char s[N];
int n, q, l, r;
inline int push_up(int rt) {
tree[rt] = tree[rt<<1]*tree[rt<<1|1];
}
/* |2|0|1|7| */
void build(int rt, int l, int r) {
if(l == r) {
tree[rt].init();
for (int i = 0; i < 5; ++i) tree[rt].a[i][i] = 0;
if(s[l] == '2') tree[rt].a[0][1] = 0, tree[rt].a[0][0] = 1;
else if(s[l] == '0') tree[rt].a[1][2] = 0, tree[rt].a[1][1] = 1;
else if(s[l] == '1') tree[rt].a[2][3] = 0, tree[rt].a[2][2] = 1;
else if(s[l] == '7') tree[rt].a[3][4] = 0, tree[rt].a[3][3] = 1;
else if(s[l] == '6') tree[rt].a[3][3] = 1, tree[rt].a[4][4] = 1;
return ;
}
int m = l+r >> 1;
build(ls);
build(rs);
push_up(rt);
}
Matrix query(int L, int R, int rt, int l, int r) {
if(L <= l && r <= R) return tree[rt];
int m = l+r >> 1;
Matrix res;
res.init();
for (int i = 0; i < 5; ++i) res.a[i][i] = 0;
if(L <= m) res = res * query(L, R, ls);
if(R > m) res = res* query(L, R, rs);
return res;
}
int main() {
scanf("%d %d", &n, &q);
scanf("%s", s+1);
build(1, 1, n);
while(q--) {
scanf("%d %d", &l, &r);
Matrix res = query(l, r, 1, 1, n);
if(res.a[0][4] == INF) printf("-1\n");
else printf("%d\n", res.a[0][4]);
}
return 0;
}
Codeforces 750 E New Year and Old Subsequence的更多相关文章
- 【codeforces 750E】New Year and Old Subsequence
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】
一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...
- Codeforces Round #651 (Div. 2) D. Odd-Even Subsequence(二分)
题目链接:https://codeforces.com/contest/1370/problem/D 题意 给出一个含有 $n$ 个数的数组 $a$,从中选出 $k$ 个数组成子序列 $s$,使得 $ ...
- Codeforces Round #651 (Div. 2) E. Binary Subsequence Rotation(dp)
题目链接:https://codeforces.com/contest/1370/problem/E 题意 给出两个长为 $n$ 的 $01$ 串 $s$ 和 $t$,每次可以选择 $s$ 的一些下标 ...
- Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value(鸽巢原理)
题目链接:https://codeforces.com/problemset/problem/1365/E 题意 有 $n$ 个元素,定义大小为 $k$ 的集合值为 $\sum2^i$,其中,若集合内 ...
- Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value 贪心
题意:E.Maximum Subsequence Value 题意: 给你n 个元素,你挑选k个元素,那么这个 k 集合的值为 ∑2i,其中,若集合内至少有 max(1,k−2)个数二进制下第 i 位 ...
- Codeforces Round#201(div1) D. Lucky Common Subsequence
题意:给定两个串,求出两个串的最长公共子序列,要求该公共子序列不包含virus串. 用dp+kmp实现 dp[i][j][k]表示以i结尾的字符串和以j结尾的字符串的公共子序列的长度(其中k表示该公共 ...
随机推荐
- 常见问题:MySQL/B+树
平衡二叉树 此前讲红黑树时也提到了平衡二叉树,红黑树和AVL树都是能保证树不退化的平衡二叉树,平衡二叉树采用二分思想组织数据,能大大提高单点查找数据的效率,其组装过程略. 作为对比,此处也列出平衡二叉 ...
- 1. Arduino对第三方开发板的支持
Arduino对第三方开发板也有支持接口,需要手动添加,我们以STM32系列为例,说明添加过程. 目前st的官方提供的一系列板子都支持Arduino,可能是Arduino-IDE更新太慢,目前版本没有 ...
- git push时出现大文件的处理方法
最近在提交项目时出现报错 文件限制只能100M,但是里面有个文件202M,超过了码云的限制. 所以顺手就把这个文件删除了 然后发现还是同样的报错,反复检查目录还是不行,找了资料说,需要git rm 命 ...
- PAT(B) 1044 火星数字(Java)进制转换
题目链接:1044 火星数字 (20 point(s)) 题目描述 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, ...
- PB笔记之数据窗口可编辑的条件
1.列的tab order为0,列不能获得焦点2.dw_control.object.datawindow.readonly="yes"3.DW.Object.<Column ...
- Spring Boot的配置文件-yml文件的集合配置方式
yml集合数据的表示方式: 这种方式是配置普通的字符串—— 如果集合中装的是对象,就这么配置—— 行内配置方式—— map的配置方式——
- javascript之instanceof
定义和用法 instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上. 语法: object instanceof construct ...
- Consul作为SpringCloud配置中心
一.背景介绍 在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等.一句话非常重要. 另外一篇文章也是这样说的,哈哈. Consul 作为Spring 推荐的分布式调度系统其也具备配置中 ...
- 【SoloPi】SoloPi使用1-初次使用,环境准备(Windows版)
下载配置Android SDK路径前往 https://developer.android.com/studio/releases/platform-tools#downloads 下载对应系统版本的 ...
- MLP神经网络实例--手写识别
1.导入MNIST数据集 直接使用fetch_mldata会报错,错误信息是python3.7把fetch_mldata方法移除了,所以需要单独下载数据集从这个网站上下载数据集: https://gi ...