답안 #300152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300152 2020-09-16T21:47:40 Z model_code 식물 비교 (IOI20_plants) Java 11
100 / 100
3246 ms 108264 KB
// plants-yanhao-2k

import java.util.TreeMap;
import java.util.ArrayList;

class segtree {
	final int n_bits=18;
	final int inf = (int)1e9;
	int[] arr = new int [1<<(n_bits+1)];
	int[] lazyadd = new int [1<<(n_bits+1)];
    int node = 1;
    int lazy = 0;
	int low(int i) {
		return (i<<(Integer.numberOfLeadingZeros(i)-31+n_bits))-(1<<n_bits);
	}
	int high(int i) {
		return ((i+1)<<(Integer.numberOfLeadingZeros(i)-31+n_bits))-(1<<n_bits)-1;
	}
    void build(int[] v) {
        for(int i=0; i<(1<<n_bits); i++) {
            arr[i+(1<<n_bits)] = (i<v.length ? v[i] : inf);
        }
        for(int i=(1<<n_bits)-1; i>=1; i--) {
            arr[i] = Math.min(arr[2*i], arr[2*i+1]);
        }
        for(int i=0; i<(1<<(n_bits+1)); i++) {
            lazyadd[i] = 0;
        }
    }
    int value(int node) {
        arr[node] += lazyadd[node];
        if(node<(1<<n_bits)) {
            lazyadd[2*node] += lazyadd[node];
            lazyadd[2*node+1] += lazyadd[node];
        }
        lazyadd[node] = 0;
        return arr[node];
    }
    void update(int node, int left, int right, int change) {
        if(right>=high(node) && left<=low(node)) {
            lazyadd[node] += change;
        } else if(right<low(node) || left>high(node)) {
            return;
        } else {
            update(2*node, left, right, change);
            update(2*node+1, left, right, change);
            arr[node] = Math.min(value(node*2), value(node*2+1));
        }
    }

    void decr(int left, int right) {
        update(1, left, right, -1);
    }

    int find_zero(int node, int lt, int rg) {
        if(rg<low(node) || lt>high(node)) {
            return -1;
        }
        if(value(node)!=0) {
            return -1;
        }
        if(node >= (1<<n_bits)) {
            return arr[node]==0 ? node-(1<<n_bits) : -1;
        }
        int x = find_zero(node*2, lt, rg);
        if(x!=-1) return x;
        return find_zero(node*2+1, lt, rg);
    }
};

class plants {
	int[] ans;
	int[] lt;
	int[] rg;
	int[][] jump_lt;
	int[][] jump_rg;
	int k_global;
	segtree s = new segtree();
	int ctr = 0;
	int n;
	int[] permutation;
	int blocking(int rg, int k) {
		int lt = rg - k + 1;
		if(lt<0) {
			int p = s.find_zero(1, lt+n, n-1);
			if(p!=-1) {
				return p;
			}
			return s.find_zero(1,0,rg);
		} else {
			return s.find_zero(1,Math.max(0,lt),rg);
		}
	}

	void pull(int idx, int k) {
		int x;
		while((x=blocking(idx,k))!=idx) {
			pull(x,k);
		}
		permutation[idx] = ctr;
		ctr++;
		// decrement the k values before it, and push value in
		if(idx >= k-1) {
			s.update(1, idx-(k-1), idx, -1);
		} else {
			s.decr(0, idx);
			s.decr(idx-(k-1)+n, n-1);
		}
		s.update(1, idx, idx, 1<<29);
	}

	void init(int k, int[] r) {
		k_global = k;
		s.build(r);
		n = r.length;
		lt = new int[n+1];
		rg = new int[n+1];
		permutation = new int[n];
		while(ctr<n) {
			int p = s.find_zero(1,0,n-1);
			assert(p!=-1);
			pull(p,k);
		}
		TreeMap<Integer, Integer> m = new TreeMap<>();
		for (int i=0; i<k; i++) {
			m.put(permutation[i], i);
		}
		m.put(1<<20, n);
		ans = new int[n+1];
		for(int i=0; i<n; i++) {
			rg[i] = m.higherEntry(permutation[i]).getValue();
			lt[(i+k-1)%n] = m.higherEntry(permutation[(i+k-1)%n]).getValue();
			m.remove(permutation[i]);
			m.put(permutation[(i+k)%n], (i+k)%n);
		}
		jump_lt = new int[18][n];
		jump_rg = new int[18][n];
		for(int i=0; i<n; i++) {
			jump_lt[0][i] = lt[i]==n ? 0 : (i+n-lt[i])%n;
			jump_rg[0][i] = rg[i]==n ? 0 : (rg[i]+n-i)%n;
		}
		for(int i=1; i<18; i++) {
			for(int j=0; j<n; j++) {
				jump_lt[i][j] = jump_lt[i-1][j] + jump_lt[i-1][(j+n-jump_lt[i-1][j])%n];
				jump_rg[i][j] = jump_rg[i-1][j] + jump_rg[i-1][(j+n+jump_rg[i-1][j])%n];
				if(jump_lt[i][j]>n) {
					jump_lt[i][j] = 0;
				}
				if(jump_rg[i][j]>n) {
					jump_rg[i][j] = 0;
				}
			}
		}		
	}
	
	int compare_plants(int x, int y) {
		// check for the direct x->y path
		int z = x;
		for(int i=17; i>=0; i--) {
			if(jump_rg[i][z] < y-z) {
				z = z + jump_rg[i][z];
			}
		}
		if(permutation[z] < permutation[y] && (y-z+n)%n < k_global) return 1;
		// direct y->x path
		z = y;
		for(int i=17; i>=0; i--) {
			if(jump_lt[i][z] < z-x) {
				z = z - jump_lt[i][z];
			}
		}
		if(permutation[z] < permutation[x] && (z-x+n)%n < k_global) return -1;
		
		// look for x->y wraparounds
		z = x;
		for(int i=17; i>=0; i--) {
			if(jump_lt[i][z] < (z+n-y)%n) {
				z = (z + n - jump_lt[i][z])%n;
			}
		}
		if(permutation[z] < permutation[y] && (z-y+n)%n < k_global) return 1;
		
		// look for y->x wraparounds
		z = y;
		for(int i=17; i>=0; i--) {
			if(jump_rg[i][z] < (x+n-z)%n) {
				z = (z + jump_rg[i][z])%n;
			}
		}
		if(permutation[z] < permutation[x] && (x-z+n)%n < k_global) return -1;
		
		return 0;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 14696 KB Output is correct
2 Correct 103 ms 14788 KB Output is correct
3 Correct 108 ms 14692 KB Output is correct
4 Correct 99 ms 14396 KB Output is correct
5 Correct 101 ms 14312 KB Output is correct
6 Correct 419 ms 29932 KB Output is correct
7 Correct 733 ms 37092 KB Output is correct
8 Correct 1745 ms 86736 KB Output is correct
9 Correct 1804 ms 86980 KB Output is correct
10 Correct 1870 ms 86128 KB Output is correct
11 Correct 1588 ms 86220 KB Output is correct
12 Correct 1604 ms 85752 KB Output is correct
13 Correct 1204 ms 86416 KB Output is correct
14 Correct 1563 ms 84728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 14556 KB Output is correct
2 Correct 118 ms 14612 KB Output is correct
3 Correct 105 ms 14804 KB Output is correct
4 Correct 103 ms 14712 KB Output is correct
5 Correct 135 ms 16840 KB Output is correct
6 Correct 259 ms 18704 KB Output is correct
7 Correct 604 ms 33876 KB Output is correct
8 Correct 179 ms 17864 KB Output is correct
9 Correct 235 ms 18704 KB Output is correct
10 Correct 610 ms 33256 KB Output is correct
11 Correct 555 ms 32772 KB Output is correct
12 Correct 679 ms 32896 KB Output is correct
13 Correct 639 ms 34556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 14556 KB Output is correct
2 Correct 118 ms 14612 KB Output is correct
3 Correct 105 ms 14804 KB Output is correct
4 Correct 103 ms 14712 KB Output is correct
5 Correct 135 ms 16840 KB Output is correct
6 Correct 259 ms 18704 KB Output is correct
7 Correct 604 ms 33876 KB Output is correct
8 Correct 179 ms 17864 KB Output is correct
9 Correct 235 ms 18704 KB Output is correct
10 Correct 610 ms 33256 KB Output is correct
11 Correct 555 ms 32772 KB Output is correct
12 Correct 679 ms 32896 KB Output is correct
13 Correct 639 ms 34556 KB Output is correct
14 Correct 898 ms 45960 KB Output is correct
15 Correct 2729 ms 104852 KB Output is correct
16 Correct 938 ms 45676 KB Output is correct
17 Correct 3027 ms 108264 KB Output is correct
18 Correct 2054 ms 94348 KB Output is correct
19 Correct 2269 ms 93820 KB Output is correct
20 Correct 2798 ms 99664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 14616 KB Output is correct
2 Correct 135 ms 14576 KB Output is correct
3 Correct 491 ms 31928 KB Output is correct
4 Correct 1954 ms 89640 KB Output is correct
5 Correct 1943 ms 90284 KB Output is correct
6 Correct 2016 ms 90424 KB Output is correct
7 Correct 2553 ms 98964 KB Output is correct
8 Correct 3246 ms 99100 KB Output is correct
9 Correct 1816 ms 88600 KB Output is correct
10 Correct 1664 ms 86324 KB Output is correct
11 Correct 1138 ms 85360 KB Output is correct
12 Correct 1957 ms 87240 KB Output is correct
13 Correct 1863 ms 95264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 14636 KB Output is correct
2 Correct 98 ms 14576 KB Output is correct
3 Correct 105 ms 14728 KB Output is correct
4 Correct 123 ms 14736 KB Output is correct
5 Correct 121 ms 14952 KB Output is correct
6 Correct 181 ms 17488 KB Output is correct
7 Correct 332 ms 21692 KB Output is correct
8 Correct 306 ms 20812 KB Output is correct
9 Correct 319 ms 21796 KB Output is correct
10 Correct 331 ms 21780 KB Output is correct
11 Correct 331 ms 20264 KB Output is correct
12 Correct 308 ms 21632 KB Output is correct
13 Correct 280 ms 22248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 115 ms 14744 KB Output is correct
2 Correct 119 ms 14620 KB Output is correct
3 Correct 109 ms 14664 KB Output is correct
4 Correct 105 ms 14392 KB Output is correct
5 Correct 203 ms 17272 KB Output is correct
6 Correct 1842 ms 91476 KB Output is correct
7 Correct 1796 ms 64140 KB Output is correct
8 Correct 1732 ms 97028 KB Output is correct
9 Correct 2553 ms 102928 KB Output is correct
10 Correct 1867 ms 87136 KB Output is correct
11 Correct 2061 ms 100360 KB Output is correct
12 Correct 1628 ms 90196 KB Output is correct
13 Correct 1772 ms 90420 KB Output is correct
14 Correct 1728 ms 90132 KB Output is correct
15 Correct 2078 ms 99484 KB Output is correct
16 Correct 1418 ms 87368 KB Output is correct
17 Correct 1565 ms 86736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 14696 KB Output is correct
2 Correct 103 ms 14788 KB Output is correct
3 Correct 108 ms 14692 KB Output is correct
4 Correct 99 ms 14396 KB Output is correct
5 Correct 101 ms 14312 KB Output is correct
6 Correct 419 ms 29932 KB Output is correct
7 Correct 733 ms 37092 KB Output is correct
8 Correct 1745 ms 86736 KB Output is correct
9 Correct 1804 ms 86980 KB Output is correct
10 Correct 1870 ms 86128 KB Output is correct
11 Correct 1588 ms 86220 KB Output is correct
12 Correct 1604 ms 85752 KB Output is correct
13 Correct 1204 ms 86416 KB Output is correct
14 Correct 1563 ms 84728 KB Output is correct
15 Correct 105 ms 14556 KB Output is correct
16 Correct 118 ms 14612 KB Output is correct
17 Correct 105 ms 14804 KB Output is correct
18 Correct 103 ms 14712 KB Output is correct
19 Correct 135 ms 16840 KB Output is correct
20 Correct 259 ms 18704 KB Output is correct
21 Correct 604 ms 33876 KB Output is correct
22 Correct 179 ms 17864 KB Output is correct
23 Correct 235 ms 18704 KB Output is correct
24 Correct 610 ms 33256 KB Output is correct
25 Correct 555 ms 32772 KB Output is correct
26 Correct 679 ms 32896 KB Output is correct
27 Correct 639 ms 34556 KB Output is correct
28 Correct 898 ms 45960 KB Output is correct
29 Correct 2729 ms 104852 KB Output is correct
30 Correct 938 ms 45676 KB Output is correct
31 Correct 3027 ms 108264 KB Output is correct
32 Correct 2054 ms 94348 KB Output is correct
33 Correct 2269 ms 93820 KB Output is correct
34 Correct 2798 ms 99664 KB Output is correct
35 Correct 106 ms 14616 KB Output is correct
36 Correct 135 ms 14576 KB Output is correct
37 Correct 491 ms 31928 KB Output is correct
38 Correct 1954 ms 89640 KB Output is correct
39 Correct 1943 ms 90284 KB Output is correct
40 Correct 2016 ms 90424 KB Output is correct
41 Correct 2553 ms 98964 KB Output is correct
42 Correct 3246 ms 99100 KB Output is correct
43 Correct 1816 ms 88600 KB Output is correct
44 Correct 1664 ms 86324 KB Output is correct
45 Correct 1138 ms 85360 KB Output is correct
46 Correct 1957 ms 87240 KB Output is correct
47 Correct 1863 ms 95264 KB Output is correct
48 Correct 101 ms 14636 KB Output is correct
49 Correct 98 ms 14576 KB Output is correct
50 Correct 105 ms 14728 KB Output is correct
51 Correct 123 ms 14736 KB Output is correct
52 Correct 121 ms 14952 KB Output is correct
53 Correct 181 ms 17488 KB Output is correct
54 Correct 332 ms 21692 KB Output is correct
55 Correct 306 ms 20812 KB Output is correct
56 Correct 319 ms 21796 KB Output is correct
57 Correct 331 ms 21780 KB Output is correct
58 Correct 331 ms 20264 KB Output is correct
59 Correct 308 ms 21632 KB Output is correct
60 Correct 280 ms 22248 KB Output is correct
61 Correct 586 ms 31620 KB Output is correct
62 Correct 1020 ms 38280 KB Output is correct
63 Correct 2557 ms 86320 KB Output is correct
64 Correct 1847 ms 89624 KB Output is correct
65 Correct 1946 ms 94496 KB Output is correct
66 Correct 2086 ms 97128 KB Output is correct
67 Correct 2522 ms 105488 KB Output is correct
68 Correct 2067 ms 87348 KB Output is correct
69 Correct 2157 ms 103032 KB Output is correct
70 Correct 1880 ms 90140 KB Output is correct
71 Correct 2210 ms 91044 KB Output is correct
72 Correct 2004 ms 91344 KB Output is correct
73 Correct 2284 ms 103340 KB Output is correct
74 Correct 2284 ms 87104 KB Output is correct
75 Correct 1578 ms 88620 KB Output is correct