#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
int st[12 * N];
void Add(int node, int l, int r, int x, int y) {
smax(st[node], y);
if (l == r) return;
int mid = l + r >> 1;
if (x <= mid) Add(2 * node, l, mid, x, y);
else Add(2 * node + 1, mid + 1, r, x, y);
}
int Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l || ql > qr) return -1;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return max(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int bit[3 * N];
void AddBit(int x) {
x += 1;
for (; x > 0; x -= x & (-x)) bit[x]++;
}
int GetBit(int x) {
x += 1; int ans = 0;
for (; x < 3 * N; x += x & (-x)) ans += bit[x];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int xxx = 1000;
while (xxx--) {
int n, k;
//cin >> n >> k;
n = 5; k = 5;
vector<int> a(n), b(n), c(k);
vector<int> all;
for (int i = 0; i < n; i++) {
//cin >> a[i] >> b[i];
a[i] = (rand() % 10) + 1;
b[i] = (rand() % 10) + 1;
all.push_back(a[i]);
all.push_back(b[i]);
}
for (int i = 0; i < k; i++) {
//cin >> c[i];
c[i] = (rand() % 10) + 1;
all.push_back(c[i]);
}
auto Bf = [&] () {
ll ans = 0;
for (int i = 0; i < n; i++) {
int cur = a[i];
for (int j = 0; j < k; j++) {
if (cur <= c[j]) cur = a[i] + b[i] - cur;
}
ans += all[cur];
cout << i << sp << all[cur] << en;
}
return ans;
};
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
map<int, int> mp;
int sz = all.size();
for (int i = 0; i < all.size(); i++) {
mp[all[i]] = i;
}
for (int i = 0; i < n; i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
}
for (int i = 0; i < k; i++) {
c[i] = mp[c[i]];
}
for (int i = 1; i <= 4 * n; i++) st[i] = -1;
for (int i = 0; i < k; i++) Add(1, 0, sz - 1, c[i], i);
vector<int> v(n);
vector<array<int, 3>> svi;
for (int i = 0; i < n; i++) {
int A = min(a[i], b[i]); int B = max(a[i], b[i]);
v[i] = Get(1, 0, sz - 1, A, B - 1);
svi.push_back({v[i], 1, i});
}
for (int i = 0; i < k; i++) svi.push_back({i, 0, i});
sort(svi.begin(), svi.end(), [&] (array<int, 3> c1, array<int, 3> c2) {
if (c1[0] != c2[0]) return c1[0] > c2[0];
return c1[1] < c2[1];
});
ll fina = 0;
for (auto u : svi) {
if (u[1] == 0) AddBit(c[u[2]]), cout << "DODAJEM " << c[u[2]] << en;
else {
int i = u[2];
int A = min(a[i], b[i]);
int B = max(a[i], b[i]);
if (i == 2) {
cout << "!!!! " << GetBit(B) << sp << B << en;
}
int x = GetBit(B) % 2;
cout << i << sp << x << sp << u[0] << en;
if (v[i] == -1) {
if (x) fina += all[b[i]], cout << i << sp << all[b[i]] << en;
else fina += all[a[i]], cout << i << sp << all[a[i]] << en;
continue;
}
if (x) fina += all[A], cout << i << sp << all[A] << en;
else fina += all[B], cout << i << sp << all[B] << en;
}
}
ll yy = Bf();
if (yy != fina) {
for (int i = 0; i < n; i++)
cout << all[a[i]] << sp << all[b[i]] << en;
for (int i = 0; i < k; i++) {
cout << all[c[i]] << en;
}
cout << yy << sp << fina << en;
break;
}
cout << "NTG\n";
}
return 0;
}
/*
5 5
2 6
3 8
7 2
5 3
4 3
3
2
7
9
6*/
Compilation message
fortune_telling2.cpp: In function 'void Add(int, int, int, int, int)':
fortune_telling2.cpp:15:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int Get(int, int, int, int, int)':
fortune_telling2.cpp:22:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < all.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |