Skip to content

Commit 75e2282

Browse files
authored
[20250624] BOJ / P5 / 집합 연산 / 권혁준
1 parent f4b89dc commit 75e2282

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N, Q;
57+
static TreeSet<Long> S;
58+
static long[] B;
59+
60+
public static void main(String[] args) throws Exception {
61+
62+
io = new IOController();
63+
64+
init();
65+
solve();
66+
67+
io.close();
68+
69+
}
70+
71+
public static void init() throws Exception {
72+
73+
N = io.nextInt();
74+
S = new TreeSet<>();
75+
for(int i=0;i<N;i++) S.add(io.nextLong());
76+
Q = io.nextInt();
77+
B = new long[Q];
78+
for(int i=0;i<Q;i++) B[i] = io.nextInt();
79+
80+
}
81+
82+
static void solve() throws Exception {
83+
84+
long sum = 0;
85+
for(long i:S) sum += i;
86+
87+
S.add(Long.MAX_VALUE);
88+
89+
Deque<long[]> list = new ArrayDeque<>();
90+
91+
long left = N;
92+
93+
// 처음에 제거되는 경우가 있으면 먼저 처리
94+
if(S.contains((long)N)) {
95+
while(left >= 0 && S.contains(left)) S.remove(left--);
96+
list.offerLast(new long[]{N, left});
97+
}
98+
99+
// 추가, 제거 반복
100+
while(true) {
101+
102+
long next = S.ceiling(left);
103+
if(next == Long.MAX_VALUE){
104+
list.offerLast(new long[]{left, next});
105+
break;
106+
}
107+
S.add(left);
108+
list.offerLast(new long[]{left, next});
109+
S.remove(next);
110+
while(left >= 0 && S.contains(left)) S.remove(left--);
111+
list.offerLast(new long[]{next, left});
112+
113+
}
114+
115+
for(long q : B) {
116+
117+
while(q > 0) {
118+
long[] cur = list.pollFirst();
119+
long l = cur[0], r = cur[1], cnt = Math.abs(l-r);
120+
121+
if(q < cnt) {
122+
if(l > r) {
123+
list.offerFirst(new long[]{l-q, r});
124+
r = l-q;
125+
}
126+
else {
127+
list.offerFirst(new long[]{l+q, r});
128+
r = l+q;
129+
}
130+
q = 0;
131+
}
132+
else q -= cnt;
133+
134+
if(l > r) {
135+
sum -= l*(l+1)/2 - r*(r+1)/2;
136+
}
137+
else {
138+
sum += (r-1)*r/2 - (l-1)*l/2;
139+
}
140+
141+
}
142+
143+
io.write(sum + "\n");
144+
145+
}
146+
147+
148+
149+
}
150+
151+
}
152+
```

0 commit comments

Comments
 (0)