Skip to content

Commit 2d4b79e

Browse files
authored
[20250804] BOJ / D5 / 반평면 땅따먹기 / 권혁준
1 parent 2a9d01a commit 2d4b79e

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
class Line {
51+
long a, b;
52+
Line(long a, long b) {
53+
this.a = a;
54+
this.b = b;
55+
}
56+
long get(long x) {
57+
return a*x + b;
58+
}
59+
}
60+
61+
class Node {
62+
int l, r;
63+
long s, e;
64+
Line line;
65+
Node(long s, long e, Line line) {
66+
this.l = -1;
67+
this.r = -1;
68+
this.s = s;
69+
this.e = e;
70+
this.line = line;
71+
}
72+
}
73+
74+
class LiChao {
75+
List<Node> tree;
76+
LiChao(long s, long e) {
77+
tree = new ArrayList<>();
78+
tree.add(new Node(s, e, new Line(0, -(long)1e18-1)));
79+
}
80+
81+
void update(int n, Line line) {
82+
long s = tree.get(n).s, e = tree.get(n).e, m = (s+e)>>1;
83+
84+
Line low = tree.get(n).line, high = line;
85+
if(low.get(s) > high.get(s)) {
86+
low = line;
87+
high = tree.get(n).line;
88+
}
89+
90+
if(low.get(e) <= high.get(e)) {
91+
tree.get(n).line = high;
92+
return;
93+
}
94+
95+
if(low.get(m) <= high.get(m)) {
96+
tree.get(n).line = high;
97+
if(tree.get(n).r == -1) {
98+
tree.get(n).r = tree.size();
99+
tree.add(new Node(m+1,e,new Line(0, -(long)1e18-1)));
100+
}
101+
update(tree.get(n).r, low);
102+
}
103+
else {
104+
tree.get(n).line = low;
105+
if(tree.get(n).l == -1) {
106+
tree.get(n).l = tree.size();
107+
tree.add(new Node(s,m,new Line(0, -(long)1e18-1)));
108+
}
109+
update(tree.get(n).l, high);
110+
}
111+
}
112+
113+
long query(int n, long x) {
114+
if(n == -1) return -(long)1e18 - 1;
115+
long s = tree.get(n).s, e = tree.get(n).e, m = (s+e)>>1;
116+
long v = tree.get(n).line.get(x);
117+
if(x <= m) return Math.max(v, query(tree.get(n).l, x));
118+
return Math.max(v, query(tree.get(n).r, x));
119+
}
120+
}
121+
122+
public class Main {
123+
124+
static IOController io;
125+
126+
//
127+
128+
static int Q;
129+
static LiChao tree;
130+
131+
public static void main(String[] args) throws Exception {
132+
133+
io = new IOController();
134+
135+
init();
136+
solve();
137+
138+
io.close();
139+
140+
}
141+
142+
static void init() throws Exception {
143+
144+
Q = io.nextInt();
145+
tree = new LiChao(-(long)1e12, (long)1e12);
146+
147+
}
148+
149+
static void solve() throws Exception {
150+
151+
while(Q-->0) {
152+
int op = io.nextInt();
153+
154+
if(op == 1) {
155+
long a = io.nextLong();
156+
long b = io.nextLong();
157+
tree.update(0, new Line(a,b));
158+
}
159+
else {
160+
long x = io.nextLong();
161+
io.write(tree.query(0, x) + "\n");
162+
}
163+
164+
}
165+
166+
}
167+
168+
}
169+
```

0 commit comments

Comments
 (0)