Cod sursa(job #2757789)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 6 iunie 2021 15:31:42
Problema Secv8 Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 8.05 kb
package com.DS;

import java.io.*;
import java.util.Random;
import java.nio.charset.StandardCharsets;
import java.util.InputMismatchException;

class TreapNode
{
    TreapNode left;
    TreapNode right;
    int size;
    int priority;
    int value;
    boolean lazy;

    TreapNode(int value)
    {
        this.size = 1;
        this.value = value;
        this.priority = new Random().nextInt(1_000_000_000);
    }
}

class TreapNodePair
{
    TreapNode first;
    TreapNode second;

    TreapNodePair(TreapNode first, TreapNode second)
    {
        this.first = first;
        this.second = second;
    }
}

class Main {

    static TreapNode root;

    static InputStream inputStream;
    static InputReader input;
    static OutputStream outputStream;
    static OutputWriter output;

    public static void update(TreapNode root)
    {
        if(root == null)
            return ;

        root.size = ((root.left == null) ? 0 : root.left.size) + ((root.right == null) ? 0 : root.right.size) + 1;
    }

    public static void propaga(TreapNode root)
    {
        if(root != null && root.lazy)
        {
            root.lazy = false;

            if(root.left != null)
                root.left.lazy = !root.left.lazy;

            if(root.right != null)
                root.right.lazy = !root.right.lazy;

            TreapNode temp = root.left;
            root.left = root.right;
            root.right = temp;
        }
    }

    public static TreapNode meld(TreapNode left, TreapNode right)
    {
        propaga(left);
        propaga(right);

        update(left);
        update(right);

        if(left == null)
            return right;

        if(right == null)
            return left;

        if(left.priority < right.priority)
        {
            left.right = meld(left.right, right);
            propaga(left);
            update(left);
            return left;
        }
        else
        {
            right.left = meld(left, right.left);
            propaga(right);
            update(right);
            return right;
        }
    }

    public static TreapNodePair split(TreapNode root, int key)
    {
        TreapNodePair res = new TreapNodePair(null, null);

        if(root == null)
            return res;

        propaga(root);
        update(root);

        int pos = 1;

        if(root.left != null)
            pos += root.left.size;

        if(pos == key)
        {
            res.first = root.left;
            root.left = null;
            res.second = root;
        }
        else if(pos < key)
        {
            TreapNodePair aux = split(root.right, key - pos);
            res.second = aux.second;
            root.right = aux.first;
            res.first = root;
        }
        else
        {
            TreapNodePair aux = split(root.left, key);
            res.first = aux.first;
            root.left = aux.second;
            res.second = root;
        }

        propaga(res.first);
        propaga(res.second);

        update(res.first);
        update(res.second);

        return res;
    }

    public static TreapNode insert(TreapNode root, int pos, int val)
    {
        propaga(root);
        update(root);

        TreapNodePair aux = split(root, pos);
        TreapNode solo = new TreapNode(val);

        return meld(meld(aux.first, solo), aux.second);
    }

    public static TreapNode reverse(TreapNode root, int x, int y)
    {
        propaga(root);
        update(root);

        TreapNodePair aux1 = split(root, y + 1);
        TreapNodePair aux2 = split(aux1.first, x);

        if(aux2.second != null)
            aux2.second.lazy = !aux2.second.lazy;

        return meld(meld(aux2.first, aux2.second), aux1.second);
    }

    public static TreapNode delete(TreapNode root, int x, int y)
    {
        propaga(root);
        update(root);

        TreapNodePair aux1 = split(root, y + 1);
        TreapNodePair aux2 = split(aux1.first, x);

        return meld(aux2.first, aux1.second);
    }

    public static int search(TreapNode root, int key)
    {
        if(root == null)
            return 0;

        propaga(root);
        update(root);

        int pos = 1;

        if(root.left != null)
            pos += root.left.size;

        if(key == pos)
            return root.value;

        if(key < pos)
            return search(root.left, key);
        else
            return search(root.right, key - pos);
    }

    public static void print(TreapNode root)
    {
        if(root == null)
            return ;

        propaga(root);

        print(root.left);
        output.print(root.value + " ");
        print(root.right);
    }

    public static void main(String... args) throws FileNotFoundException {
        inputStream = new FileInputStream("E:\\Secv8\\src\\com\\DS\\secv8.in");
        input = new InputReader(inputStream);
        outputStream = new FileOutputStream("E:\\Secv8\\src\\com\\DS\\secv8.out");
        output = new OutputWriter(outputStream);

        int numberOfOps = input.readInt();
        input.readInt();

        for(int i = 1; i <= numberOfOps; ++i)
        {
            char opType = input.readChar();

            switch(opType)
            {
                case 'I':
                    int pos = input.readInt();
                    int value = input.readInt();

                    root = insert(root, pos, value);
                    break;
                case 'A':
                    pos = input.readInt();
                    output.printLine(search(root, pos));
                    break;
                case 'R':
                    int x = input.readInt();
                    int y = input.readInt();
                    root = reverse(root, x, y);
                    break;
                default:
                    x = input.readInt();
                    y = input.readInt();
                    root = delete(root, x, y);
                    break;
            }
        }

        print(root);
        output.close();
    }
}

class InputReader {
    private String buffer;
    private int currentChar;

    public InputReader(InputStream stream)
    {
        try {
            buffer = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
            stream.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public char read()
    {
        if(currentChar == buffer.length())
            throw new InputMismatchException();
        return buffer.charAt(currentChar++);
    }

    private boolean isSpaceChar(char ch)
    {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }

    public char readChar()
    {
        char ch;

        do {
            ch = read();
        }while(isSpaceChar(ch));

        return ch;
    }

    public int readInt()
    {
        char ch;
        do{
            ch = read();
        }while(isSpaceChar(ch));

        int sign = ((ch == '-') ? -1 : 1);

        if(ch == '-')
            ch = read();

        int number = 0;

        do{
            if(!(ch >= '0' && ch <= '9')) {
                throw new InputMismatchException();
            }
            number = number * 10 + ch - '0';
            ch = read();
        }while(!isSpaceChar(ch));

        return number * sign;
    }
}

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public void print (Object... objects)
    {
        for(int i = 0; i < objects.length; ++i)
        {
            if(i != 0)
                writer.print(' ');

            writer.print(objects[i]);
        }
    }

    public void printLine(Object... objects)
    {
        print(objects);
        writer.println();
    }

    public void close()
    {
        writer.close();
    }
}