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 ...
随机推荐
- (七)Linux下的关机与重启命令
============================================================================================= 关机与重启命 ...
- 寄存器简介 与 ebp esp
http://www.cnblogs.com/zhuyuanhao/archive/2012/10/16/3262870.html 32位CPU所含有的寄存器有:4个数据寄存器(EAX.EBX.ECX ...
- UVa 10288 - Coupons(数学期望 + 递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- HDU 1162Eddy's picture(MST问题)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Blocking Master Example QT 自带 的 serial 即 串口 例子
1.官方解释文档:http://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html Blocking Master shows how to ...
- 大话Linux内核中锁机制之信号量、读写信号量
大话Linux内核中锁机制之信号量.读写信号量 在上一篇博文中笔者分析了关于内存屏障.读写自旋锁以及顺序锁的相关内容,本篇博文将着重讨论有关信号量.读写信号量的内容. 六.信号量 关于信号量的内容,实 ...
- page_address()函数分析--如何通过page取得虚拟地址
由于X86平台上面,内存是划分为低端内存和高端内存的,所以在两个区域内的page查找对应的虚拟地址是不一样的. 一. x86上关于page_address()函数的定义 在include/linux/ ...
- 随机获取指定范围内N个不重复数字
/// <summary> /// 随机获取指定范围内N个不重复数字 /// </summary> /// <param name="min"> ...
- C# 数组集合分页 Skip Take
var input=new input(); var personList= new List<Person>(); //一个查询集合 var Total = personList.Cou ...