New Year and Old Subsequence

第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对。。

然后就gun取看题解了, 用线段树维护dp的值, 然后区间合并求答案。 每个节点保存dp[ i ][ j ]表示, 把当前管理的区间删到

s{2017}中的 s[ i + 1 ] - s[ j - 1 ],最少删几个, 然后合并的时候5 ^ 3合并。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct info {
int a[][];
info() {
memset(a, inf, sizeof(a));
}
void go(char c) {
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
a[i][j] = i == j ? : inf;
if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
}
};
info operator + (const info& x, const info& y) {
info z;
for(int i = ; i < ; i++)
for(int j = i; j < ; j++)
for(int k = i; k < ; k++)
chkmin(z.a[i][j], x.a[i][k] + y.a[k][j]);
return z;
}
info a[N << ];
void build(char* s, int l, int r, int rt) {
if(l == r) {
a[rt].go(s[l]);
return;
}
int mid = l + r >> ;
build(s, lson); build(s, rson);
a[rt] = a[rt << ] + a[rt << | ];
}
info query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) return a[rt];
int mid = l + r >> ;
if(R <= mid) return query(L, R, lson);
else if(L > mid) return query(L, R, rson);
else return query(L, R, lson) + query(L, R, rson);
} int n, q;
char s[N]; int main() {
scanf("%d%d", &n, &q);
scanf("%s", s + );
build(s, , n, );
while(q--) {
int L, R;
scanf("%d%d", &L, &R);
info ans = query(L, R, , n, );
printf("%d\n", ans.a[][] == inf ? - : ans.a[][]);
}
return ;
} /*
*/

Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)的更多相关文章

  1. Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划

    A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...

  2. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

  3. codeforces 629D D. Babaei and Birthday Cake (线段树+dp)

    D. Babaei and Birthday Cake time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  5. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...

  7. [Codeforces 750E]New Year and Old Subsequence

    Description 题库链接 给出一个长度为 \(n\) 的仅包含数字的字符串. \(q\) 次询问,每次询问该串 \([a,b]\) 段内删去几个数能够使其不含 \(2016\) 的子串,但存在 ...

  8. Codeforces 750E 线段树DP

    题意:给你一个字符串,有两种操作:1:把某个位置的字符改变.2:询问l到r的子串最少需要删除多少个字符,使得这个子串含有2017子序列,并且没有2016子序列? 思路:线段树上DP,我们设状态0, 1 ...

  9. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

随机推荐

  1. koa-session 记录当前会话内容

    最近做毕设需要在nodejs服务器下记录当前用户账号,所用的node框架是koa,所以相对应配套的用了koa-session,发现和之前学的session差不多,都是会话级别的. 一.session和 ...

  2. mysql主从复制、redis基础、持久化和主从复制

    一.mysql(mariadb)基础 1.基础命令(centos7操作系统下) 1.启动mysql systemctl start mariadb 2.linux客户端连接自己 mysql -uroo ...

  3. java集合框架综述

    一.集合框架图 简化图: 说明:对于以上的框架图有如下几点说明 1.所有集合类都位于java.util包下.Java的集合类主要由两个接口派生而出:Collection和Map,Collection和 ...

  4. 第七节:语法总结(1)(自动属性、out参数、对象初始化器、var和dynamic等)

    一. 语法糖简介   语法糖也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方 ...

  5. vsftpd启动问题简记

    centos7 能以ipv6方式启动,启动只需修改配置如下 如需同时启动到ipv4跟ipv6,需拷贝配置文件,一份配置中只监听ipv4,一份配置中只监听ipv6 centos6中无法启动到ipv6,错 ...

  6. SpringBoot系列: JdbcTemplate 事务控制

    ============================Spring JdbcTemplate 事务控制============================之前使用 JDBC API 操作, 经常 ...

  7. [物理学与PDEs]第2章第4节 激波 4.1 间断连接条件

    1.  守恒律方程 $$\bex \cfrac{\p f}{\p t}+\cfrac{\p q}{\p x}=0 \eex$$ 在间断线上应满足 ``间断连接条件'': $$\bex [f]\cfra ...

  8. apache中 sed 指定文件中某字符串增加行

    #!/bin/bash #在 servername 域名 字符串后面添加指定字符串 servername=`grep ServerName httpd-vhosts.conf |awk '{print ...

  9. sql server 2008 中的 server profiler 的简单使用

    server profiler 是一个SQL server的 数据库执行语句的监控工具. 登录你需要监控的数据库. 2 .设置要监控进程的PID. 3.设置监控的数据库. 4 . 最后点击运行 就可以 ...

  10. 410 for 循环 运算 改变循环的控制流 死循环 遍历数组 定义方法 有名函数匿名函数 定义函数的方法取值 date math 局部变量 函数 局部与全局变量 次幂/随机数/取绝对值/向上取整/平方根

    for(1.表达式1;2.表达式2;3.表达式3){ 4.循环体语句; } 先执行1 ,在执行2, 表达式, 如果2结果为false,退出循环 如果2是true 执行4 在执行3 执行2 举例打印1- ...