Codeforces 380C. Sereja and Brackets【线段树】

题目大意:

给出一串括号,有m个查询(包含a,b)问区间[a,b]之间有多少个匹配的括号

做法:

处理两个数组r[i](代表从1到i之间有多少个已匹配的右括号),l[i](代表从1到i之间有多少个没有匹配的左括号)。我们要算[a,b]之间的匹配的括号数,首先用r[b]-r[a-1],但是遮掩更有可能a到b之间的右括号是在[1,a-1]之间被匹配的,那么再减去l[a-1],但是减去的这一部分中,也有可能是在[b+1,len]这个区间中被匹配,要加上这一部分不该减去的数值,那么只需要在加上min(l[a]~l[b])即可。区间最小值,那就用线段树吧

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1000010
using namespace std;
#define L(x) (x)<<1
#define R(x) (x)<<1|1
int m,len,r[N],l[N];
char bra[N];
struct node
{
    int ll,rr,v;
}t[N*6];
int bulid(int x,int y,int idx)
{
    t[idx].ll=x,t[idx].rr=y;
    if(x==y) return t[idx].v=l[x];
    int mid=(x+y)/2;
    return t[idx].v=min(bulid(x,mid,L(idx)),bulid(mid+1,y,R(idx)));
}
int query(int x,int y,int idx)
{
    if(x==t[idx].ll && y==t[idx].rr) return t[idx].v;
    int mid=(t[idx].ll+t[idx].rr)/2;
    if(x>mid) return query(x,y,R(idx));
    if(y<=mid) return query(x,y,L(idx));
    return min(query(x,mid,L(idx)),query(mid+1,y,R(idx)));
}
int main()
{
    scanf("%s",bra);
    len=strlen(bra);
    int tmp=0;
    for(int i=1;i<=len;i++)
    {
        r[i]=r[i-1];
        if(bra[i-1]=='(') tmp++;
        else if(tmp) r[i]++,tmp--;
        l[i]=tmp;
    }
    bulid(1,len,1);
    scanf("%d",&m);
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(a==b) {cout<<0<<endl;continue;}
        int ans=min(r[b]-r[a-1],r[b]-r[a-1]-l[a-1]+query(a,b,1));
        cout<<ans*2<<endl;
    }
    return 0;
}
时间: 2024-07-30 10:15:32

Codeforces 380C. Sereja and Brackets【线段树】的相关文章

CodeForces 380C Sereja and Brackets(扫描线+树状数组)

[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括号,其匹配的左括号是固定的, 我们保存每个右括号匹配的左括号位置, 对区间询问进行线扫描,将扫描的区间右端点及其之前所有的右括号对应的左括号位置做标记, 只要查询询问区间的标记个数就是答案,这个可以用树状数组维护. [代码] #include <cstdio> #include <algor

Codeforces 444C DZY Loves Colors(线段树)

题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是修改区间上l到r上面德值为x,2是询问l到r区间总的修改值. 解题思路:线段树模板题. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 5*1e5; typedef long lo

Codeforces 57B Martian Architecture 暴力||线段树

题目链接:点击打开链接 题意:n长的序列(初始全为0) m个操作 k个查询 下面m个操作[l,r] h 代表 a[l] +=h; a[l+1] += h+i; a[l+i] += h+i;  l<=i<=r 然后问k个位置的和 因为k<=100 所以直接暴力也可以 ----------------------- 如果k<=100000 也是可以做的 只需要给区间记录一个标记lazy,表示从左端点开始 l, l+1, l+i ··· l+r 而向下更新时, 左区间则直接更新, 右区间

codeforces 487B B. Strip(rmq+线段树+二分)

题目链接: codeforces 487B 题目大意: 给出一个序列,要把序列划分成段,每一段最少有L个元素,段中的最大元素和最小元素之差不大于s,问划分的段的最少的数量是多少. 题目分析: 首先用rmq维护区间最大值和区间最小值. 然后按顺序扫描数组,线段树维护的数组,每个记录当前点作为最后一个点的前i个点划分的最小的段数,那么每次更新就是二分找到可以转移到我的最远距离,然后再选取与我距离大于l的那部分,取最小值即可. 最终结果就是线段树维护的数组的最后一个位置的元素的值. AC代码: #in

codeforces 482B. Interesting Array【线段树区间更新】

题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val.就是区间l---r上的与的值为val,最后问你原来的数组是多少?如果不存在输出no 分析:分析发现要满足所有的区间,而一个点上假如有多个区间的话,这个点的值就是所有区间或的值,因为只有这样才能满足所有区间的,把所有位上的1都保存下来了,那么可以发现用线段树来维护,但是那么怎么判断满不满足条件呢?可以也用线段树,更新了之后在整个维护一遍看看满不满足题意,如

Codeforces GYM 100114 D. Selection 线段树维护DP

D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description When selecting files in an application dialog, Vasya noted that he can get the same selection in different ways. A simple mouse click selects a sing

Codeforces 834D The Bakery - 动态规划 - 线段树

Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery. Soon the expenses started to overcome the income, so Slastyona decid

Codeforces 486E LIS of Sequence(线段树+LIS)

题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组,现在要确定每个位置上的数属于哪一种类型. 解题思路:先求出每个位置选的情况下的最长LIS,因为开始的想法,所以求LIS直接用线段树写了,没有改,可以用 log(n)的算法直接求也是可以的.然后在从后向前做一次类似LIS,每次判断A[i]是否小于f[dp[i]+1],这样就可以确定该位 置是否属于LIS序列.然后为第三类的则说明dp[i] = k的只有一个满足. #include <cstdio>

CodeForces 600E Lomsat gelral(线段树合并)

题目链接:http://codeforces.com/problemset/problem/600/E You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the