# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
59335 | gusfring | Fortune Telling 2 (JOI14_fortune_telling2) | C++14 | 2392 ms | 148700 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n,m;
int a[maxn], b[maxn], c[maxn];
int pos[maxn], res[maxn];
vector<int> p[maxn];
int sz;
int limit, tree[maxn*9];
int fen[maxn*3];
map<int,int> mp;
int rec[maxn*3];
void cpidx() {
for(int i=1; i<=n; i++) mp[a[i]], mp[b[i]];
for(int i=1; i<=m; i++) mp[c[i]];
for(auto &it : mp){
it.second = ++sz;
rec[sz] = it.first;
}
for(int i=1; i<=n; i++) a[i] = mp[a[i]], b[i] = mp[b[i]];
for(int i=1; i<=m; i++) c[i] = mp[c[i]];
}
void init(){
limit = 1;
while(limit<=sz) limit*=2;
}
void update(int x, int val){
x += limit;
tree[x] = max(tree[x], val);
while(x){
x/=2;
tree[x] = max(tree[x<<1], tree[x<<1|1]);
}
}
int query(int l, int r){
l += limit; r += limit;
int res = 0;
while(l<r){
if(l % 2 != 0) res = max(res, tree[l++]);
if(r % 2 == 0) res = max(res, tree[r--]);
l/=2; r/=2;
}
if(l == r) res = max(res, tree[l]);
return res;
}
void add(int x, int val){
while(x){
fen[x] += val;
x -= x&-x;
}
}
int sum(int x){
int res = 0;
while(x<=sz){
res += fen[x];
x += x&-x;
}
return res;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) scanf("%d%d",&a[i],&b[i]);
for(int i=1; i<=m; i++) scanf("%d",&c[i]);
cpidx();
init();
for(int i=1; i<=m; i++) update(c[i], i);
for(int i=1; i<=n; i++) {
pos[i] = query(min(a[i],b[i]), max(a[i],b[i])-1);
p[pos[i]+1].push_back(i);
}
for(int i=m; i>=1; i--){
add(c[i],1);
for(auto x : p[i]) res[x] = sum(max(a[x], b[x]));
}
long long ans = 0;
for(int i=1; i<=n; i++){
if(pos[i]==0){
if(res[i] % 2 == 0) ans += rec[a[i]];
else ans += rec[b[i]];
}
else{
if(res[i] % 2 == 1) ans += rec[min(a[i], b[i])];
else ans += rec[max(a[i], b[i])];
}
}
printf("%lld",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |