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. Shell脚本运行报错Bad substitution解决办法

    环境Ubuntu18.4.02 脚本运行报错 # sh test.sh test.sh: 2: test.sh: Bad substitution 原因 从 ubuntu 6.10 开始,ubuntu ...

  2. 12点睛Spring4.1-Spring Aware

    12.1 Aware 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源; 使用aware能让我们在应用的任意位置获得spri ...

  3. adb(Andorid Debug Bridge)安装和使用

    以下是adb工具包最新2017Google官方版下载地址: ADB和Fastboot for Windows https://dl.google.com/android/repository/plat ...

  4. mysql的HA及openstack和k8s区别

    数据库HA(Hight Availability) mysql自身支持主主/主备部署,多主(>2),一主多备 galera mariadb集群:多主相互备份 修改容器的启动配置 修改Docker ...

  5. Static和Const的区别

    static static局部变量 将一个变量声明为函数的局部变量,那么这个局部变量在函数执行完成之后不会被释放,而是继续保留在内存中 static 全局变量 表示一个变量在当前文件的全局内可访问 s ...

  6. 小程序JS框架

  7. Django2.2_No installed app with label 'admin'

    版本:py37+django2.2 错误:项目启动时出现,No installed app with label 'admin' 原因:可以发现只要注释掉settings里面的数据库DATABASES ...

  8. Caused by: java.lang.NullPointerException: Location is required

    将 Parent root = FXMLLoader.load(getClass().getResource("xml/MainUI.fxml")); 改为: Parent roo ...

  9. LeetCode 206. 反转链表(Reverse Linked List) 16

    206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...

  10. (6)Spring Boot web开发 --- 错误处理页面

    文章目录 处理时间(`Date`)类型 thymeleaf 页面拼接字符串 映射路径占位符 使用 put.delete 方法 错误处理机制 处理时间(Date)类型 Spring Boot 进行参数绑 ...