Guest User

Untitled

a guest
Dec 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1.  
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: Администратор
  5. * Date: 10.05.2011
  6. * Time: 13:27:24
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. import java.io.*;
  10. import java.util.*;
  11. import java.math.*;
  12. public class prF {
  13. static public class tree {
  14. int y; long sumbaby; int countbaby; int dolg; int x;
  15. tree L, R;
  16. tree (int xx, int a) {
  17. x = xx;
  18. y = a;
  19. sumbaby = 0;
  20. countbaby = 0;
  21. dolg = 0;
  22. }
  23. }
  24. static void datdolg(tree T) {
  25. if (T!=null) {
  26. T.dolg = (T.dolg+1)%2;
  27. }
  28. }
  29. static tree Tvosppereddolg;
  30. static void pereddolg(tree T) {
  31. T.dolg = 0;
  32. datdolg(T.L);
  33. datdolg(T.R);
  34. Tvosppereddolg = T.L;
  35. T.L = T.R;
  36. T.R = Tvosppereddolg;
  37. }
  38. static long getsumbaby(tree T) {
  39. if (T!=null) {
  40. return T.sumbaby;
  41. } else {
  42. return 0;
  43. }
  44. }
  45. static int getcountmbaby(tree T) {
  46. if (T!=null) {
  47. return T.countbaby;
  48. } else {
  49. return 0;
  50. }
  51. }
  52. static int getx(tree T) {
  53. if (T!=null) {
  54. return T.x;
  55. } else {
  56. return 0;
  57. }
  58. }
  59. static long data(tree T) {
  60. if (T!=null) {
  61. return (getx(T) + getsumbaby(T)) ; }
  62. else {
  63. return 0;
  64. }
  65. }
  66. static int vercount(tree T) {
  67. if (T==null) {
  68. return 0;
  69. } else {
  70. return T.countbaby+1;
  71. }
  72. }
  73. static void rebuild(tree T) {
  74. T.sumbaby = data(T.L) + data(T.R);
  75. T.countbaby = vercount(T.L) + vercount(T.R);
  76. }
  77. static int tipa_x(tree T) {
  78. if (T==null) {
  79. return 0;
  80. } else {
  81. return vercount(T.L)+1;
  82. }
  83. }
  84. static int tix;
  85. static tree[] split (tree T, int x) {
  86. if (T == null) {
  87. tree res[]=new tree[2]; res[0]=null; res[1]=null;
  88.  
  89. return res;
  90. }
  91. tree[] res;
  92. if (T.dolg == 1) {
  93. pereddolg(T); }
  94. if (x < tipa_x(T)) {
  95. res = split(T.L, x);
  96. T.L = res[1];
  97. rebuild(T);
  98. res[1] = T;
  99.  
  100. } else {
  101. tix = tipa_x(T);
  102. res = split(T.R, x-tix) ;
  103. T.R = res[0];
  104. rebuild(T);
  105. res[0] = T;
  106. }
  107. return res;
  108. }
  109. static tree merge (tree L, tree R) {
  110. if (L==null) {
  111. return R;
  112. }
  113. if (R==null) {
  114. return L;
  115. }
  116. if (L.dolg == 1) {
  117. pereddolg(L); }
  118. if (R.dolg == 1) {
  119. pereddolg(R); }
  120. if (L.y > R.y) {
  121. L.R = merge (L.R, R);
  122. rebuild(L);
  123. return L;
  124. } else {
  125. R.L = merge(L, R.L);
  126. rebuild(R);
  127. return R;
  128. }
  129. }
  130. static tree[] Tsum1 = new tree[2];
  131. static tree[] Tsum2 = new tree[2];
  132. static long vospsum;
  133. static long sum (int l, int r, tree T) {
  134. Tsum1 = split(T, l-1);
  135. Tsum2 = split(Tsum1[1], r-vercount(Tsum1[0]));
  136. vospsum = data(Tsum2[0]);
  137. Tsum1[1] = merge(Tsum2[0], Tsum2[1]);
  138. T = merge(Tsum1[0], Tsum1[1]);
  139. return vospsum;
  140. }
  141. static void perevertos(int l, int r, tree T) {
  142. Tsum1 = split(T, l-1);
  143. Tsum2 = split(Tsum1[1], r-vercount(Tsum1[0]));
  144. // showtree(Tsum2[0], "");
  145. datdolg(Tsum2[0]);
  146. Tsum1[1] = merge(Tsum2[0], Tsum2[1]);
  147. T = merge(Tsum1[0], Tsum1[1]);
  148. }
  149. static StreamTokenizer in;
  150. int nextInt() throws Exception
  151. {
  152. in.nextToken();
  153. return (int)in.nval;
  154. }
  155. static int rand6() {
  156. return ((int)(1000000*Math.random())) ;
  157. }
  158. static void showtree (tree T, String s) {
  159. if (T != null) {
  160. System.out.print(s + "sumb ");
  161. System.out.println(T.sumbaby);
  162. System.out.print(s + "countb ");
  163. System.out.println(T.countbaby);
  164. System.out.print(s + "x ");
  165. System.out.println(T.x);
  166. System.out.println(s +"left");
  167. showtree(T.L, s + " ");
  168. System.out.println(s + "right");
  169. showtree(T.R, s + " ");
  170. System.out.println();
  171. }
  172. }
  173. public static void main(String args[]) throws Exception{
  174. in = new StreamTokenizer(new BufferedReader(new FileReader("reverse.in")));
  175. PrintWriter out = new PrintWriter("reverse.out");
  176. int detcount=0;
  177. int num;
  178. in.nextToken();
  179. detcount = (int)in.nval;
  180. in.nextToken();
  181. num = (int)in.nval;
  182. int i;
  183. int y;
  184. tree N; tree T=null;
  185. for (i=1; i<=detcount; i++) {
  186. in.nextToken();
  187. y = rand6();
  188. N = new tree((int)in.nval, y);
  189. T = merge(T, N);
  190. }
  191. int q; int l, r;
  192. for (i=1; i<=num; i++) {
  193. in.nextToken();
  194. q = (int)in.nval;
  195. if (q==0) {
  196. in.nextToken();
  197. l = (int)in.nval;
  198. in.nextToken();
  199. r = (int)in.nval;
  200. out.println(sum(l, r, T));
  201. // showtree(T, "");
  202. } else {
  203. in.nextToken();
  204. l = (int)in.nval;
  205. in.nextToken();
  206. r = (int)in.nval;
  207. perevertos(l, r, T);
  208. // showtree(T, "");
  209. }
  210. }
  211. out.close();
  212. }
  213. }
Add Comment
Please, Sign In to add comment