fork(1) 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 GSS4 {
  8. static int array[];
  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 test=1;
  14. while(true){
  15. String s=r.readLine();
  16. if(s==null || s.equalsIgnoreCase("") || s.equals("\n"))break;
  17. int N=Integer.parseInt(s);
  18. sb.append("Case #"+test++ +":\n");
  19. array=new int[N+1];
  20. for(int i=1;i<=N;i++){
  21. array[i]=r.nextInt();
  22. }
  23. Segment_Tree root=new Segment_Tree(1, N);
  24. int M=r.nextInt();
  25. for(int i=0;i<M;i++){
  26. int choice=r.nextInt();
  27. int low=r.nextInt();
  28. int high=r.nextInt();
  29. if(choice==0){
  30. root.update(low, high);
  31. }
  32. else{
  33. sb.append(root.query(low, high).sum+"\n");
  34. }
  35. }
  36. sb.append("\n");
  37. }
  38. pr.write(sb.toString());
  39. pr.flush();
  40. }
  41. static class Reader {
  42. final private int BUFFER_SIZE = 1 << 16;
  43. private DataInputStream din;
  44. private byte[] buffer;
  45. private int bufferPointer, bytesRead;
  46.  
  47. public Reader() {
  48. din = new DataInputStream(System.in);
  49. buffer = new byte[BUFFER_SIZE];
  50. bufferPointer = bytesRead = 0;
  51. }
  52.  
  53. public Reader(String file_name) throws IOException {
  54. din = new DataInputStream(new FileInputStream(file_name));
  55. buffer = new byte[BUFFER_SIZE];
  56. bufferPointer = bytesRead = 0;
  57. }
  58.  
  59. public String readLine() throws IOException {
  60. byte[] buf = new byte[64];
  61. int cnt = 0, c;
  62. while ((c = read()) != -1) {
  63. if (c == '\n')
  64. break;
  65. buf[cnt++] = (byte) c;
  66. }
  67. return new String(buf, 0, cnt);
  68. }
  69.  
  70. public int nextInt() throws IOException {
  71. int ret = 0;
  72. byte c = read();
  73. while (c <= ' ')
  74. c = read();
  75. boolean neg = (c == '-');
  76. if (neg)
  77. c = read();
  78. do {
  79. ret = ret * 10 + c - '0';
  80. } while ((c = read()) >= '0' && c <= '9');
  81. if (neg)
  82. return -ret;
  83. return ret;
  84. }
  85.  
  86. public long nextLong() throws IOException {
  87. long ret = 0;
  88. byte c = read();
  89. while (c <= ' ')
  90. c = read();
  91. boolean neg = (c == '-');
  92. if (neg)
  93. c = read();
  94. do {
  95. ret = ret * 10 + c - '0';
  96. } while ((c = read()) >= '0' && c <= '9');
  97. if (neg)
  98. return -ret;
  99. return ret;
  100. }
  101.  
  102. public double nextDouble() throws IOException {
  103. double ret = 0, div = 1;
  104. byte c = read();
  105. while (c <= ' ')
  106. c = read();
  107. boolean neg = (c == '-');
  108. if (neg)
  109. c = read();
  110. do {
  111. ret = ret * 10 + c - '0';
  112. } while ((c = read()) >= '0' && c <= '9');
  113. if (c == '.')
  114. while ((c = read()) >= '0' && c <= '9')
  115. ret += (c - '0') / (div *= 10);
  116. if (neg)
  117. return -ret;
  118. return ret;
  119. }
  120.  
  121. private void fillBuffer() throws IOException {
  122. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  123. if (bytesRead == -1)
  124. buffer[0] = -1;
  125. }
  126.  
  127. private byte read() throws IOException {
  128. if (bufferPointer == bytesRead)
  129. fillBuffer();
  130. return buffer[bufferPointer++];
  131. }
  132.  
  133. public void close() throws IOException {
  134. if (din == null)
  135. return;
  136. din.close();
  137. }
  138. }
  139. static class Segment_Tree{
  140. Segment_Tree left_child,right_child;
  141. int start,end;long sum;
  142. long lazy;
  143. public Segment_Tree(){}
  144. public Segment_Tree(int a,int b){
  145. start=a;
  146. end=b;
  147. if(start!=end){
  148. int mid=(start+end)>>1;
  149. left_child=new Segment_Tree(start, mid);
  150. right_child=new Segment_Tree(mid+1, end);
  151. join(this,left_child,right_child);
  152. }
  153. else{
  154. sum=GSS4.array[start];
  155. }
  156. }
  157. public void join(Segment_Tree parent,Segment_Tree left_child,Segment_Tree right_child){
  158. parent.sum=left_child.sum+right_child.sum;
  159. }
  160. public Segment_Tree query(int a,int b){
  161. if(a>b || a<start || b>end)return new Segment_Tree();
  162. if(start==a && end==b)return this;
  163. int mid=(start+end)>>1;
  164. if(a>mid)return right_child.query(a, b);
  165. else if(b<=mid) return left_child.query(a, b);
  166. Segment_Tree ans=new Segment_Tree();
  167. join(ans,left_child.query(a, mid),right_child.query(mid+1, b));
  168. return ans;
  169. }
  170. public void update(int a,int b){
  171.  
  172. if(a>b || a<start || b>end)return;
  173. if(start!=end){
  174. int mid=(start+end)>>1;
  175. left_child.update(a, mid);
  176. right_child.update(mid+1, b);
  177. join(this, left_child, right_child);
  178. }
  179. else{
  180. this.sum=(long)Math.sqrt(this.sum);
  181. }
  182. }
  183. }
  184. }
  185.  
Success #stdin #stdout 0.07s 380352KB
stdin
5
1 2 3 4 5
5
1 2 4
0 2 4
1 2 4
0 4 5
1 1 5
4
10 10 10 10
3
1 1 4
0 2 3
1 1 4
stdout
Case #1:
9
4
6

Case #2:
40
26