Submission #775623

# Submission time Handle Problem Language Result Execution time Memory
775623 2023-07-06T16:25:03 Z Essa2006 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
1038 ms 87404 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
const int pr=20, s_p=1<<pr, e_p=(1<<(pr+1))-1;
int n, k;
vector<int>A, B, T, S_mx, S_sum;
map<int, int>mp, inv;

void pre(){
    A.clear(), B.clear(), T.clear(), mp.clear(), inv.clear(), S_mx.clear(), S_sum.clear();
    A.resize(n+1), B.resize(n+1), T.resize(k+1), S_mx.resize(1<<(pr+1), -1), S_sum.resize(1<<(pr+1));
}

void update_sum(int ind, int delta){
    S_sum[ind]+=delta;
    while(ind/=2)
        S_sum[ind]+=delta;
}
void update_mx(int ind, int new_){
    S_mx[ind]=max(S_mx[ind], new_);
    while(ind/=2)
        S_mx[ind]=max(S_mx[ind], new_);
}

int get_sum(int id, int u, int v, int l, int r){
    if(l>v || r<u)
        return 0;
    if(l>=u && r<=v)
        return S_sum[id];
    int md=(l+r)/2;
    int fir=get_sum(id*2, u, v, l, md);
    int sec=get_sum(id*2+1, u, v, md+1, r);
    return fir+sec;
}
int get_mx(int id, int u, int v, int l, int r){
    if(l>v || r<u)
        return -1;
    if(l>=u && r<=v)
        return S_mx[id];
    int md=(l+r)/2;
    int fir=get_mx(id*2, u, v, l, md);
    int sec=get_mx(id*2+1, u, v, md+1, r);
    return max(fir, sec);
}

void comp(){
    vector<int>S=T;
    for(int i=1;i<=n;i++){
        S.push_back(A[i]);
        S.push_back(B[i]);
    }
    sort(all(S));
    int cnt=1;
    for(int i=0;i<S.size();i++){
        if(!i || S[i]!=S[i-1]){
            mp[S[i]]=cnt, inv[cnt]=S[i], cnt++;
        }
    }
    for(int i=1;i<=n;i++){
        A[i]=mp[A[i]], B[i]=mp[B[i]];
    }
    for(int j=0;j<k;j++){
        T[j]=mp[T[j]];
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>k;
    pre();
    for(int i=1;i<=n;i++){
        cin>>A[i]>>B[i];
    }
    for(int j=0;j<k;j++){
        cin>>T[j];
    }
    comp();
    for(int j=0;j<k;j++){
        update_mx(T[j]+s_p, j);
    }
    vector<vector<int>>Q(k);
    ll ans=0;
    for(int i=1;i<=n;i++){
        int mn=min(A[i], B[i]), mx=max(A[i], B[i]);
        if(mn==mx){
            ans+=inv[mn];
            continue;
        }
        int mx_ind=get_mx(1, mn+s_p, mx-1+s_p, s_p, e_p);
        Q[(mx_ind>=0?mx_ind:0)].push_back((mx_ind>=0?i:-i));
    }
    
    for(int j=k-1;j>=0;j--){
        update_sum(T[j]+s_p, 1);
        for(int f=0;f<Q[j].size();f++){
            int mn=min(A[abs(Q[j][f])], B[abs(Q[j][f])]), mx=max(A[abs(Q[j][f])], B[abs(Q[j][f])]);
            int sum=get_sum(1, mx+s_p, e_p, s_p, e_p);
            if(Q[j][f]<0){
                if(sum&1)
                    ans+=inv[B[abs(Q[j][f])]];
                else
                    ans+=inv[A[abs(Q[j][f])]];
                continue;
            }
            if(sum&1)
                ans+=inv[mn];
            else
                ans+=inv[mx];
        }
    }
    cout<<ans;
}

Compilation message

fortune_telling2.cpp: In function 'void comp()':
fortune_telling2.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i=0;i<S.size();i++){
      |                 ~^~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:99:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         for(int f=0;f<Q[j].size();f++){
      |                     ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 16852 KB Output is correct
2 Correct 7 ms 16980 KB Output is correct
3 Correct 8 ms 16980 KB Output is correct
4 Correct 8 ms 16980 KB Output is correct
5 Correct 8 ms 16980 KB Output is correct
6 Correct 8 ms 16984 KB Output is correct
7 Correct 8 ms 16980 KB Output is correct
8 Correct 8 ms 16980 KB Output is correct
9 Correct 9 ms 17012 KB Output is correct
10 Correct 8 ms 16852 KB Output is correct
11 Correct 8 ms 16980 KB Output is correct
12 Correct 8 ms 16980 KB Output is correct
13 Correct 10 ms 16916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 16852 KB Output is correct
2 Correct 7 ms 16980 KB Output is correct
3 Correct 8 ms 16980 KB Output is correct
4 Correct 8 ms 16980 KB Output is correct
5 Correct 8 ms 16980 KB Output is correct
6 Correct 8 ms 16984 KB Output is correct
7 Correct 8 ms 16980 KB Output is correct
8 Correct 8 ms 16980 KB Output is correct
9 Correct 9 ms 17012 KB Output is correct
10 Correct 8 ms 16852 KB Output is correct
11 Correct 8 ms 16980 KB Output is correct
12 Correct 8 ms 16980 KB Output is correct
13 Correct 10 ms 16916 KB Output is correct
14 Correct 29 ms 19924 KB Output is correct
15 Correct 58 ms 23228 KB Output is correct
16 Correct 92 ms 26496 KB Output is correct
17 Correct 118 ms 29680 KB Output is correct
18 Correct 119 ms 29664 KB Output is correct
19 Correct 118 ms 29636 KB Output is correct
20 Correct 120 ms 29600 KB Output is correct
21 Correct 112 ms 29776 KB Output is correct
22 Correct 74 ms 25876 KB Output is correct
23 Correct 68 ms 23960 KB Output is correct
24 Correct 64 ms 22628 KB Output is correct
25 Correct 78 ms 27088 KB Output is correct
26 Correct 77 ms 25108 KB Output is correct
27 Correct 84 ms 25884 KB Output is correct
28 Correct 81 ms 27056 KB Output is correct
29 Correct 105 ms 28984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 16852 KB Output is correct
2 Correct 7 ms 16980 KB Output is correct
3 Correct 8 ms 16980 KB Output is correct
4 Correct 8 ms 16980 KB Output is correct
5 Correct 8 ms 16980 KB Output is correct
6 Correct 8 ms 16984 KB Output is correct
7 Correct 8 ms 16980 KB Output is correct
8 Correct 8 ms 16980 KB Output is correct
9 Correct 9 ms 17012 KB Output is correct
10 Correct 8 ms 16852 KB Output is correct
11 Correct 8 ms 16980 KB Output is correct
12 Correct 8 ms 16980 KB Output is correct
13 Correct 10 ms 16916 KB Output is correct
14 Correct 29 ms 19924 KB Output is correct
15 Correct 58 ms 23228 KB Output is correct
16 Correct 92 ms 26496 KB Output is correct
17 Correct 118 ms 29680 KB Output is correct
18 Correct 119 ms 29664 KB Output is correct
19 Correct 118 ms 29636 KB Output is correct
20 Correct 120 ms 29600 KB Output is correct
21 Correct 112 ms 29776 KB Output is correct
22 Correct 74 ms 25876 KB Output is correct
23 Correct 68 ms 23960 KB Output is correct
24 Correct 64 ms 22628 KB Output is correct
25 Correct 78 ms 27088 KB Output is correct
26 Correct 77 ms 25108 KB Output is correct
27 Correct 84 ms 25884 KB Output is correct
28 Correct 81 ms 27056 KB Output is correct
29 Correct 105 ms 28984 KB Output is correct
30 Correct 243 ms 45132 KB Output is correct
31 Correct 374 ms 54016 KB Output is correct
32 Correct 587 ms 64988 KB Output is correct
33 Correct 1002 ms 87224 KB Output is correct
34 Correct 182 ms 42820 KB Output is correct
35 Correct 1030 ms 87192 KB Output is correct
36 Correct 1038 ms 87072 KB Output is correct
37 Correct 1010 ms 87152 KB Output is correct
38 Correct 989 ms 87180 KB Output is correct
39 Correct 1018 ms 87020 KB Output is correct
40 Correct 862 ms 87404 KB Output is correct
41 Correct 1000 ms 87028 KB Output is correct
42 Correct 1030 ms 86876 KB Output is correct
43 Correct 512 ms 87024 KB Output is correct
44 Correct 537 ms 86960 KB Output is correct
45 Correct 542 ms 86976 KB Output is correct
46 Correct 465 ms 57136 KB Output is correct
47 Correct 352 ms 47420 KB Output is correct
48 Correct 599 ms 68276 KB Output is correct
49 Correct 667 ms 68508 KB Output is correct