Skip to content

Commit de1d8b1

Browse files
authored
[20250314] BOJ / P5 / Random Generator / 권혁준
1 parent 5ca5312 commit de1d8b1

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class SegTree{
7+
int[] tree;
8+
SegTree(int size){
9+
tree = new int[size*4+1];
10+
}
11+
12+
void upt(int s, int e, int i, int v, int n) {
13+
if(s == e) {
14+
tree[n] = v;
15+
return;
16+
}
17+
int m=(s+e)>>1;
18+
if(i <= m) upt(s, m, i, v, n*2);
19+
else upt(m+1, e, i, v, n*2+1);
20+
tree[n] = tree[n*2] + tree[n*2+1];
21+
}
22+
23+
int find(int s, int e, int v, int n) {
24+
if(s == e) return s;
25+
int m=(s+e)>>1;
26+
if(v <= tree[n*2]) return find(s, m, v, n*2);
27+
return find(m+1, e, v-tree[n*2], n*2+1);
28+
}
29+
}
30+
31+
class Main {
32+
33+
// IO field
34+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
35+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
36+
static StringTokenizer st = new StringTokenizer("");
37+
38+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
39+
static int nextInt() throws Exception {
40+
if(!st.hasMoreTokens()) nextLine();
41+
return Integer.parseInt(st.nextToken());
42+
}
43+
static long nextLong() throws Exception {
44+
if(!st.hasMoreTokens()) nextLine();
45+
return Long.parseLong(st.nextToken());
46+
}
47+
static void bwEnd() throws Exception {bw.flush();bw.close();}
48+
49+
// Additional field
50+
51+
static int N;
52+
static SegTree seg;
53+
54+
public static void main(String[] args) throws Exception {
55+
56+
ready();
57+
solve();
58+
59+
bwEnd();
60+
61+
}
62+
63+
static void ready() throws Exception{
64+
65+
N = nextInt();
66+
seg = new SegTree(N);
67+
for(int i=1;i<=N;i++) seg.upt(1, N, i, nextInt(), 1);
68+
69+
}
70+
71+
static void solve() throws Exception{
72+
73+
for(int i=1;i<=N;i++) {
74+
int p = nextInt();
75+
int x = seg.find(1, N, p, 1);
76+
bw.write(x + " ");
77+
seg.upt(1, N, x, 0, 1);
78+
}
79+
80+
}
81+
82+
}
83+
84+
```

0 commit comments

Comments
 (0)