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. webpack 安装vue(两种代码模式compiler 和runtime)

    使用webpack安装vue,import之后,运营项目报错,如下: [Vue warn]: You are using the runtime-only build of Vue where the ...

  2. RabbitMQ官方教程二 Work Queues(GOLANG语言实现)

    RabbitMQ官方教程二 Work Queues(GOLANG语言实现) 在第一个教程中,我们编写了程序来发送和接收来自命名队列的消息. 在这一部分中,我们将创建一个工作队列,该队列将用于在多个wo ...

  3. Appium移动自动化测试-----(五) java-client安装与测试

    前提条件 当你点击这一章时,说明你是打算使用 Java 语言编写 appium 自动化测试脚本的. 1.安装 Java 环境 ,我想这一步你已经搞定了 2.安装 IntelliJ IDEA , 当然, ...

  4. mybatis xml动态语句写法

    mapper.java: /** * @Description: 根据摄像机Id查询出入记录 * @Param: * name 姓名 * monitorId 布控ID * starttime 开始时间 ...

  5. CentOS 安装tab命令补全

    CentOS 安装tab命令补全 1. 安装epel 源 yum -y install epel-release 2. 加快yum速度 yum -y install yum-plugin-fastes ...

  6. shrio学习笔记

    Thymeleaf扩展坐标 <!--thyemleaf对shrio的扩展坐标--> <dependency> <groupId>com.github.thebora ...

  7. Ly与lyon的巅峰对决,描色法

    http://paste.ubuntu.com/14124956/ #include <stdio.h> #include <stdlib.h> struct node { i ...

  8. 【转】linux卸载mysql

    查看是否安装了MySQL执行命令rpm -qa | grep mysql 执行过程[root@localhost ~]# rpm -qa | grep mysqlmysql-community-lib ...

  9. Struts笔记4

    Struts2-拦截器-单个拦截器 自定义拦截器 1.创建一个继承AbstractInterceptor的类 package com.gyf.web.interceptor; import com.o ...

  10. vim 常用命令总结(排版精良,内容优质)

    1. 格式说明 <xxx>:尖括号的含义表示这是一个占位参数,也就是必须有的参数,实际输入的内容是 xxx [xxx]:方括号的含义表示这是一个可选参数,也就是可有可无,实际输入的内容是 ...