1

can anyone help me in my code. I am getting WA and m not able to rectify it plzzz my code http://ideone.com/DkrwIg problem link : http://www.spoj.com/problems/BRCKTS/

i am a bit doubtful in my modification function.

#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;
}
Akshit Verma
  • 77
  • 2
  • 8
  • 1
    Welcome to SO! Could you explain what a "WA" is? Just for a dummy like me. Also include the problem directly and understandable, external links are generally not appreciated. Please edit the question, do not answer here in the "comments" – Stefan Hegny Jun 29 '16 at 06:49

0 Answers0