fork download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6.  
  7. class BRCKTS {
  8. static int a[];
  9. public static void main(String[] args) throws IOException {
  10. // TODO Auto-generated method stub
  11. Reader r=new Reader();
  12. StringBuilder sb=new StringBuilder();
  13. int i=1;
  14. while(true){
  15.  
  16. String sr=r.readLine();
  17. if(sr==null || sr.equalsIgnoreCase(""))break;
  18. sb.append("Test "+(i++)+":\n");
  19. int n=Integer.parseInt(sr);
  20. //boolean odd=false;
  21. //if((n&1)==1)odd=true;
  22. a=new int[n+1];
  23. String s=r.readLine();
  24. for(int j=1;j<=n;j++){
  25. a[j]=s.charAt(j-1)=='('?1:-1;
  26. }
  27. Segment_Tree st=new Segment_Tree(1, n);
  28. //Segment_Tree.print_postOrder(st);
  29. int m=r.nextInt();
  30.  
  31. for(int j=0;j<m;j++){
  32. int q=r.nextInt();
  33.  
  34. if(q==0){
  35.  
  36. // else if(st.sum==0 && st.min_sum==0)sb.append("YES\n");
  37. if(st.number_of_closed_unbalanced==0 && st.number_of_open_unbalanced==0 )sb.append("YES\n");
  38. else sb.append("NO\n");
  39. }
  40. else st.update(q);
  41. //System.out.println(st.sum+" "+st.min_sum);
  42. //System.out.println("H");
  43. }
  44. }
  45. pr.write(sb.toString());
  46. pr.flush();
  47. }
  48. static class Segment_Tree{
  49. int start,end,number_of_open_unbalanced,number_of_closed_unbalanced;
  50. Segment_Tree left_child,right_child;
  51. //int sum,min_sum;
  52. public Segment_Tree(){};
  53. public Segment_Tree(int a,int b){
  54. start=a;end=b;
  55. if(start!=end){
  56. int mid=(start+end)>>1;
  57. left_child=new Segment_Tree(a,mid);
  58. right_child=new Segment_Tree(mid+1,b);
  59. join(this,left_child,right_child);
  60. }
  61. else {
  62. //sum=min_sum=BRCKTS.a[start]==1?1:-1;
  63. if(BRCKTS.a[start]==1)
  64. number_of_open_unbalanced=1;
  65. else number_of_closed_unbalanced=1;
  66. }
  67. }
  68. public void join(Segment_Tree parent,Segment_Tree leftchild,Segment_Tree rightchild){
  69. //parent.sum=leftchild.sum+rightchild.sum;
  70. //parent.min_sum=Math.min(leftchild.min_sum,rightchild.min_sum+leftchild.sum);
  71. parent.number_of_closed_unbalanced=leftchild.number_of_closed_unbalanced;
  72. parent.number_of_open_unbalanced=rightchild.number_of_open_unbalanced;
  73. int unbala=rightchild.number_of_closed_unbalanced-leftchild.number_of_open_unbalanced;
  74. if(unbala>0){
  75. parent.number_of_closed_unbalanced+=unbala;
  76. }
  77. else parent.number_of_open_unbalanced+=Math.abs(unbala);
  78. }
  79. public void update(int index){
  80. if(start==index && end==index){
  81.  
  82. if(number_of_open_unbalanced==1){
  83. number_of_closed_unbalanced=1;
  84. number_of_open_unbalanced=0;
  85. }
  86. else {
  87. number_of_closed_unbalanced=0;
  88. number_of_open_unbalanced=1;
  89. }
  90. //sum=min_sum=min_sum==1?-1:1;
  91. return ;
  92. }
  93. int mid=(start+end)>>1;
  94. if(index<=mid)left_child.update(index);
  95. else if(index>mid)right_child.update(index);
  96. join(this, left_child, right_child);
  97. }
  98. public static void print_inOrder(Segment_Tree parent){
  99. if(parent==null)return;
  100. else{
  101. print_inOrder(parent.left_child);
  102. System.out.println(parent.number_of_open_unbalanced+" "+parent.number_of_closed_unbalanced);
  103. print_inOrder(parent.right_child);
  104. }
  105. }
  106. public static void print_postOrder(Segment_Tree parent){
  107. if(parent==null)return;
  108. else{
  109. print_postOrder(parent.left_child);
  110. print_postOrder(parent.right_child);
  111. System.out.println(parent.number_of_open_unbalanced+" "+parent.number_of_closed_unbalanced);
  112. }
  113. }
  114. }
  115. static class Reader {
  116. final private int BUFFER_SIZE = 1 << 16;
  117. private DataInputStream din;
  118. private byte[] buffer;
  119. private int bufferPointer, bytesRead;
  120.  
  121. public Reader() {
  122. din = new DataInputStream(System.in);
  123. buffer = new byte[BUFFER_SIZE];
  124. bufferPointer = bytesRead = 0;
  125. }
  126.  
  127. public Reader(String file_name) throws IOException {
  128. din = new DataInputStream(new FileInputStream(file_name));
  129. buffer = new byte[BUFFER_SIZE];
  130. bufferPointer = bytesRead = 0;
  131. }
  132.  
  133. public String readLine() throws IOException {
  134. byte[] buf = new byte[64];
  135. int cnt = 0, c;
  136. while ((c = read()) != -1) {
  137. if (c == '\n')
  138. break;
  139. buf[cnt++] = (byte) c;
  140. }
  141. return new String(buf, 0, cnt);
  142. }
  143.  
  144. public int nextInt() throws IOException {
  145. int ret = 0;
  146. byte c = read();
  147. while (c <= ' ')
  148. c = read();
  149. boolean neg = (c == '-');
  150. if (neg)
  151. c = read();
  152. do {
  153. ret = ret * 10 + c - '0';
  154. } while ((c = read()) >= '0' && c <= '9');
  155. if (neg)
  156. return -ret;
  157. return ret;
  158. }
  159.  
  160. public long nextLong() throws IOException {
  161. long ret = 0;
  162. byte c = read();
  163. while (c <= ' ')
  164. c = read();
  165. boolean neg = (c == '-');
  166. if (neg)
  167. c = read();
  168. do {
  169. ret = ret * 10 + c - '0';
  170. } while ((c = read()) >= '0' && c <= '9');
  171. if (neg)
  172. return -ret;
  173. return ret;
  174. }
  175.  
  176. public double nextDouble() throws IOException {
  177. double ret = 0, div = 1;
  178. byte c = read();
  179. while (c <= ' ')
  180. c = read();
  181. boolean neg = (c == '-');
  182. if (neg)
  183. c = read();
  184. do {
  185. ret = ret * 10 + c - '0';
  186. } while ((c = read()) >= '0' && c <= '9');
  187. if (c == '.')
  188. while ((c = read()) >= '0' && c <= '9')
  189. ret += (c - '0') / (div *= 10);
  190. if (neg)
  191. return -ret;
  192. return ret;
  193. }
  194.  
  195. private void fillBuffer() throws IOException {
  196. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  197. if (bytesRead == -1)
  198. buffer[0] = -1;
  199. }
  200.  
  201. private byte read() throws IOException {
  202. if (bufferPointer == bytesRead)
  203. fillBuffer();
  204. return buffer[bufferPointer++];
  205. }
  206.  
  207. public void close() throws IOException {
  208. if (din == null)
  209. return;
  210. din.close();
  211. }
  212. }
  213.  
  214. }
  215. /*
  216. 12
  217. (()(()(())))
  218. 16
  219. 0
  220. 1
  221. 3
  222. 0
  223. 4
  224. 5
  225. 6
  226. 0
  227. 10
  228. 0
  229. 2
  230. 0
  231. 1
  232. 3
  233. 4
  234. 0
  235. */
Success #stdin #stdout 0.08s 380160KB
stdin
14
)(()()(()())))
3
0
1
0

stdout
Test 1:
NO
YES