█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 5194

Problema 0x9c - Máximo elemento en un subarreglo enorme

  • Autor: Ikerlb
  • Fecha: 2021-10-26 08:59:36 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.132 s 12 KBi
#2
Correcto
0.103 s 13 KBi
#3
Correcto
0.133 s 11 KBi
#4
Correcto
0.1 s 23 KBi
#5
Correcto
0.128 s 11 KBi
#6
Correcto
0.116 s 11 KBi
#7
Correcto
0.109 s 12 KBi
#8
Correcto
0.114 s 11 KBi
#9
Correcto
0.118 s 13 KBi
#10
Correcto
0.133 s 11 KBi
#11
Correcto
0.438 s 31 KBi
#12
Correcto
0.443 s 31 KBi
#13
Correcto
0.458 s 31 KBi
#14
Correcto
0.454 s 38 KBi
#15
Correcto
0.381 s 30 KBi
Puntos totales: 100 / 100

Código

import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.FileInputStream;

class SegmentTree{
    private int[] arr;
    private int[][] tree;

    public SegmentTree(int[] arr){
        this.arr = arr;
        this.tree = new int[4 * arr.length][2];

        this.buildTree(1, 0, arr.length - 1);
    }

    private int[] buildTree(int i, int l, int r){
        if(l == r){
            this.tree[i][0] = this.arr[l];
            this.tree[i][1] = l;
            return this.tree[i];
        }
        int mid = (l + r) >> 1;
        int[] res, lsq, rsq;
        lsq = this.buildTree(2 * i, l, mid);
        rsq = this.buildTree(2 * i + 1, mid + 1, r);
        if(lsq[0] == rsq[0]){
            res = new int[2];
            res[0] = lsq[0];
            res[1] = Math.min(lsq[1], rsq[1]);
        }
        else if(lsq[0] > rsq[0])
            res = lsq;
        else
            res = rsq;
        this.tree[i] = res;
        return res;
    }

    public int query(int l, int r){
        return this.query(1, l, r, 0, this.arr.length - 1)[1];
    }

    private int[] query(int i, int l, int r, int ll, int rr){
        if(l == ll && r == rr)
            return this.tree[i];
        int mid = (ll + rr) >> 1;
        if(r <= mid)
            return this.query(2 * i, l, r, ll, mid);
        else if(l > mid)
            return this.query(2 * i + 1, l, r, mid + 1, rr);
        int[] lsq, rsq;
        lsq = this.query(2 * i, l, mid, ll, mid);
        rsq = this.query(2 * i + 1, mid + 1, r, mid + 1, rr);
        if(lsq[0] == rsq[0]){
            int[] res = {lsq[0], Math.min(rsq[1], lsq[1])};
            return res;
        }
        else if(lsq[0] > rsq[0])
            return lsq;
        return rsq;
    }

}


class Main{
    static class Reader{
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader(){
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException{
            din = new DataInputStream(
                new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException{
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') {
                    if (cnt != 0)
                        break;
                    else
                        continue;
                }
                buf[cnt++] = (byte)c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException{
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException{
            bytesRead = din.read(buffer, bufferPointer = 0,
                                 BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException{
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException{
            if (din == null)
                return;
            din.close();
        }
    }

    public static void main(String[] args) throws IOException{
        Reader fr = new Reader();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int N = fr.nextInt();
        int[] arr = new int[N];
        for(int i = 0; i < N; i++)
            arr[i] = fr.nextInt();

        SegmentTree st = new SegmentTree(arr);

        int Q = fr.nextInt();
        int L, R;
        for(int i = 0; i < Q; i++){
            L = fr.nextInt();
            R = fr.nextInt();
            bw.write(st.query(L, R)+"\n");
        }
        bw.flush();
    }
}