Submission #908047

# Submission time Handle Problem Language Result Execution time Memory
908047 2024-01-16T07:29:18 Z Warinchai Fortune Telling 2 (JOI14_fortune_telling2) C++14
35 / 100
230 ms 42040 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
int N=2e5+1;
struct segtree{
    int info[800005];
    void upd(int st,int en,int i,int pos,int val){
        if(st>pos||en<pos)return;
        if(st==en)return void(info[i]=val);
        int m=(st+en)/2;
        upd(st,m,i*2,pos,val);
        upd(m+1,en,i*2+1,pos,val);
        info[i]=max(info[i*2],info[i*2+1]);
    }
    int fmax(int st,int en,int i,int l,int r){
        if(st>r||en<l)return 0;
        if(st>=l&&en<=r)return info[i];
        int m=(st+en)/2;
        return max(fmax(st,m,i*2,l,r),fmax(m+1,en,i*2+1,l,r));
    }
}str;
struct fenwick{
    int info[200005];
    void upd(int x,int val){
        for(int i=x;i<=200000;i+=(i&-i)){
            info[i]+=val;
        }
    }
    int fsum(int x){
        int sum=0;
        for(int i=x;i>0;i-=(i&-i)){
            sum+=info[i];
        }
        return sum;
    }
}ftr;
int ca[200005];
int cb[200005];
int cca[200005];
int ccb[200005];
vector<int>v;
vector<int>q;
vector<int>nq;
map<int,int>mp;
vector<pair<int,int> >qr[200005];
int side[200005];
int val[200005];
int ans[200005];
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>ca[i]>>cb[i];
        if(!mp[ca[i]])mp[ca[i]]++,v.push_back(ca[i]);
        if(!mp[cb[i]])mp[cb[i]]++,v.push_back(cb[i]);
    }
    for(int i=1;i<=k;i++){
        int x;
        cin>>x;
        q.push_back(x);
        if(!mp[x])mp[x]++,v.push_back(x);
    }
    sort(v.begin(),v.end());
    for(int i=1;i<=k;i++){
        int id=lower_bound(v.begin(),v.end(),q[i-1])-v.begin()+1;
        //cerr<<q[i-1]<<":"<<id<<"\n";
        nq.push_back(id);
        str.upd(1,N,1,id,i);
        //cerr<<str.fmax(1,N,1,id,id)<<":"<<id<<"\n";
    }
    for(int i=0;i<n;i++){
        int id=lower_bound(v.begin(),v.end(),ca[i])-v.begin()+1;
        cca[i]=id;
        id=lower_bound(v.begin(),v.end(),cb[i])-v.begin()+1;
        ccb[i]=id;
    }
    int sum=0;
    for(int i=0;i<n;i++){
        if(ca[i]<cb[i]){
            int l=str.fmax(1,N,1,cca[i],ccb[i]-1);
            qr[l].push_back({i,ccb[i]});
            //cerr<<"L:"<<l<<" "<<ccb[i]<<"\n";
            side[i]=1;
            if(l==0)side[i]=0;
        }else if(ca[i]>cb[i]){
            int l=str.fmax(1,N,1,ccb[i],cca[i]-1);
            qr[l].push_back({i,cca[i]});
            //cerr<<"L:"<<l<<" "<<cca[i]<<"\n";
            side[i]=0;
        }else{
            side[i]=0;
            sum+=ca[i];
        }
    }
    /*for(int i=0;i<n;i++){
        cerr<<cca[i]<<" "<<ccb[i]<<"\n";
    }
    for(int i=1;i<=8;i++){
        //cerr<<str.fmax(1,N,1,i,i)<<" ";
    }cerr<<endl;*/
    int cur=2e5;
    for(int i=k;i>=1;i--){
        //cerr<<i<<"?"<<nq[i-1]<<"\n";
        while(cur>i){
            //cerr<<"cur:"<<cur<<"\n";
            for(auto x:qr[cur]){
                int id=x.second;
                int am=ftr.fsum(2e5)-ftr.fsum(id-1);
                //cerr<<x.first<<" "<<ftr.fsum(2e5)<<"-"<<ftr.fsum(id-1)<<"\n";
                if(am%2==1)side[x.first]^=1;
                ans[x.first]=side[x.first];
                sum+=ans[x.first]==0?ca[x.first]:cb[x.first];
                //cerr<<am<<" ans"<<x.first<<" "<<(ans[x.first]==0?ca[x.first]:cb[x.first])<<"\n";
            }
            cur--;
        }
        //cerr<<"work\n";
        ftr.upd(nq[i-1],1);
        //cerr<<"work\n";
    }
    while(cur>=0){
        for(auto x:qr[cur]){
            int id=x.second;
            int am=ftr.fsum(2e5)-ftr.fsum(id-1);
            if(am%2==1)side[x.first]^=1;
            ans[x.first]=side[x.first];
            sum+=ans[x.first]==0?ca[x.first]:cb[x.first];
            //cerr<<am<<" ans"<<x.first<<" "<<(ans[x.first]==0?ca[x.first]:cb[x.first])<<"\n";
        }
        cur--;
    }
    cout<<sum;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 5 ms 19288 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 6 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19036 KB Output is correct
7 Correct 7 ms 19036 KB Output is correct
8 Correct 7 ms 19036 KB Output is correct
9 Correct 6 ms 19036 KB Output is correct
10 Correct 6 ms 19036 KB Output is correct
11 Correct 6 ms 19000 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 5 ms 19288 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 6 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19036 KB Output is correct
7 Correct 7 ms 19036 KB Output is correct
8 Correct 7 ms 19036 KB Output is correct
9 Correct 6 ms 19036 KB Output is correct
10 Correct 6 ms 19036 KB Output is correct
11 Correct 6 ms 19000 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19016 KB Output is correct
14 Correct 31 ms 21628 KB Output is correct
15 Correct 55 ms 24664 KB Output is correct
16 Correct 92 ms 29580 KB Output is correct
17 Correct 114 ms 32200 KB Output is correct
18 Correct 117 ms 32480 KB Output is correct
19 Correct 116 ms 32452 KB Output is correct
20 Correct 121 ms 32176 KB Output is correct
21 Correct 119 ms 32708 KB Output is correct
22 Correct 67 ms 29040 KB Output is correct
23 Correct 59 ms 27596 KB Output is correct
24 Correct 53 ms 26312 KB Output is correct
25 Correct 75 ms 30544 KB Output is correct
26 Correct 72 ms 28612 KB Output is correct
27 Correct 84 ms 29380 KB Output is correct
28 Correct 81 ms 29564 KB Output is correct
29 Correct 115 ms 31172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 19036 KB Output is correct
2 Correct 5 ms 19288 KB Output is correct
3 Correct 6 ms 19036 KB Output is correct
4 Correct 6 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19036 KB Output is correct
7 Correct 7 ms 19036 KB Output is correct
8 Correct 7 ms 19036 KB Output is correct
9 Correct 6 ms 19036 KB Output is correct
10 Correct 6 ms 19036 KB Output is correct
11 Correct 6 ms 19000 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 5 ms 19016 KB Output is correct
14 Correct 31 ms 21628 KB Output is correct
15 Correct 55 ms 24664 KB Output is correct
16 Correct 92 ms 29580 KB Output is correct
17 Correct 114 ms 32200 KB Output is correct
18 Correct 117 ms 32480 KB Output is correct
19 Correct 116 ms 32452 KB Output is correct
20 Correct 121 ms 32176 KB Output is correct
21 Correct 119 ms 32708 KB Output is correct
22 Correct 67 ms 29040 KB Output is correct
23 Correct 59 ms 27596 KB Output is correct
24 Correct 53 ms 26312 KB Output is correct
25 Correct 75 ms 30544 KB Output is correct
26 Correct 72 ms 28612 KB Output is correct
27 Correct 84 ms 29380 KB Output is correct
28 Correct 81 ms 29564 KB Output is correct
29 Correct 115 ms 31172 KB Output is correct
30 Incorrect 230 ms 42040 KB Output isn't correct
31 Halted 0 ms 0 KB -