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的更多相关文章

  1. 【codeforces 750E】New Year and Old Subsequence

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

  4. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

  5. Codeforces Round #651 (Div. 2) D. Odd-Even Subsequence(二分)

    题目链接:https://codeforces.com/contest/1370/problem/D 题意 给出一个含有 $n$ 个数的数组 $a$,从中选出 $k$ 个数组成子序列 $s$,使得 $ ...

  6. Codeforces Round #651 (Div. 2) E. Binary Subsequence Rotation(dp)

    题目链接:https://codeforces.com/contest/1370/problem/E 题意 给出两个长为 $n$ 的 $01$ 串 $s$ 和 $t$,每次可以选择 $s$ 的一些下标 ...

  7. Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value(鸽巢原理)

    题目链接:https://codeforces.com/problemset/problem/1365/E 题意 有 $n$ 个元素,定义大小为 $k$ 的集合值为 $\sum2^i$,其中,若集合内 ...

  8. Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value 贪心

    题意:E.Maximum Subsequence Value 题意: 给你n 个元素,你挑选k个元素,那么这个 k 集合的值为 ∑2i,其中,若集合内至少有 max(1,k−2)个数二进制下第 i 位 ...

  9. Codeforces Round#201(div1) D. Lucky Common Subsequence

    题意:给定两个串,求出两个串的最长公共子序列,要求该公共子序列不包含virus串. 用dp+kmp实现 dp[i][j][k]表示以i结尾的字符串和以j结尾的字符串的公共子序列的长度(其中k表示该公共 ...

随机推荐

  1. Docker 安装运行MySQL

    1.镜像主页 https://hub.docker.com/_/mysql 2.拉取5.7版本 docker pull mysql:5.7 3.或者拉取最新8.x版本 docker pull mysq ...

  2. servlet中ServletContainerInitializer的简单说明

    根据官方文档到的说明 public interface ServletContainerInitializer Interface which allows a library/runtime to ...

  3. 最小二乘求解常数k使得kx=y(x,y为列向量)

    直接求解法 取范数 \[ E(k)=\|kx-y\|^{2}\\ \] 构建最小二乘得出 \[ \arg \min (E(k))=k^2x^Tx+y^Ty-2x^Tyk \] 对k求导有 \[ 2x^ ...

  4. LeetCode 112. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  5. [SourceTree] - 使用内置 Git 克隆项目出现 templates not found 问题之解决

    背景 使用 SourceTree 克隆 Asp.Net Core 项目失败. 错误 warning: templates not found C:\Program Files\Git\share\gi ...

  6. [C++] 习题 2.15 实现简单环形队列

    目录 前置技能 环形队列 具体实现 设计一个环形队列,用front和rear分别作为队头和队尾指针,另外用一个tag表示队列是空 ( 0 ) 还是不空 ( 1 ),这样就可以用front==rear作 ...

  7. yzoj 2377 颂芬梭哈 题解

    题意 Alice 和 Mukyu 最近偶然得到了一本写有一种叫做梭哈的扑克游戏的规则的说明书(名为<C████████nd>,中间部分被涂掉了),据其所述,梭哈是一种使用黑桃.红心.梅花. ...

  8. iTunes向ipad传影片

    iTunes向ipad传影片(方法一) 在电脑上用itunes传视频到ipad-百度经验 iTunes向ipad传影片(方法二)

  9. AS3.0 位图(BMP)解析类

    /** * *-----------------------------* * | *** BMP格式解析类 *** | * *-----------------------------* * * 编 ...

  10. python装饰器无参及有参案例

    装饰器无参和有参案例: