#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long maxK = 200'000;
const long long logN = 18;
const long long INF = 1'000'000'000;
long long N, K;
vector<long long> T(1+maxK);
struct segtree
{
long long l;
long long r;
vector<long long> v;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(long long L, long long R)
{
l = L;
r = R;
if(l == r)
{
v = vector<long long>{T[l]};
}
else
{
long long m = (l+r)/2;
left = new segtree(l, m);
right = new segtree(m+1, r);
for(long long x: left->v) v.push_back(x);
for(long long y: right->v) v.push_back(y);
sort(v.begin(), v.end());
}
}
long long query(long long L, long long R, long long X, long long Y)
{
if(X > Y) return 0;
if(R < l || r < L) return 0;
else if(L <= l && r <= R)
{
long long lo = -1;
for(long long bit = logN; bit >= 0; bit--)
{
if(lo + (1 << bit) >= v.size()) continue;
if(v[lo + (1 << bit)] >= X) continue;
lo += (1 << bit);
}
lo++;
if(v[lo] < X || Y < v[lo]) return 0;
long long hi = -1;
for(long long bit = logN; bit >= 0; bit--)
{
if(hi + (1 << bit) >= v.size()) continue;
if(v[hi + (1 << bit)] > Y) continue;
hi += (1 << bit);
}
return hi-lo+1;
}
else return left->query(L, R, X, Y) + right->query(L, R, X, Y);
}
long long get_last(long long A, long long B)
{
if(l == r) return l;
else if(right->query(1, N, A, B) >= 1)
return right->get_last(A, B);
else
return left->get_last(A, B);
}
};
int main()
{
cin >> N >> K;
long long A[N+1], B[N+1];
for(long long i = 1; i <= N; i++) cin >> A[i] >> B[i];
for(long long j = 1; j <= K; j++) cin >> T[j];
segtree S(0, N);
long long res = 0;
for(long long i = 1; i <= N; i++)
{
if(A[i] == B[i])
{
res += A[i];
continue;
}
long long last_pos = S.get_last(min(A[i], B[i]), max(A[i], B[i]) - 1);
if(last_pos == 0)
{
long long Q = S.query(1, N, max(A[i], B[i]), INF);
if(Q % 2 == 0) res += A[i];
else res += B[i];
}
else
{
long long Q = S.query(last_pos + 1, N, max(A[i], B[i]), INF);
if(Q % 2 == 0) res += max(A[i], B[i]);
else res += min(A[i], B[i]);
}
}
cout << res << '\n';
}
Compilation message
fortune_telling2.cpp: In member function 'long long int segtree::query(long long int, long long int, long long int, long long int)':
fortune_telling2.cpp:57:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(lo + (1 << bit) >= v.size()) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
fortune_telling2.cpp:67:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if(hi + (1 << bit) >= v.size()) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |