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表示该公共 ...
随机推荐
- Nodejs Client for FastDFS
FastDFS 是分布式文件存储系统.这个项目是FastDFS的NodeJS客户端,用来与FastDFS Server进行交互,进行文件的相关操作.我测试过的server版本是4.0.6. githu ...
- Python3 IO编程之StringIO和BytesIO
StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. 要把str写入StringIO,我们需要先创建一个StringIO,然后像文件一样写入即可 >>> from ...
- INTEL 7代CPU I5 7500 集显HD630 WIN7 64位
HD630 在WIN7 下的硬件ID (在设备管理器 - 显卡 - 属性 中查看): PCI\VEN_8086&DEV_5912&SUBSYS_D0001458&REV_04 ...
- 16、vue引入echarts,划中国地图
vue引入echarts npm install echarts --save main.js引入 import echarts from 'echarts' Vue.prototype.$echar ...
- 解决RedisDesktopManager连接不上redis问题
linux 下安装redis很简单,在此不做赘述 发现linux上启动redis,测试redis使用正常, 但使用RedisDesktopManager却连接不上,报错如下,报错信息显示:当前使用的P ...
- 微服务Consul系列之服务部署、搭建、使用
使用Consul解决了哪些问题 是否在为不同环境来维护不同项目配置而发愁 是否有因为配置的更改导致代码还要进行修改.发布因为客流量大了还要规避开高峰期等到半夜来发布 微服务架构下应用的分解业务系统与服 ...
- 【网络开发】UDP组播接收端解析
UDP组播接收端解析 网络中的一台主机如果希望能够接收到来自网络中其它主机发往某一个组播组的数据报,那么这么主机必须先加入该组播组,然后就可以从组地址接收数据包.在广域网中,还涉及到路由器支持组播路由 ...
- 反馈神经网络Hopfield网络
一.前言 经过一段时间的积累,对于神经网络,已经基本掌握了感知器.BP算法及其改进.AdaLine等最为简单和基础的前馈型神经网络知识,下面开启的是基于反馈型的神经网络Hopfiled神经网络.前馈型 ...
- git rebase VS git merge
git rebase VS git merge 写在前面 如果你不能很好的应用 Git,那么这里为你提供一个非常棒的 Git 在线练习工具 Git Online(回复公众号「工具」,获取更多内容) , ...
- 安装本地 jar 文件
Maven 提供了 maven-install-plugin 可将 jar 文件安装至本地 repository.安装命令如下: mvn install:install-file -Dfile= -D ...