nowcoder wannafly 25 E:01串
E:01 串
分析:
线段树维护转移矩阵。每个节点是一个矩阵,区间内的矩阵乘起来就是答案矩阵。矩阵乘法满足结合律,所以线段树维护。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define Root 1, n, 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
char s[N];
struct Node{
int a[][];
void Calc(int v) {
if (v) a[][] = , a[][] = , a[][] = , a[][] = ;
else a[][] = , a[][] = , a[][] = , a[][] = ;
}
}T[N << ];
Node operator + (const Node &A,const Node &B) {
Node C; memset(C.a, 0x3f, sizeof(C.a));
for (int k = ; k < ; ++k)
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
C.a[i][j] = min(C.a[i][j], A.a[i][k] + B.a[k][j]);
return C;
}
void build(int l,int r,int rt) {
if (l == r) { T[rt].Calc(s[l] - ''); return ; }
int mid = (l + r) >> ;
build(lson); build(rson);
T[rt] = T[rt << ] + T[rt << | ];
}
void update(int l,int r,int rt,int p,int v) {
if (l == r) { T[rt].Calc(v); return ; }
int mid = (l + r) >> ;
if (p <= mid) update(lson, p, v);
else update(rson, p, v);
T[rt] = T[rt << ] + T[rt << | ];
}
Node query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return T[rt];
int mid = (l + r) >> ;
if (R <= mid) return query(lson, L, R);
else if (L > mid) return query(rson, L, R);
else return query(lson, L, R) + query(rson, L, R);
}
int main() {
int n = read();
scanf("%s", s + );
build(Root);
Node ans;
for (int Q = read(); Q--; ) {
int opt = read(), x = read(), y = read();
if (opt == ) {
ans = query(Root, x, y);
printf("%d\n",min(ans.a[][], ans.a[][] + ));
}
else update(Root, x, y);
}
return ;
}
nowcoder wannafly 25 E:01串的更多相关文章
- 1415: 小ho的01串 [字符串]
点击打开链接 1415: 小ho的01串 [字符串] 题目描述 有一个由0和1组成的字符串,它好长呀--------一望无际 恩,说正题,小ho的数学不太好,虽然是学计算机的但是看见0和1也是很头疼的 ...
- JZOJ P1847:找01串
传送门 DP预处理+贪心 首先设$f[i][j]$表示长度为$i$的01串中有不大于$j$个1,然后显然 $f[i][j]=\sum_{k=1} ^{j} C[i][k]$ $C[i][j]=C[i- ...
- 洛谷P2727 01串 Stringsobits
P2727 01串 Stringsobits 24通过 55提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 这题的思路是啥啊!!!跪求- 题目背景 考虑 ...
- C++实现01串排序
题目内容:将01串首先按长度排序,长度相同时,按1的个数从少到多进行排序,1的个数相同时再按ASCII码值排序. 输入描述:输入数据中含有一些01串,01串的长度不大于256个字符. 输出描述:重新排 ...
- 01串(dp)
01串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有“11”子串的这种长度的01串共有多少个, ...
- 【巧妙】【3-21个人赛】Problem C 01串
Problem C Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- NYOJ-252 01串
01串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有"11"子串的这样的长 ...
- NYOJ 252 01串(斐波那契数列变形)
01串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有“11”子串的这种长度的01串共有多少个, ...
- COGS 862. 二进制数01串【dp+经典二分+字符串】
862. 二进制数01串 ★ 输入文件:kimbits.in 输出文件:kimbits.out 简单对比 时间限制:1 s 内存限制:128 MB USACO/kimbits(译 by ...
随机推荐
- ZOJ-3279 Ants 树状数组 + 二分
题目链接: https://cn.vjudge.net/problem/ZOJ-3279 题目大意: 有1到n 那个level 每一个level有a[i]只蚂蚁两种操作 p a b 把第a个level ...
- BeanDefinition的创建(BeanDefinitionHolder的创建)
这个对另一篇章Bean Definition从加载.解析.处理.注册到BeanFactory的过程的扩展. Spring框架中BeanDefinition的继承结构. Spring框架中BeanDef ...
- 【ES6】最常用的es6特性(一)
参考链接: http://www.jianshu.com/p/ebfeb687eb70 http://www.cnblogs.com/Wayou/p/es6_new_features.html 1.l ...
- [转]MVVM架构~mvc,mvp,mvvm大话开篇
MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负 责显示.作为一种新的模式,MVP与MVC有着一 ...
- sys,os加glob加os.path用法
1 sys.path是python的搜索模块的路径集,是一个list 打印下大概长这样 C:\Python27\Lib\idlelib C:\Windows\system32\python27.z ...
- CDN的作用与基本过程
转载请注明出处: leehao.me 或 https://blog.csdn.net/lihao21/article/details/52808747 简介 CDN,Content Distribu ...
- Fiddler模拟Http请求
Fiddler模拟Http请求 一.自定义Http请求:
- tomcat启动错误org.springframework.beans.factory.CannotLoadBeanClassException的解决
tomcat启动时一直报这个错误,但是报错的类确实存在. 清空tomcat,更新maven项目,重配tomcat都没有解决. 最后解决办法: Eclipse环境:Project-->clean ...
- 你真的了解Scrum吗?
敏捷开发是以用户的需求为核心,采用迭代.循序渐进的方法进行软件开发.而Scrum是实现敏捷开发的具体方式之一.然而你对Scrum又了解多少呢? 什么是Scrum Scrum是橄榄球运动的一个专业术语, ...
- jdk1.8换成1.7
电脑中装了jdk1.7,然后又装了1.8, 后来项目需要1.7,就把path环境变量中的java_home改成了1.7. 然后控制台输入java_version,后提示如下: Error: Regis ...