Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by IntelliJ IDEA.
- * User: Администратор
- * Date: 10.05.2011
- * Time: 13:27:24
- * To change this template use File | Settings | File Templates.
- */
- import java.io.*;
- import java.util.*;
- import java.math.*;
- public class prF {
- static public class tree {
- int y; long sumbaby; int countbaby; int dolg; int x;
- tree L, R;
- tree (int xx, int a) {
- x = xx;
- y = a;
- sumbaby = 0;
- countbaby = 0;
- dolg = 0;
- }
- }
- static void datdolg(tree T) {
- if (T!=null) {
- T.dolg = (T.dolg+1)%2;
- }
- }
- static tree Tvosppereddolg;
- static void pereddolg(tree T) {
- T.dolg = 0;
- datdolg(T.L);
- datdolg(T.R);
- Tvosppereddolg = T.L;
- T.L = T.R;
- T.R = Tvosppereddolg;
- }
- static long getsumbaby(tree T) {
- if (T!=null) {
- return T.sumbaby;
- } else {
- return 0;
- }
- }
- static int getcountmbaby(tree T) {
- if (T!=null) {
- return T.countbaby;
- } else {
- return 0;
- }
- }
- static int getx(tree T) {
- if (T!=null) {
- return T.x;
- } else {
- return 0;
- }
- }
- static long data(tree T) {
- if (T!=null) {
- return (getx(T) + getsumbaby(T)) ; }
- else {
- return 0;
- }
- }
- static int vercount(tree T) {
- if (T==null) {
- return 0;
- } else {
- return T.countbaby+1;
- }
- }
- static void rebuild(tree T) {
- T.sumbaby = data(T.L) + data(T.R);
- T.countbaby = vercount(T.L) + vercount(T.R);
- }
- static int tipa_x(tree T) {
- if (T==null) {
- return 0;
- } else {
- return vercount(T.L)+1;
- }
- }
- static int tix;
- static tree[] split (tree T, int x) {
- if (T == null) {
- tree res[]=new tree[2]; res[0]=null; res[1]=null;
- return res;
- }
- tree[] res;
- if (T.dolg == 1) {
- pereddolg(T); }
- if (x < tipa_x(T)) {
- res = split(T.L, x);
- T.L = res[1];
- rebuild(T);
- res[1] = T;
- } else {
- tix = tipa_x(T);
- res = split(T.R, x-tix) ;
- T.R = res[0];
- rebuild(T);
- res[0] = T;
- }
- return res;
- }
- static tree merge (tree L, tree R) {
- if (L==null) {
- return R;
- }
- if (R==null) {
- return L;
- }
- if (L.dolg == 1) {
- pereddolg(L); }
- if (R.dolg == 1) {
- pereddolg(R); }
- if (L.y > R.y) {
- L.R = merge (L.R, R);
- rebuild(L);
- return L;
- } else {
- R.L = merge(L, R.L);
- rebuild(R);
- return R;
- }
- }
- static tree[] Tsum1 = new tree[2];
- static tree[] Tsum2 = new tree[2];
- static long vospsum;
- static long sum (int l, int r, tree T) {
- Tsum1 = split(T, l-1);
- Tsum2 = split(Tsum1[1], r-vercount(Tsum1[0]));
- vospsum = data(Tsum2[0]);
- Tsum1[1] = merge(Tsum2[0], Tsum2[1]);
- T = merge(Tsum1[0], Tsum1[1]);
- return vospsum;
- }
- static void perevertos(int l, int r, tree T) {
- Tsum1 = split(T, l-1);
- Tsum2 = split(Tsum1[1], r-vercount(Tsum1[0]));
- // showtree(Tsum2[0], "");
- datdolg(Tsum2[0]);
- Tsum1[1] = merge(Tsum2[0], Tsum2[1]);
- T = merge(Tsum1[0], Tsum1[1]);
- }
- static StreamTokenizer in;
- int nextInt() throws Exception
- {
- in.nextToken();
- return (int)in.nval;
- }
- static int rand6() {
- return ((int)(1000000*Math.random())) ;
- }
- static void showtree (tree T, String s) {
- if (T != null) {
- System.out.print(s + "sumb ");
- System.out.println(T.sumbaby);
- System.out.print(s + "countb ");
- System.out.println(T.countbaby);
- System.out.print(s + "x ");
- System.out.println(T.x);
- System.out.println(s +"left");
- showtree(T.L, s + " ");
- System.out.println(s + "right");
- showtree(T.R, s + " ");
- System.out.println();
- }
- }
- public static void main(String args[]) throws Exception{
- in = new StreamTokenizer(new BufferedReader(new FileReader("reverse.in")));
- PrintWriter out = new PrintWriter("reverse.out");
- int detcount=0;
- int num;
- in.nextToken();
- detcount = (int)in.nval;
- in.nextToken();
- num = (int)in.nval;
- int i;
- int y;
- tree N; tree T=null;
- for (i=1; i<=detcount; i++) {
- in.nextToken();
- y = rand6();
- N = new tree((int)in.nval, y);
- T = merge(T, N);
- }
- int q; int l, r;
- for (i=1; i<=num; i++) {
- in.nextToken();
- q = (int)in.nval;
- if (q==0) {
- in.nextToken();
- l = (int)in.nval;
- in.nextToken();
- r = (int)in.nval;
- out.println(sum(l, r, T));
- // showtree(T, "");
- } else {
- in.nextToken();
- l = (int)in.nval;
- in.nextToken();
- r = (int)in.nval;
- perevertos(l, r, T);
- // showtree(T, "");
- }
- }
- out.close();
- }
- }
Add Comment
Please, Sign In to add comment