結果

問題 No.618 labo-index
ユーザー 夕叢霧香(ゆうむらきりか)
提出日時 2017-11-29 18:52:06
言語 Java
(openjdk 23)
結果
AC  
実行時間 797 ms / 6,000 ms
コード長 3,943 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 79,060 KB
実行使用メモリ 70,044 KB
最終ジャッジ日時 2024-11-27 19:06:26
合計ジャッジ時間 18,129 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    static int q;
    static int[]t;
    static long[]x;
    Main(){
        long b=0;
        long[]c=new long[q];
        int p=0;
        for(int i=0;i<q;++i)
            if(t[i]==1){
                c[p++]=x[i]-b;
                x[i]-=b;
            }
            else if(t[i]==3)
                b+=x[i];
            else
                x[i]=c[(int)x[i]-1];
        Arrays.sort(c,0,p);
        int p2=0;
        Map<Long,Integer>hm=new HashMap<Long,Integer>();
        for(int i=0;i<p;++i)
            if(i>0&&c[i]==c[i-1]);
            else hm.put(c[i],p2++);
        long[]cc=new long[p2];
        p2=0;
        for(int i=0;i<p;++i)
            if(i>0&&c[i]==c[i-1]);
            else cc[p2++]=c[i];
        PlusSegmentTree st=new PlusSegmentTree();
        b=0;
        for(int i=0;i<q;++i){
            if(t[i]==1){
                int j=hm.get(x[i]);
                st.update(j,st.query(j,j+1)+1);
            }else if(t[i]==2){
                int j=hm.get(x[i]);
                st.update(j,st.query(j,j+1)-1);
            }else
                b+=x[i];
            int ps=0,fl=p2+1;
            while(fl-ps>1){
                int y=(ps+fl)/2;
                if(st.query(y,p2)>=(y>0?cc[y-1]+1+b:-1L<<50))
                    ps=y;
                else
                    fl=y;
            }
            if(false){
                out.println("b="+b);
                out.print("st:");
                for(int j=0;j<p2;++j){
                    out.print(" "+st.query(j,j+1));
                }
                out.println();
                out.println("ps="+ps);
            }
            out.println(Math.min(st.query(ps,p2),ps==p2?Long.MAX_VALUE:cc[ps]+b));
        }
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        q=sc.nextInt();
        t=new int[q];
        x=new long[q];
        for(int i=0;i<q;++i){
            t[i]=sc.nextInt();
            x[i]=sc.nextLong();
        }
        new Main();
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
class PlusSegmentTree {
    static final int SIZE = 1 << 17;
    long[] seg;
    PlusSegmentTree() {
	this.seg = new long[2 * SIZE];
    }
    void update(int x, long value) {
	x += SIZE - 1;
	this.seg[x] = value;
	while (x > 0) {
	    x = (x - 1) / 2;
	    this.seg[x] = this.seg[2 * x + 1] + this.seg[2 * x + 2];
	}
    }
    long query(int l, int r) {
	l += SIZE - 1;
	r += SIZE - 1;
        long y = 0;
	while (l < r) {
	    if ((l & 1) == 0) {
		y += this.seg[l];
	    }
	    if ((r & 1) == 0) {
		y += this.seg[r - 1];
	    }
	    l /= 2;
	    r = (r - 1) / 2;
	}
	return y;
    }
}
0