Skip to content

Instantly share code, notes, and snippets.

@utkarshl
Created January 1, 2013 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save utkarshl/4429155 to your computer and use it in GitHub Desktop.
Save utkarshl/4429155 to your computer and use it in GitHub Desktop.
#include"segment_tree.cpp"
#include"algorithm"
#include"stdio.h"
struct node{
int min,sum;
void merge(node& l,node& r){
sum = l.sum+r.sum;
min = std::min(l.min,l.sum+r.min);
}
};
void flip(node& n){
n.min*=-1;
n.sum=n.min;
}
int main(){
for(int i=0;i<10;i++){
printf("Test %d:\n",i+1);
segtree<node> s;
int n;
scanf("%d",&n);
char inp[n+1];
scanf("%s",inp);
node arr[n+1];
for(int i=0;i<n;i++)
arr[i].min=arr[i].sum=inp[i]=='('?1:-1;
arr[n].min=arr[n].sum=0;
s.init(n,arr,arr[n]);
int m;
scanf("%d",&m);
while(m--){
int k;
scanf("%d",&k);
if(k)
s.update<&flip>(k-1);
else{
node x=s.query(0,100000000);
printf("%s\n",(x.sum==0&&x.min==0)?"YES":"NO");
}
}
}
}
@harshag96
Copy link

can you explain a little bit that how your code is able to solve the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment