Skip to content

Commit affd5cf

Browse files
committed
boj 4442 빌보의 생일
1 parent 6922859 commit affd5cf

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

세그먼트 트리/4442.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <unordered_map>
3+
#include <string>
4+
#include <cstring>
5+
#define MAX 100001
6+
using namespace std;
7+
typedef long long ll;
8+
9+
unordered_map<string, int> m;
10+
int idxList[MAX];
11+
ll tree[MAX << 2];
12+
int N;
13+
14+
void init() {
15+
m.clear();
16+
memset(tree, 0, sizeof(tree));
17+
}
18+
19+
ll update(int node, int l, int r, int idx, int diff) {
20+
if (idx < l || r < idx) return tree[node];
21+
if (l == r) return ++tree[node];
22+
23+
int m = (l + r) >> 1;
24+
return tree[node] = update(node << 1, l, m, idx, diff) + update((node << 1) + 1, m + 1, r, idx, diff);
25+
}
26+
27+
ll query(int node, int l, int r, int s, int e) {
28+
if (l > e || r < s) return 0LL;
29+
if (s <= l && r <= e) return tree[node];
30+
31+
int m = (l + r) >> 1;
32+
return query(node << 1, l, m, s, e) + query((node << 1) + 1, m + 1, r, s, e);
33+
}
34+
35+
void func() {
36+
ll ret = 0LL;
37+
for (int i = 0; i < N; i++) {
38+
ret += query(1, 1, N, idxList[i] + 1, N);
39+
update(1, 1, N, idxList[i], 1);
40+
}
41+
cout << ret << '\n';
42+
}
43+
44+
void input() {
45+
cin >> N;
46+
if (!N) exit(0);
47+
48+
string str;
49+
int cnt = 0;
50+
for (int i = 0; i < N; i++) {
51+
cin >> str;
52+
m[str] = ++cnt;
53+
}
54+
55+
for (int i = 0; i < N; i++) {
56+
cin >> str;
57+
idxList[i] = m[str];
58+
}
59+
}
60+
61+
int main() {
62+
cin.tie(nullptr); cout.tie(nullptr);
63+
ios::sync_with_stdio(false);
64+
65+
while (1) {
66+
input();
67+
func();
68+
init();
69+
}
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)