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