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. int m=r.nextInt();
  29.  
  30. for(int j=0;j<m;j++){
  31. int q=r.nextInt();
  32.  
  33. if(q==0){
  34. if(odd)sb.append("NO\n");
  35. else if(st.sum==0 && st.min_sum==0)sb.append("YES\n");
  36. else sb.append("NO\n");
  37. }
  38. else st.update(q);
  39. //System.out.println("H");
  40. }
  41. }
  42. pr.write(sb.toString());
  43. pr.flush();
  44. }
  45. static class Segment_Tree{
  46. int start,end,sum,min_sum;
  47. Segment_Tree left_child,right_child;
  48.  
  49. public Segment_Tree(){};
  50. public Segment_Tree(int a,int b){
  51. start=a;end=b;
  52. if(start!=end){
  53. int mid=(start+end)>>1;
  54. left_child=new Segment_Tree(a,mid);
  55. right_child=new Segment_Tree(mid+1,b);
  56. join(this,left_child,right_child);
  57. }
  58. else {
  59. sum=min_sum=BRCKTS.a[start]==1?1:-1;
  60. }
  61. }
  62. public void join(Segment_Tree parent,Segment_Tree leftchild,Segment_Tree rightchild){
  63. parent.sum=leftchild.sum+rightchild.sum;
  64. parent.min_sum=Math.min(leftchild.min_sum,rightchild.min_sum+leftchild.sum);
  65. }
  66. public void update(int index){
  67. if(start==index && end==index){
  68. sum=min_sum=min_sum==1?-1:1;
  69. return ;
  70. }
  71. int mid=(start+end)>>1;
  72. if(index<=mid)left_child.update(index);
  73. else if(index>mid)right_child.update(index);
  74. join(this, left_child, right_child);
  75. }
  76. }
  77. static class Reader {
  78. final private int BUFFER_SIZE = 1 << 16;
  79. private DataInputStream din;
  80. private byte[] buffer;
  81. private int bufferPointer, bytesRead;
  82.  
  83. public Reader() {
  84. din = new DataInputStream(System.in);
  85. buffer = new byte[BUFFER_SIZE];
  86. bufferPointer = bytesRead = 0;
  87. }
  88.  
  89. public Reader(String file_name) throws IOException {
  90. din = new DataInputStream(new FileInputStream(file_name));
  91. buffer = new byte[BUFFER_SIZE];
  92. bufferPointer = bytesRead = 0;
  93. }
  94.  
  95. public String readLine() throws IOException {
  96. byte[] buf = new byte[64];
  97. int cnt = 0, c;
  98. while ((c = read()) != -1) {
  99. if (c == '\n')
  100. break;
  101. buf[cnt++] = (byte) c;
  102. }
  103. return new String(buf, 0, cnt);
  104. }
  105.  
  106. public int nextInt() throws IOException {
  107. int ret = 0;
  108. byte c = read();
  109. while (c <= ' ')
  110. c = read();
  111. boolean neg = (c == '-');
  112. if (neg)
  113. c = read();
  114. do {
  115. ret = ret * 10 + c - '0';
  116. } while ((c = read()) >= '0' && c <= '9');
  117. if (neg)
  118. return -ret;
  119. return ret;
  120. }
  121.  
  122. public long nextLong() throws IOException {
  123. long ret = 0;
  124. byte c = read();
  125. while (c <= ' ')
  126. c = read();
  127. boolean neg = (c == '-');
  128. if (neg)
  129. c = read();
  130. do {
  131. ret = ret * 10 + c - '0';
  132. } while ((c = read()) >= '0' && c <= '9');
  133. if (neg)
  134. return -ret;
  135. return ret;
  136. }
  137.  
  138. public double nextDouble() throws IOException {
  139. double ret = 0, div = 1;
  140. byte c = read();
  141. while (c <= ' ')
  142. c = read();
  143. boolean neg = (c == '-');
  144. if (neg)
  145. c = read();
  146. do {
  147. ret = ret * 10 + c - '0';
  148. } while ((c = read()) >= '0' && c <= '9');
  149. if (c == '.')
  150. while ((c = read()) >= '0' && c <= '9')
  151. ret += (c - '0') / (div *= 10);
  152. if (neg)
  153. return -ret;
  154. return ret;
  155. }
  156.  
  157. private void fillBuffer() throws IOException {
  158. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  159. if (bytesRead == -1)
  160. buffer[0] = -1;
  161. }
  162.  
  163. private byte read() throws IOException {
  164. if (bufferPointer == bytesRead)
  165. fillBuffer();
  166. return buffer[bufferPointer++];
  167. }
  168.  
  169. public void close() throws IOException {
  170. if (din == null)
  171. return;
  172. din.close();
  173. }
  174. }
  175.  
  176. }
  177. /*
  178. 12
  179. (()(()(())))
  180. 16
  181. 0
  182. 1
  183. 3
  184. 0
  185. 4
  186. 5
  187. 6
  188. 0
  189. 10
  190. 0
  191. 2
  192. 0
  193. 1
  194. 3
  195. 4
  196. 0
  197. */
Success #stdin #stdout 0.06s 380224KB
stdin
4
()((
4
4
0
2
0

stdout
Test 1:
YES
NO