fork download
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. const int N=4e5;
  4. string str;
  5. int node[4*N+5];
  6. void build(int i , int j , int index)
  7. {
  8. if(i==j)
  9. {
  10. if(str[i]=='(')
  11. {
  12. node[index]=1;
  13. }
  14. else
  15. {
  16. node[index]=-1;
  17. }
  18. return ;
  19. }
  20. int mid=(i+j)/2;
  21. build(i,mid,2*index);
  22. build(mid+1,j,2*index+1);
  23. node[index]=node[2*index]+node[2*index+1];
  24. return ;
  25. }
  26. void update(int i , int j , int index, int k)
  27. {
  28. if(j<k||i>k)
  29. return ;
  30. if(i==j&&i==k)
  31. {
  32. if(node[index]==1)
  33. node[index]=-1;
  34. else
  35. node[index]=1;
  36. return ;
  37. }
  38. int mid=(i+j)/2;
  39. update(i,mid,2*index,k);
  40. update(mid+1,j,2*index+1,k);
  41. node[index]=node[2*index]+node[2*index+1];
  42. return ;
  43. }
  44. int main ()
  45. {
  46. ios_base::sync_with_stdio(false);
  47. cin.tie(NULL);
  48. int t =10;
  49. int a=1;
  50. while(t--)
  51. {
  52. int n ;
  53. cin >> n ;
  54. cin >> str ;
  55. build(0,n-1,1);
  56. cout<<"TEST "<<a<<":"<<"\n";
  57. int m ;
  58. cin >> m ;
  59. while(m--)
  60. {
  61. int k;
  62. cin >> k ;
  63. if(k==0)
  64. {
  65. if(node[1]==0&&node[2]>=0&&node[3]<=0)
  66. cout<<"YES"<<"\n";
  67. else
  68. cout<<"NO"<<"\n";
  69. }
  70. else
  71. update(0,n-1,1,k-1);
  72. }
  73. a++;
  74. }
  75. }
Success #stdin #stdout 0s 5540KB
stdin
16
((()(())()()()()
6
0
14
0
13
14
0
1
(
0
1
(
0
1
(
0
1
(
0
1
(
0
1
(
0
1
(
0
1
(
0
1
(
0
stdout
TEST 1:
NO
NO
YES
TEST 2:
TEST 3:
TEST 4:
TEST 5:
TEST 6:
TEST 7:
TEST 8:
TEST 9:
TEST 10: