結果
| 問題 | No.121 傾向と対策:門松列(その2) | 
| コンテスト | |
| ユーザー |  夕叢霧香(ゆうむらきりか) | 
| 提出日時 | 2018-01-22 03:03:58 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                TLE
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 4,620 bytes | 
| コンパイル時間 | 2,930 ms | 
| コンパイル使用メモリ | 79,936 KB | 
| 実行使用メモリ | 386,336 KB | 
| 最終ジャッジ日時 | 2024-12-25 22:04:44 | 
| 合計ジャッジ時間 | 22,747 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 8 TLE * 1 | 
ソースコード
import java.io.*;
import java.util.*;
class Main {
    static final long calc(int[] a){
        int n=a.length;
        int[]b=a.clone();
        {
            // coord comp
            int[]c=b.clone();
            Arrays.sort(c);
            int pos=0;
            Map<Integer,Integer>hm=new HashMap<Integer,Integer>();
            for(int i=0;i<n;++i){
                if(i==0||c[i]!=c[i-1]){
                    hm.put(c[i],pos);
                    pos++;
                }
            }
            for(int i=0;i<n;++i)b[i]=hm.get(b[i]);
        }
        //System.err.println("a="+Arrays.toString(a));
        //System.err.println("coord(a)="+Arrays.toString(b));
        PlusSegmentTree seg=new PlusSegmentTree();
        long[]left=new long[n];
        for(int i=0;i<n;++i){
            left[i]=seg.query(0,b[i]);
            seg.update(b[i],seg.query(b[i],b[i]+1)+1);
        }
        for(int i=0;i<n;++i)seg.update(i, 0);
        long[]right=new long[n];
        for(int i=n-1;i>=0;--i){
            right[i]=seg.query(b[i]+1,n);
            seg.update(b[i],seg.query(b[i],b[i]+1)+1);
        }
        long ans=0;
        for(int i=0;i<n;++i)ans+=(long)left[i]*(long)right[i];
        return ans;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]a=sc.nextIntArray(n);
        int[]b=a.clone();
        Arrays.sort(b);
        long dis=0;
        int pos=0;
        int[]e=new int[n],f=new int[n];
        {
            int cnt=0;
            for(int i=0;i<n;++i){
                if(i>0&&b[i]!=b[i-1]){
                    e[pos]=b[i-1];
                    f[pos]=cnt;
                    pos++;
                    cnt=0;
                }
                cnt++;
            }
            e[pos]=b[n-1];
            f[pos]=cnt;
            pos++;
            e=Arrays.copyOf(e,pos);
            f=Arrays.copyOf(f,pos);
        }
        dis=(long)n*(long)(n-1)*(long)(n-2);
        for(int i=0;i<pos;++i){
            long tmp=f[i];
            tmp=tmp*(tmp-1)*(n-tmp)*3;
            dis-=tmp;
            tmp=f[i];
            tmp=tmp*(tmp-1)*(tmp-2);
            dis-=tmp;
        }
        //System.err.println(Arrays.toString(e));
        //System.err.println(Arrays.toString(f));
        //System.err.println(dis);
        dis/=6;
        // eliminate increasing subseq. of length 3
        // eliminate decreasing subseq. of length 3
        dis-=calc(a);
        for(int i=0;i<n;++i)a[i]=-a[i];
        dis-=calc(a);
        out.println(dis);
        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;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
class PlusSegmentTree {
    static final int SIZE = 1 << 20;
    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 = y + this.seg[l];
	    }
	    if ((r & 1) == 0) {
		y = y + this.seg[r - 1];
	    }
	    l /= 2;
	    r = (r - 1) / 2;
	}
	return y;
    }
}
            
            
            
        