fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. class FastReader {
  7.  
  8. public FastReader() {
  9. }
  10.  
  11. String next() throws IOException {
  12. while (st == null || !st.hasMoreElements()) {
  13. st = new StringTokenizer(br.readLine());
  14. }
  15. return st.nextToken();
  16. }
  17.  
  18. int nextInt() throws IOException {
  19. return Integer.parseInt(next());
  20. }
  21.  
  22. long nextLong() throws IOException {
  23. return Long.parseLong(next());
  24. }
  25.  
  26. double nextDouble() throws IOException {
  27. return Double.parseDouble(next());
  28. }
  29.  
  30. String nextLine() throws IOException{
  31. return br.readLine();
  32. }
  33. }
  34.  
  35. class Main{
  36.  
  37. static class pair{
  38. long val;
  39. long type;
  40. pair(long val, long type){
  41. this.val = val;
  42. this.type = type;
  43. }
  44. }
  45. static long[][] segmentArray;
  46. static pair[] lazyArray;
  47. static int[] ar;
  48.  
  49. public static void build(int ind, int l, int r){
  50. if( l == r){
  51. segmentArray[ind][0] = (long) ar[l] * ar[l];
  52. segmentArray[ind][1] = ar[l];
  53. return;
  54. }
  55. int mid = (l+r)/2;
  56. build(2*ind+1, l, mid);
  57. build(2*ind+2, mid+1, r);
  58.  
  59. segmentArray[ind][0] = segmentArray[2*ind+1][0] + segmentArray[2*ind+2][0];
  60. segmentArray[ind][1] = segmentArray[2*ind+1][1] + segmentArray[2*ind+2][1];
  61. }
  62.  
  63. public static long find(int ind, long l, long r, int lr, int rr){
  64.  
  65. if( lazyArray[ind] != null ){
  66. pair temp = lazyArray[ind];
  67. if( temp.type == 1){
  68. segmentArray[ind][0] += (r-l+1) * temp.val * temp.val + 2 * temp.val * segmentArray[ind][1];
  69. segmentArray[ind][1] += (r - l + 1) * temp.val;
  70. }
  71. else{
  72. segmentArray[ind][0] = (r-l+1) * temp.val * temp.val;
  73. segmentArray[ind][1] = (r - l + 1) * temp.val;
  74. }
  75. if( l != r){
  76. helper(2*ind+1, temp.val, temp.type);
  77. helper(2*ind+2, temp.val, temp.type);
  78. }
  79. lazyArray[ind] = null;
  80. }
  81.  
  82. if( r < lr || rr < l || l > r)
  83. return 0;
  84.  
  85. if( l >= lr && r <= rr)
  86. return segmentArray[ind][0];
  87.  
  88. long mid = (r+l)/2;
  89. return find(2*ind+1, l, mid, lr, rr ) + find(2*ind+2, mid+1, r, lr, rr);
  90. }
  91.  
  92. private static void update(int ind, long l, long r, int u, int v, int val, int type) {
  93.  
  94. if( lazyArray[ind] != null ){
  95. pair temp = lazyArray[ind];
  96. if( temp.type == 1){
  97. segmentArray[ind][0] += (r - l + 1) * temp.val* temp.val + 2 * temp.val * segmentArray[ind][1];
  98. segmentArray[ind][1] += (r - l + 1) * temp.val;
  99. }
  100. else{
  101. segmentArray[ind][0] = (r - l + 1) * temp.val* temp.val;
  102. segmentArray[ind][1] = (r - l + 1) * temp.val;
  103. }
  104. if( l != r){
  105. helper(2*ind+1, temp.val, temp.type);
  106. helper(2*ind+2, temp.val, temp.type);
  107. }
  108. lazyArray[ind] = null;
  109. }
  110.  
  111. if( l >= u && r <= v){
  112. if( type == 1){
  113. segmentArray[ind][0] += (r - l + 1) * val * val + 2 * val * segmentArray[ind][1];
  114. segmentArray[ind][1] += (r - l + 1) * val;
  115. }
  116. else{
  117. segmentArray[ind][0] = (r - l + 1) * val * val;
  118. segmentArray[ind][1] = (r - l + 1) * val;
  119. }
  120. if( l != r){
  121. helper(2*ind+1, val, type);
  122. helper(2*ind+2, val, type);
  123. }
  124. return;
  125. }
  126.  
  127. if( r < u || v < l || l > r)
  128. return;
  129.  
  130. long mid = (r+l)/2;
  131. update(2*ind+1, l, mid, u, v, val, type);
  132. update(2*ind+2, mid+1, r, u, v, val, type);
  133.  
  134. segmentArray[ind][0] = segmentArray[2*ind+1][0] + segmentArray[2*ind+2][0];
  135. segmentArray[ind][1] = segmentArray[2*ind+1][1] + segmentArray[2*ind+2][1];
  136. }
  137.  
  138. public static void helper(int i, long pre_val, long pre_type) {
  139. if( lazyArray[i] == null){
  140. lazyArray[i] = new pair(pre_val, pre_type);
  141. }
  142. else {
  143. pair next = lazyArray[i];
  144. if (pre_type == 1 && (next.type == 1 || next.type == 0)) {
  145. next.val = pre_val + next.val;
  146. } else {
  147. next.type = 0;
  148. next.val = pre_val;
  149. }
  150. }
  151. }
  152.  
  153. public static void main(String[] args) throws IOException {
  154. FastReader ff = new FastReader();
  155. int t = ff.nextInt();
  156. for(int x =1; x <= t; x++) {
  157. System.out.println("Case " +x + ":");
  158. int n = ff.nextInt();
  159. int q = ff.nextInt();
  160. segmentArray = new long[4 * n + 5][2];
  161. lazyArray = new pair[4 * n + 5];
  162. ar = new int[n];
  163.  
  164. for(int i =0; i < n; i++){
  165. ar[i] = ff.nextInt();
  166. }
  167.  
  168. build(0, 0, n - 1);
  169. while(q-- > 0) {
  170. int op = ff.nextInt();
  171. int u = ff.nextInt()-1;
  172. int v = ff.nextInt()-1;
  173. if (op == 2) {
  174. System.out.println((int) find(0, 0, n - 1, u, v));
  175. }
  176. else {
  177. int val = ff.nextInt();
  178. update(0, 0, n - 1, u, v, val, op);
  179. }
  180. }
  181. }
  182. }
  183. }
Success #stdin #stdout 0.1s 50588KB
stdin
2
4 5
1 2 3 4
2 1 4
0 3 4 1
2 1 4
1 3 4 1
2 1 4
1 1
1
2 1 1
stdout
Case 1:
30
7
13
Case 2:
1