CCCPaste

Untitled

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class DLLNode {
    protected E element;
    protected DLLNode pred, succ;

    public DLLNode(E elem, DLLNode pred, DLLNode succ) {
        this.element = elem;
        this.pred = pred;
        this.succ = succ;
    }

    @Override
    public String toString() {
        return "<-"+element.toString()+"->";
    }
}


class DLL {
    private DLLNode first, last;

    public DLL() {
        // Construct an empty SLL
        this.first = null;
        this.last = null;
    }

    public void deleteList() {
        first = null;
        last = null;
    }

    public int length() {
        int ret;
        if (first != null) {
            DLLNode tmp = first;
            ret = 1;
            while (tmp.succ != null) {
                tmp = tmp.succ;
                ret++;
            }
            return ret;
        } else
            return 0;

    }

    public void insertFirst(E o) {
        DLLNode ins = new DLLNode(o, null, first);
        if (first == null)
            last = ins;
        else
            first.pred = ins;
        first = ins;
    }

    public void insertLast(E o) {
        if (first == null)
            insertFirst(o);
        else {
            DLLNode ins = new DLLNode(o, last, null);
            last.succ = ins;
            last = ins;
        }
    }

    public void insertAfter(E o, DLLNode after) {
        if(after==last){
            insertLast(o);
            return;
        }
        DLLNode ins = new DLLNode(o, after, after.succ);
        after.succ.pred = ins;
        after.succ = ins;
    }

    public void insertBefore(E o, DLLNode before) {
        if(before == first){
            insertFirst(o);
            return;
        }
        DLLNode ins = new DLLNode(o, before.pred, before);
        before.pred.succ = ins;
        before.pred = ins;
    }

    public E deleteFirst() {
        if (first != null) {
            DLLNode tmp = first;
            first = first.succ;
            if (first != null) first.pred = null;
            if (first == null)
                last = null;
            return tmp.element;
        } else
            return null;
    }

    public E deleteLast() {
        if (first != null) {
            if (first.succ == null)
                return deleteFirst();
            else {
                DLLNode tmp = last;
                last = last.pred;
                last.succ = null;
                return tmp.element;
            }
        }
        // else throw Exception
        return null;
    }


    @Override
    public String toString() {
        String ret = new String();
        if (first != null) {
            DLLNode tmp = first;
            ret += tmp + "<->";
            while (tmp.succ != null) {
                tmp = tmp.succ;
                ret += tmp + "<->";
            }
        } else
            ret = "Prazna lista!!!";
        return ret;
    }

    public DLLNode getFirst() {
        return first;
    }

    public DLLNode getLast() {

        return last;
    }

}

public class DLLVojska {


    public static void main(String[] args) throws IOException {
        DLL lista = new DLL();
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String s = stdin.readLine();
        int N = Integer.parseInt(s);
        s = stdin.readLine();
        String[] ids = s.split(" ");
        for (int i = 0; i < N; i++) {
            lista.insertLast(Integer.parseInt(ids[i]));
        }

        s = stdin.readLine();
        String interval[] = s.split(" ");
        int a = Integer.parseInt(interval[0]);
        int b = Integer.parseInt(interval[1]);

        s = stdin.readLine();
        interval = s.split(" ");
        int c = Integer.parseInt(interval[0]);
        int d = Integer.parseInt(interval[1]);


        DLL result = vojska(lista, a, b, c, d);


        DLLNode node = result.getFirst();
        System.out.print(node.element);
        node = node.succ;
        while(node != null){
            System.out.print(" "+node.element);
            node = node.succ;
        }

    }

    private static DLL vojska(DLL lista, int a, int b, int c, int d) {

        // Vasiot kod tuka
        int a_index = -1, b_index = -1, c_index = -1, d_index = -1;
        DLLNode head = lista.getFirst();
        int i = 0;
        while(head != null) {
            if(head.element == a) {
                a_index = i;
            }
            if(head.element == b) {
                b_index = i;
            }
            if(head.element == c) {
                c_index = i;
            }
            if(head.element == d) {
                d_index = i;
            }
            head = head.succ;
            i++;
        }
        int[] niza = new int[lista.length()];
        head = lista.getFirst();
        i = 0;
        while(head != null) {
            niza[i] = head.element;
            head = head.succ;
            i++;
        }

        DLL answer = new DLL();
        int tmp_c_index = c_index;
        int tmp_a_index = a_index;
        for(int j = 0; j < niza.length; j++) {
            if(a_index == j) {
                    while(tmp_c_index <= d_index) {
                        answer.insertLast(niza[tmp_c_index]);
                        tmp_c_index++;
                    }
                j = b_index;

            }
            else if(c_index == j) {
                while(tmp_a_index <= b_index) {
                    answer.insertLast(niza[tmp_a_index]);
                    tmp_a_index++;

                }
                j = d_index;
            }
            else {
                answer.insertLast(niza[j]);
            }
        }
        return answer;
    }
}