codeforces div2 603 E. Editor(线段树)
题目链接:https://codeforces.com/contest/1263/problem/E
题意:一个编译器,每次输入一些字符,R表示光标右移,L表示光标左移,然后有一些左括号( 和 右括号 ),每次会询问当前输入的数据的括号是否合法,如果不合法输出-1,如果合法输出最大合法的括号对数,合法的括号就是()这种形式的。
思路:大致思路是用线段树维护区间一个区间前缀和,初始化前缀和为0。遇到单点更新,(让管辖区间+1,)就让管辖区间-1,,判断是否是合法括号需要判断区间最小值是否为0,且保证1到n区间的前缀和为0(画图思考一下),满足两种条件的同时才能说明此为合法括号序列,最终查询区间最大值就是最大匹配的括号对数(画图思考)。
代码:
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn = 1e6+;
struct node{
int l,r;
int Min,Max;
int val;
int lazy;
}tree[maxn*+];
void pushdown(int index){
if(tree[index].lazy){
tree[index<<].val += (tree[index<<].r-tree[index<<].l+)*tree[index].lazy;
tree[index<<|].val +=(tree[index<<|].r-tree[index<<|].l+)*tree[index].lazy;
tree[index<<].Max += tree[index].lazy;
tree[index<<|].Max += tree[index].lazy;
tree[index<<].Min += tree[index].lazy;
tree[index<<|].Min += tree[index].lazy;
tree[index<<].lazy += tree[index].lazy;
tree[index<<|].lazy += tree[index].lazy;
tree[index].lazy = ;
}
}
void pushup(int index){
tree[index].val = tree[index<<].val+tree[index<<|].val;
tree[index].Max = max(tree[index<<].Max,tree[index<<|].Max);
tree[index].Min = min(tree[index<<].Min,tree[index<<|].Min);
} void build(int l,int r,int index){//建树
tree[index].l = l,tree[index].r = r,tree[index].lazy = ;
if(l == r){
//scanf("%d",&tree[index].val);
tree[index].val = ;
tree[index].Max = tree[index].Min = tree[index].val;
return;
}
int mid = (l+r)>>;//
build(l,mid,index<<);
build(mid+,r,index<<|);
pushup(index);
}
void update(int l,int r,int index,int val){//区间修改
if(l <= tree[index].l && r >= tree[index].r){
tree[index].val += (tree[index].r-tree[index].l+)*val;
tree[index].Max += val;
tree[index].Min += val;
tree[index].lazy += val;//延时标记
return ;
}
pushdown(index);
int mid = (tree[index].l+tree[index].r)>>;
if(l <= mid){
update(l,r,index<<,val);
}
if(r > mid){
update(l,r,index<<|,val);
}
pushup(index);
} LL query_range(int l,int r,int index){//区间查询
if(l <= tree[index].l && r >= tree[index].r){
return tree[index].val;
//return tree[index].mn;
}
pushdown(index);
int mid = (tree[index].l+tree[index].r)>>;
LL res = ;
LL Max = ;
LL Min = inf;
if(l <= mid){
res += query_range(l,r,index<<);
}
if(r > mid){
res += query_range(l,r,index<<|);
}
return res;
} LL query_max(int l,int r,int index){//最大值查询
if(l<= tree[index].l && r>= tree[index].r ){
return tree[index].Max;
}
pushdown(index);
int mid = (tree[index].l + tree[index].r )>>;
LL ans = ;
LL MAX = ,MIN = inf;
if(l <= mid) MAX = max(query_max(l,r,index<<),MAX);
if(r > mid) MAX = max(query_max(l,r,index<<|),MAX);
return MAX;
}
LL query_min(int l,int r,int index){//最小值查询
if(l<= tree[index].l && r>= tree[index].r ){
return tree[index].Min;
}
pushdown(index);
int mid = (tree[index].l + tree[index].r )>>;
LL ans = ;
LL MAX = ,MIN = inf;
if(l <= mid) MIN = min(query_min(l,r,index<<),MIN);
if(r > mid) MIN = min(query_min(l,r,index<<|),MIN);
return MIN;
}
string s;
LL ss[maxn];
int main(){
int n;
scanf("%d",&n);
n = n+;
build(,n,);
cin>>s;
int pos = ;
vector<int> ans;
for(int i = ;i<s.length() ;i++){
if(s[i] == '('){
update(pos,n,,-ss[pos]);
ss[pos] = ;
}
else if(s[i] == ')'){
update(pos,n,,--ss[pos]);
ss[pos] = -;
}
else if(s[i] == 'R'){
pos++;
}
else if(s[i] == 'L'){
if(pos>) pos--;
}
else{
if(ss[pos] == ){
update(pos,n,,-);
}
else if(ss[pos] == -){
update(pos,n,,);
}
ss[pos] = ;
}
if(query_min(,n,) == && query_range(n,n,) == ){
ans.push_back(query_max(,n,));
}
else{
ans.push_back(-);
}
}
for(int i = ;i<ans.size() ;i++){
cout<<ans[i]<<" ";
}
//cout<<endl;
return ;
}
codeforces div2 603 E. Editor(线段树)的更多相关文章
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟
E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works o ...
- Codeforces #504(div1+div2) 1023D Array Restoration(线段树)
题目大意:给你一个数组,数组是经过q次区间覆盖后的结果,第i次覆盖是把区间内的值赋值为i,其中有若干个地方数值未知(就是0),让你判断这个数组是否可以经过覆盖后得到的,如果可以,输出任意一种可行数组. ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
随机推荐
- php 常用数学函数
函数 描述 实例 输入 输出 abs() 求绝对值 $abs = abs(-4.2); //4.2 数字 绝对值数字 ceil() 进一法取整 echo ceil(9.999); // 10 浮点数 ...
- ElementUI的Table-column_render-header自定义表头
ElementUI的Table表格,官方网站上提供了很多样式,但是在日常开发中还会碰到各种情况,显然官方提供的是不能满足需求的.那么,我们就根据自己的需求对table进行改造. 先丢出关于Table的 ...
- linux C++ 读取mysql结果保存
c++读取mysql数据库结果保存 #include <fstream> #include <iomanip> #include <iostream> #inclu ...
- Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- 从0开始学算法--排序(1.12c++利用标准库排序)
1,简单数组按升序排序 sort(a,a+n); #include <algorithm> #include <iostream> #include <cstring&g ...
- HDU-1506 Largest Rectangle in a Histogram【单调栈】
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- 在bootstrap的column中的formatter里不能传递row参数吗?
row 是一个对象 你需要把它转成字符串用JSON.stringify(row),这样就是字符串了,但是还有问题,你需要给加密一下encodeURI(JSON.stringify(row)),然后方 ...
- 牛客CSP-S提高组赛前集训营3 赛后总结
货物收集 二分答案.复杂度\(O(n\log n)\). 货物分组 用费用提前计算的思想,考虑用一个新的箱子来装货物会发生什么. 显然费用会加上后面的所有货物的总重. \(60\)分的\(O(n^2) ...
- Vue ui创建项目
vue-cli 3.0 版本为我们提供了集 创建.管理.分析 为一体的可视化界面vue UI,一个可视化项目管理器 一.打开终端,安装最新vue-cli npm install -g @vue/cli ...
- 未安装Oracle数据库,使用PL\SQL Developer连接远程数据库解决方案
使用PL/SQL远程连接Oracle服务器 背景:本地未安装oracle数据库服务器,希望远程连接Oracle服务器 1.下载oracle数据库客户端 下载64位windows的instantclie ...