QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#64484 | #4425. Cake | As3b_team_f_masr# | RE | 0ms | 0kb | C++ | 2.0kb | 2022-11-24 21:53:39 | 2022-11-24 21:53:41 |
Judging History
answer
#include <bits/stdc++.h>
typedef long double ld;
typedef long long ll;
using namespace std;
int di[] = {1, 0, -1, -1, 0, 1, -1, 1};
int dj[] = {1, 1, 0, -1, -1, 0, 1, -1};
const ll oo = 1e18, MOD = 998244353;
const int N = 5e5 + 5, M = 1e6 + 5;
const ld PI = acos(-1.0), EPS = 1e-9;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int t, n, a[2][N], b[2][N], tree[N];
void add(int pos, ll val) {
for (++pos; pos < N; pos += (pos & (-pos)))tree[pos - 1] += val;
}
ll get(int pos) {
ll ret = 0;
for (++pos; pos; pos -= (pos & (-pos)))ret += tree[pos - 1];
return ret;
}
//#define endl '\n'
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("farm.in", "r", stdin);
//memset(dp, -1, sizeof dp);
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) tree[i] = 0;
for (int i = 1; i <= n; i++) cin >> a[1][i];
for (int i = 1; i <= n; i++) cin >> a[2][i];
for (int i = 1; i <= n; i++) cin >> b[1][i];
for (int i = 1; i <= n; i++) cin >> b[2][i];
map<pair<pair<int, int>, int>, vector<int> > mp;
for (int i = n; i >= 1; i--) {
mp[{{a[1][i], a[2][i]}, i & 1}].push_back(i);
mp[{{a[2][i], a[1][i]}, !(i & 1)}].push_back(i);
}
ll ans = 0;
bool no = 0;
for (int i = 1; i <= n; i++) {
if (mp[{{b[1][i], b[2][i]}, i & 1}].empty()) {
no = 1;
break;
}
int idx = mp[{{b[1][i], b[2][i]}, i & 1}].back();
ans += mp[{{b[1][i], b[2][i]}, i & 1}].back() + get(n) - get(idx) - i;
add(idx, 1);
mp[{{b[1][i], b[2][i]}, i & 1}].pop_back();
mp[{{b[2][i], b[1][i]}, !(i & 1)}].pop_back();
}
cout << (no ? -1 : ans) << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 500000 471518156 319758862 812815356 520822448 129241996 461169933 796713727 608641317 281180101 953966756 749634941 274104949 996181952 88142916 998544672 125597509 991731126 974767231 338911715 674197249 167602044 682799026 226927279 703198907 216742488 8185420 94921423 690039818 859329736 45428...