Spoj括号分段树

时间:2016-06-29 06:47:06

标签: segment-tree

任何人都可以在我的代码中帮助我。我正在获得西澳大利亚州,而且我无法纠正它 我的代码http://ideone.com/DkrwIg 问题链接:http://www.spoj.com/problems/BRCKTS/

我对我的修改功能有点怀疑。

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
char str[40010];
struct node
{
    int sum;
    int minsum;
}tree[1000005];
void build(int id,int l,int r)
{
    if(r-l<2)
    {
        if(str[l] == '(')
        {
            tree[id].sum = 1;
            tree[id].minsum = 1;
        }
        else
        {
            tree[id].sum = -1;
            tree[id].minsum = -1;
        }
        return;
    }
    int mid = (r+l)/2;
    build(id*2,l,mid);
    build(id*2+1,mid,r);
    tree[id].sum = tree[id*2].sum + tree[id*2+1].sum;
    tree[id].minsum = min(tree[id*2].minsum,tree[id*2].minsum+tree[id*2+1].minsum);
}
void modify(int index,int id,int l,int r)
{
    if(r-l<2)
    {
        tree[id].sum = tree[id].minsum = -tree[id].sum;
        return;
    }
    int mid = (r+l)/2;
    if(index<mid)
    modify(index,id*2,l,mid);
    else
    modify(index,id*2+1,mid,r);
    tree[id].sum = tree[id*2].sum + tree[id*2+1].sum;
    tree[id].minsum = min(tree[id*2].minsum,tree[id*2].minsum+tree[id*2+1].minsum);
}
int main()
{
    int n,k;
    int val;
    int h = 1;
    for(int h=1;h<=10;h++)
    {
        scanf("%d",&n);
        scanf("%s",str);
        build(1,0,n);
        //cout<<"Test "<<h<<" :"<<endl;
        printf("Test %d:\n",h);
        //cin>>k;
        scanf("%d",&k);
        while(k--)
        {
            cin>>val;
            if(!val)
            {
                if(tree[1].sum == 0 && tree[1].minsum == 0)
                {
                    //cout<<"YES"<<endl;
                    printf("YES\n");
                }
                else
                {
                    //cout<<"NO"<<endl;
                    printf("NO\n");
                }
                //cout<<tree[1].sum<<"------------"<<tree[1].minsum<<endl;
            }
            else
            {
                modify(val-1,1,0,n);
            }
        }
    }
    return 0;
}

0 个答案:

没有答案