forked from Anmol2307/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYODANESS.cpp
133 lines (109 loc) · 2.34 KB
/
YODANESS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Author: Hakobyan Tigran
*/
#pragma comment(linker, "/STACK:60777216")
#define printTime(begin, end) printf("%.3lf\n", (double)(end - begin) / (double)CLOCKS_PER_SEC)
#include <string.h>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <functional>
#include <complex>
#include <iostream>
#include <fstream>
#include <sstream>
#include <bitset>
#include <limits>
#include <ctime>
#include <cassert>
#include <valarray>
using namespace std;
#define IN(a) freopen(a, "r", stdin)
#define OUT(a) freopen(a, "w", stdout)
#define mp(a, b) make_pair(a, b)
#define det(a, b, c, d) (a * d - c * b)
#define sbstr(s, i, j) s.substr(i, j - i + 1)
#define clear(dp) memset(dp, -1, sizeof(dp))
#define reset(arr) memset(arr, 0, sizeof(arr))
#define EPS 1e-9
#define PI acos(-1.0)
#define IINF 1000000000
#define LINF 6000000000000000000LL
const int MOD = 10007;
const int maxN = 30003;
const int HASH_SIZE = 10007;
int n;
char word[22];
int t[maxN], a[maxN];
int hash_values[maxN];
int hash_table[HASH_SIZE];
int hash_value (const char *s, int len) {
int h = 0;
int p = 31;
for(int i = len - 1; i >= 0; --i) {
h = s[i] + p * h % MOD;
if(h >= MOD) {
h -= MOD;
}
}
if(h >= MOD) {
printf("I have something wrong in implementation!!!!");
}
return h;
}
void init () {
memset(t, 0, sizeof(t));
memset(hash_table, -1, sizeof(hash_table));
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%s", word);
hash_values[i] = hash_value(word, strlen(word));
}
for(int i = 0; i < n; ++i) {
scanf("%s", word);
hash_table[hash_value(word, strlen(word))] = i;
}
for(int i = 0; i < n; ++i) {
a[i] = hash_table[hash_values[i]];
}
}
void update (int i, int delta) {
for( ; i < n; i |= i + 1)
t[i] += delta;
}
int get (int i) {
int s = 0;
if(i < 0)
return 0;
for( ; i >= 0; i = (i & (i + 1)) - 1)
s += t[i];
return s;
}
void solve () {
int answer = 0;
for(int i = 0; i < n; ++i) {
int dbg = get(a[i] - 1);
answer += a[i] - dbg;
update(a[i], 1);
}
printf("%d\n", answer);
}
int main () {
int test_case;
scanf("%d", &test_case);
while(test_case--) {
init();
solve();
}
return 0;
}