hdu 5217 Brackets(線段樹)

題意:給一串括號,有2個操作,1。翻轉某個括號。2。查詢某段區間內未匹配的括號(或者說跟他匹配的在區間外面),第k個未匹配的括號是第幾個。

做法:我們可以把一段區間的括號匹配情況總結爲)(,即左邊的右括號和右邊的左括號,其餘的自然都已經匹配了,這樣就可以利用線段樹去合併。對於操作1單點更新即可。操作2就麻煩很多了。我YY了一個做法,把這個區間有關的點(也就是線段樹內區間查詢的點)給拿出來,重新建一顆小的線段樹(因爲點數不超過2*logn個),然後從頂往下查詢第k個的是哪個區間第幾個(慢慢分類討論吧。。)。到葉節點就可以知道是哪個區間的第幾個是答案,然後再到原樹上找,同樣的方法,到葉節點了,葉節點的編號即是答案,我在拿出這些區間的時候記錄了在原樹的標號,所以後面2個操作都可以寫成非遞歸的,可以減少點常數。

AC代碼:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b; }
template<class T> inline T Min(T a, T b){ return a < b ? a : b; }
const int maxn = 200000+10;
struct SegTree
{
    int x,y;
    int flag;
}T[maxn<<2];
char s[maxn];

void pushup(int rt)
{
    T[rt].x = T[rt<<1].x;
    T[rt].y = T[rt<<1|1].y;
    if(T[rt<<1].y > T[rt<<1|1].x) T[rt].y += T[rt<<1].y-T[rt<<1|1].x;
    else T[rt].x += T[rt<<1|1].x-T[rt<<1].y;
}

void build(int l, int r, int rt)
{
    T[rt].x = T[rt].y = T[rt].flag = 0;
    if(l == r)
    {
        T[rt].flag = l;
        if(s[l] == ')') T[rt].x = 1;
        else T[rt].y = 1;
        return;
    }
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int L, int l, int r, int rt)
{
    if(l == r)
    {
        T[rt].x ^= 1;
        T[rt].y ^= 1;
        return;
    }
    int mid = (l+r)>>1;
    if(L <= mid) update(L,lson);
    else update(L,rson);
    pushup(rt);
}

struct node
{
    int x,y,rt;
    node(){}
    node(int _x, int _y, int _rt):x(_x),y(_y),rt(_rt){}
}no[45];

int nct;

void get(int L, int R, int l, int r, int rt)
{
    if(L <= l && R >= r)
    {
        no[++nct] = node(T[rt].x,T[rt].y,rt);
        return;
    }
    int mid = (l+r)>>1;
    if(L <= mid) get(L,R,lson);
    if(R > mid) get(L,R,rson);
}

node tree[40<<2];
void pushupsmall(int rt)
{
    tree[rt].x = tree[rt<<1].x; tree[rt].y = tree[rt<<1|1].y;
    if(tree[rt<<1].y > tree[rt<<1|1].x) tree[rt].y += tree[rt<<1].y-tree[rt<<1|1].x;
    else tree[rt].x += tree[rt<<1|1].x-tree[rt<<1].y;
}

void buildsmall(int l, int r, int rt)
{
    tree[rt].rt = 0;
    if(l == r)
    {
        tree[rt] = no[l];
        return;
    }
    int mid = (l+r)>>1;
    buildsmall(lson);
    buildsmall(rson);
    pushupsmall(rt);
}

pair<int,int> getans(int ge)
{
    int pos = 1;
    while(tree[pos].rt == 0)
    {
        if(ge <= tree[pos].x)
        {
            if(ge <= tree[pos<<1].x) pos = pos<<1;
            else
            {
                ge = ge-tree[pos<<1].x+tree[pos<<1].y;
                pos = pos<<1|1;
            }
        }
        else
        {
            ge -= tree[pos].x;
            if(tree[pos<<1].y > tree[pos<<1|1].x)
            {
                if(ge <= tree[pos<<1].y-tree[pos<<1|1].x)
                {
                    ge += tree[pos<<1].x;
                    pos = pos<<1;
                }
                else
                {
                    ge -= tree[pos<<1].y-tree[pos<<1|1].x;
                    ge += tree[pos<<1|1].x;
                    pos = pos<<1|1;
                }
            }
            else
            {
                ge += tree[pos<<1|1].x;
                pos = pos<<1|1;
            }
        }
    }
    return mp(ge,tree[pos].rt);
}

int solve(int ge, int pos)
{
    while(T[pos].flag == 0)
    {
        if(ge <= T[pos].x)
        {
            if(ge <= T[pos<<1].x) pos = pos<<1;
            else
            {
                ge = ge-T[pos<<1].x+T[pos<<1].y;
                pos = pos<<1|1;
            }
        }
        else
        {
            ge -= T[pos].x;
            if(T[pos<<1].y > T[pos<<1|1].x)
            {
                if(ge <= T[pos<<1].y-T[pos<<1|1].x)
                {
                    ge += T[pos<<1].x;
                    pos = pos<<1;
                }
                else
                {
                    ge -= T[pos<<1].y-T[pos<<1|1].x;
                    ge += T[pos<<1|1].x;
                    pos = pos<<1|1;
                }
            }
            else
            {
                ge += T[pos<<1|1].x;
                pos = pos<<1|1;
            }
        }
    }
    return T[pos].flag;
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        scanf("%d%d",&n,&q);
        scanf("%s",s+1);
        build(1,n,1);
//        cout<<T[1].x<<" "<<T[1].y<<endl;
        while(q--)
        {
            int ha,x,l,r,k;
            scanf("%d",&ha);
            if(ha == 1)
            {
                scanf("%d",&x);
                update(x,1,n,1);
            }
            else
            {
                scanf("%d%d%d",&l,&r,&k);
                nct = 0;
                get(l,r,1,n,1);
                buildsmall(1,nct,1);
                if(tree[1].x+tree[1].y < k)
                {
                    printf("-1\n");
                    continue;
                }
                pair<int,int> tmp = getans(k);
                printf("%d\n",solve(tmp.first,tmp.second));
            }
        }
    }
    return 0;
}


發佈了187 篇原創文章 · 獲贊 9 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章