zoukankan      html  css  js  c++  java
  • 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 only with correct bracket sequences (abbreviated as CBS).

    Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))":

    • 1st bracket is paired with 8th,
    • 2d bracket is paired with 3d,
    • 3d bracket is paired with 2d,
    • 4th bracket is paired with 7th,
    • 5th bracket is paired with 6th,
    • 6th bracket is paired with 5th,
    • 7th bracket is paired with 4th,
    • 8th bracket is paired with 1st.

    Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported:

    • «L» — move the cursor one position to the left,
    • «R» — move the cursor one position to the right,
    • «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to).

    After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted).

    There are pictures illustrated several usages of operation "D" below.

    All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor.

    Polycarp is very proud of his development, can you implement the functionality of his editor?

    Input

    The first line contains three positive integers nm and p (2 ≤ n ≤ 500 000, 1 ≤ m ≤ 500 000, 1 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even.

    It is followed by the string of n characters "(" and ")" forming the correct bracket sequence.

    Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.

    Output

    Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.

    Examples
    input
    8 4 5
    (())()()
    RDLD
    output
    ()
     
    Note

    In the first sample the cursor is initially at position 5. Consider actions of the editor:

    1. command "R" — the cursor moves to the position 6 on the right;
    2. command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5;
    3. command "L" — the cursor moves to the position 4 on the left;
    4. command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1.

    Thus, the answer is equal to ().

     题意:

      给你一个字符串只包含(),为合法匹配的括号串

      给你一系列的操作LRD

      问你最后这个串变成什么了

    题解:

      每次操作我们用线段树第k大寻找相邻位置对应左右移动

      对于操作就是区间修改了

      可以先用栈预处理这个区间出来

      都能在线段树上操作

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e7+20, M = 1e6+10, mod = 1e9+7;
    
    typedef long long ll;
    
    char s[N],op[N];
    int n,m,p,f[N];
    stack<int > q;
    int l[N],r[N],sum[N],lazy[N];
    void pushdown(int k) {
        if(lazy[k]==-1) return ;
        sum[k<<1] = 0;
        sum[k<<1|1] = 0;
        lazy[k<<1] = 0;
        lazy[k<<1|1] = 0;
        lazy[k] = -1;
        sum[k] = sum[k<<1]+sum[k<<1|1];
    }
    void build(int k,int s,int t) {
        l[k] = s;r[k] = t;
        sum[k] = 0;
        lazy[k] = -1;
        if(s==t) {
            sum[k] = 1;
            return ;
        }
        int mid = (s+t)>>1;
        build(k<<1,s,mid);
        build(k<<1|1,mid+1,t);
        sum[k] = sum[k<<1]+sum[k<<1|1];
    }
    void update(int k,int s,int t) {
        if(lazy[k]!=-1) {
            pushdown(k);
        }
        if(l[k]==s&&r[k]==t) {
            sum[k] = 0;
            lazy[k] = 0;
            return ;
        }
        int mid = (l[k]+r[k])>>1;
        if(t<=mid) {
            update(k<<1,s,t);
        }
        else if(s>mid) update(k<<1|1,s,t);
        else {
            update(k<<1,s,mid);
            update(k<<1|1,mid+1,t);
        }
        sum[k] = sum[k<<1]+sum[k<<1|1];
    }
    int ask(int k,int x) {
        if(lazy[k]!=-1) pushdown(k);
        if(l[k]==x&&r[k]==x) {
            return sum[k];
        }
        int mid = (l[k]+r[k])>>1;
        if(x<=mid) return ask(k<<1,x);
        else return ask(k<<1|1,x);
    }
    
    int ask(int k,int x,int y) {
        if(lazy[k]!=-1) pushdown(k);
        if(l[k]==x&&r[k]==y) {
            return sum[k];
        }
        int mid = (l[k]+r[k])>>1;
        if(y<=mid) {
           return ask(k<<1,x,y);
        }
        else if(x>mid) return ask(k<<1|1,x,y);
        else {
           return  ask(k<<1,x,mid) + ask(k<<1|1,mid+1,y);
        }
        sum[k] = sum[k<<1]+sum[k<<1|1];
    }
    
    int query2(int id, int s, int t, int k){
         if(lazy[id]!=-1) pushdown(id);
          if(s == t){
              return s;
          }
          int mid = (s+t)>>1;
          if(sum[id<<1] >= k) {
                return query2(id<<1, s , mid, k);
          }else {
                return query2(id<<1|1, mid + 1, t, k - sum[id<<1]);
          }
    }
    
    int main() {
        scanf("%d%d%d",&n,&m,&p);
        scanf("%s%s",s+1,op+1);
        for(int i=1;i<=n;i++) {
            if(s[i]=='(') {
                q.push(i);
            }
            else {
                int k = q.top();
                f[i] = k;
                f[k] = i;
                q.pop();
            }
        }
        build(1,1,n);
        for(int i=1;i<=m;i++) {
            if(op[i]=='R') {
                int tmp = ask(1,1,p);
                tmp+=1;
                 p = query2(1,1,n,tmp);
            }
            else if(op[i]=='L') {
                int tmp = ask(1,1,p-1) ;
                  p = query2(1,1,n,tmp);
            }
            else {
                update(1,min(f[p],f[f[p]]),max(f[p],f[f[p]]));
                p = max(f[p],f[f[p]]);
                int tmp = ask(1,1,p) ;
                if(ask(1,p,n)) {
                    p = query2(1,1,n,tmp+1);
                }
                else if(tmp) {
                    p = query2(1,1,n,tmp);
                }
                else break;
            }
        }
        for(int i=1;i<=n;i++) {
            if(ask(1,i)) {
                printf("%c",s[i]);
            }
        }
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    Linux升级内核教程(CentOS7)
    如何更新远程主机上的 Linux 内核
    CentOS在ssh下远程重装系统
    独立服务器远程重装Linux系统
    大公司都有哪些开源项目~~~阿里,百度,腾讯,360,新浪,网易,小米等
    win7旗舰版64位缺失tbb.dll文件
    一文看懂IC芯片生产流程:从设计到制造与封装
    Open WATCOM指南
    eComStation 1.2
    开源网络准入系统(open source Network Access Control system)
  • 原文地址:https://www.cnblogs.com/zxhl/p/5464048.html
Copyright © 2011-2022 走看看