Skip to content

Commit 224319c

Browse files
authored
Merge pull request #912 from AlgorithmWithGod/khj20006
[20250917] BOJ / D5 / Historical Research / 권혁준
2 parents 9e64f7f + ec47747 commit 224319c

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
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())
24+
nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static int N, Q, bucketSize;
58+
static int[] a, origin;
59+
static int[][] queries;
60+
static long[] seg;
61+
62+
public static void update(int s, int e, int i, long v, int n) {
63+
if(s == e) {
64+
seg[n] += v;
65+
return;
66+
}
67+
int m = (s+e)>>1;
68+
if(i <= m) update(s,m,i,v,n*2);
69+
else update(m+1,e,i,v,n*2+1);
70+
seg[n] = Math.max(seg[n*2], seg[n*2+1]);
71+
}
72+
73+
public static void main(String[] args) throws Exception {
74+
75+
io = new IOController();
76+
77+
N = io.nextInt();
78+
Q = io.nextInt();
79+
bucketSize = (int)Math.sqrt(N);
80+
a = new int[N];
81+
int[][] tmp = new int[N][];
82+
origin = new int[N];
83+
84+
for(int i=0;i<N;i++) {
85+
a[i] = io.nextInt();
86+
tmp[i] = new int[]{a[i], i};
87+
}
88+
89+
Arrays.sort(tmp, (c,d) -> c[0]-d[0]);
90+
int value = 0, prev = tmp[0][0];
91+
origin[0] = prev;
92+
for(int i=0;i<N;i++) {
93+
int v = tmp[i][0], x = tmp[i][1];
94+
if(prev != v) value++;
95+
a[x] = value;
96+
origin[value] = v;
97+
prev = v;
98+
}
99+
100+
queries = new int[Q][];
101+
for(int i=0;i<Q;i++) {
102+
int s = io.nextInt()-1;
103+
int e = io.nextInt()-1;
104+
queries[i] = new int[]{s,e,i};
105+
}
106+
107+
Arrays.sort(queries, (c,d) -> {
108+
if(c[0] / bucketSize == d[0] / bucketSize) {
109+
return c[1]-d[1];
110+
}
111+
return c[0] / bucketSize - d[0] / bucketSize;
112+
});
113+
114+
long[] ans = new long[Q];
115+
seg = new long[524288];
116+
int s = 0, e = -1;
117+
for(int i=0;i<Q;i++) {
118+
int ns = queries[i][0];
119+
int ne = queries[i][1];
120+
int idx = queries[i][2];
121+
122+
while(s < ns) {
123+
int val = a[s];
124+
int org = origin[val];
125+
update(0,100001,val,-org,1);
126+
s++;
127+
}
128+
while(ns < s) {
129+
int val = a[--s];
130+
int org = origin[val];
131+
update(0,100001,val,org,1);
132+
}
133+
while(e < ne) {
134+
int val = a[++e];
135+
int org = origin[val];
136+
update(0,100001,val,org,1);
137+
}
138+
while(ne < e) {
139+
int val = a[e];
140+
int org = origin[val];
141+
update(0,100001,val,-org,1);
142+
e--;
143+
}
144+
145+
ans[idx] = seg[1];
146+
147+
}
148+
149+
for(int i=0;i<Q;i++) io.write(ans[i] + "\n");
150+
151+
io.close();
152+
153+
}
154+
155+
}
156+
```

0 commit comments

Comments
 (0)