Submission #475037

# Submission time Handle Problem Language Result Execution time Memory
475037 2021-09-20T16:24:17 Z nicolaalexandra Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
630 ms 24220 KB
#include <bits/stdc++.h>
#define DIM 200010
using namespace std;

struct card{
    int x,y,poz;
} v[DIM];

int aint[DIM*4],aib[DIM*4],poz[DIM],sol[DIM],w[DIM*4];
vector <int> events[DIM];
pair <int,int> t[DIM];
int n,i,j,k,el;

int cautare_binara (int v[], int n, int val){
    int st = 1, dr = n;
    while (st <= dr){
        int mid = (st+dr)>>1;
        if (v[mid] == val)
            return mid;
        if (v[mid] < val)
            st = mid+1;
        else dr = mid-1;
    }
}

void build (int nod, int st, int dr){
    if (st == dr){
        aint[nod] = t[st].second;
        return;
    }
    int mid = (st+dr)>>1;
    build (nod<<1,st,mid);
    build ((nod<<1)|1,mid+1,dr);

    aint[nod] = max (aint[nod<<1],aint[(nod<<1)|1]);
}

int query (int nod, int st, int dr, int x, int y){

    if (x <= st && dr <= y)
        return aint[nod];

    int mid = (st+dr)>>1, sol_st = 0, sol_dr = 0;
    if (x <= mid)
        sol_st = query (nod<<1,st,mid,x,y);
    if (y > mid)
        sol_dr = query ((nod<<1)|1,mid+1,dr,x,y);

    return max (sol_st,sol_dr);
}

inline int cmp (pair <int,int> a, pair <int,int> b){
    return a.second < b.second;
}
inline int cmp2 (card a, card b){
    return max(a.x,a.y) < max(b.x,b.y);
}

inline int cmp3 (pair <int,int> a, pair <int,int> b){
    return a.first > b.first;
}
inline int cmp4 (card a, card b){
    return a.poz < b.poz;
}


void update_aib (int p, int val){
    for (;p<=el;p+=(p&-p))
        aib[p] += val;
}

int query_aib (int p){
    int sol = 0;
    for (;p;p-=(p&-p))
        sol += aib[p];
    return sol;
}

int main (){

    //ifstream cin ("date.in");
    //ofstream cout ("date.out");

    cin>>n>>k;
    for (i=1;i<=n;i++){
        cin>>v[i].x>>v[i].y;
        v[i].poz = i;

        w[++el] = v[i].x;
        w[++el] = v[i].y;
    }

    for (i=1;i<=k;i++){
        cin>>t[i].first;
        t[i].second = i;
        w[++el] = t[i].first;
    }

    sort (w+1,w+el+1);

    int el2 = 1;
    for (i=2;i<=el;i++)
        if (w[i] != w[i-1])
            w[++el2] = w[i];

    el = el2;



    sort (t+1,t+k+1);

    build (1,1,k);

    for (i=1;i<=n;i++){

        int st = 1, dr = k, sol_x = 0, sol_y = 0;
        while (st <= dr){
            int mid = (st+dr)>>1;
            if (t[mid].first >= min(v[i].x,v[i].y) ){
                sol_x = mid;
                dr = mid-1;
            } else st = mid+1;
        }

        st = 1, dr = k;
        while (st <= dr){
            int mid = (st+dr)>>1;
            if (t[mid].first < max(v[i].x,v[i].y)){
                sol_y = mid;
                st = mid+1;
            } else dr = mid-1;
        }

        if (sol_x && sol_y){
            poz[i] = query (1,1,k,sol_x,sol_y);
            events[poz[i]].push_back(i);
        }
        //cout<<poz[i]<<"\n";
    }


    sort (t+1,t+k+1,cmp);

    for (i=k;i>=0;i--){
        for (auto it : events[i]){
            int val_y = cautare_binara(w,el,max(v[it].x,v[it].y));
            sol[it] = query_aib(el) - query_aib(val_y - 1);
        }

        if (i){
            int val_t = cautare_binara(w,el,t[i].first);
            update_aib (val_t,1);
        }
    }



    long long ans = 0;
    for (i=1;i<=n;i++){

        if (!poz[i]){
            if (sol[i] % 2)
                ans += v[i].y;
            else ans += v[i].x;
        } else {

            if (sol[i] % 2 == 0)
                ans += max(v[i].x,v[i].y);
            else ans += min(v[i].x,v[i].y);
        }
    }

    cout<<ans;



    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int cautare_binara(int*, int, int)':
fortune_telling2.cpp:24:1: warning: control reaches end of non-void function [-Wreturn-type]
   24 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 5 ms 5068 KB Output is correct
4 Correct 6 ms 5068 KB Output is correct
5 Correct 5 ms 5140 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 6 ms 5068 KB Output is correct
9 Correct 5 ms 5068 KB Output is correct
10 Correct 5 ms 5068 KB Output is correct
11 Correct 5 ms 5068 KB Output is correct
12 Correct 5 ms 5068 KB Output is correct
13 Correct 5 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 5 ms 5068 KB Output is correct
4 Correct 6 ms 5068 KB Output is correct
5 Correct 5 ms 5140 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 6 ms 5068 KB Output is correct
9 Correct 5 ms 5068 KB Output is correct
10 Correct 5 ms 5068 KB Output is correct
11 Correct 5 ms 5068 KB Output is correct
12 Correct 5 ms 5068 KB Output is correct
13 Correct 5 ms 5068 KB Output is correct
14 Correct 29 ms 6028 KB Output is correct
15 Correct 54 ms 7000 KB Output is correct
16 Correct 84 ms 7748 KB Output is correct
17 Correct 112 ms 8916 KB Output is correct
18 Correct 116 ms 8896 KB Output is correct
19 Correct 110 ms 8864 KB Output is correct
20 Correct 113 ms 8904 KB Output is correct
21 Correct 124 ms 8984 KB Output is correct
22 Correct 80 ms 8260 KB Output is correct
23 Correct 80 ms 8228 KB Output is correct
24 Correct 79 ms 8104 KB Output is correct
25 Correct 81 ms 8432 KB Output is correct
26 Correct 89 ms 8472 KB Output is correct
27 Correct 101 ms 8864 KB Output is correct
28 Correct 98 ms 8760 KB Output is correct
29 Correct 106 ms 8900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5068 KB Output is correct
2 Correct 5 ms 5068 KB Output is correct
3 Correct 5 ms 5068 KB Output is correct
4 Correct 6 ms 5068 KB Output is correct
5 Correct 5 ms 5140 KB Output is correct
6 Correct 6 ms 5068 KB Output is correct
7 Correct 5 ms 5068 KB Output is correct
8 Correct 6 ms 5068 KB Output is correct
9 Correct 5 ms 5068 KB Output is correct
10 Correct 5 ms 5068 KB Output is correct
11 Correct 5 ms 5068 KB Output is correct
12 Correct 5 ms 5068 KB Output is correct
13 Correct 5 ms 5068 KB Output is correct
14 Correct 29 ms 6028 KB Output is correct
15 Correct 54 ms 7000 KB Output is correct
16 Correct 84 ms 7748 KB Output is correct
17 Correct 112 ms 8916 KB Output is correct
18 Correct 116 ms 8896 KB Output is correct
19 Correct 110 ms 8864 KB Output is correct
20 Correct 113 ms 8904 KB Output is correct
21 Correct 124 ms 8984 KB Output is correct
22 Correct 80 ms 8260 KB Output is correct
23 Correct 80 ms 8228 KB Output is correct
24 Correct 79 ms 8104 KB Output is correct
25 Correct 81 ms 8432 KB Output is correct
26 Correct 89 ms 8472 KB Output is correct
27 Correct 101 ms 8864 KB Output is correct
28 Correct 98 ms 8760 KB Output is correct
29 Correct 106 ms 8900 KB Output is correct
30 Correct 210 ms 12716 KB Output is correct
31 Correct 294 ms 15088 KB Output is correct
32 Correct 409 ms 17944 KB Output is correct
33 Correct 612 ms 23980 KB Output is correct
34 Correct 188 ms 12100 KB Output is correct
35 Correct 630 ms 23996 KB Output is correct
36 Correct 623 ms 24112 KB Output is correct
37 Correct 621 ms 24164 KB Output is correct
38 Correct 609 ms 24124 KB Output is correct
39 Correct 622 ms 24220 KB Output is correct
40 Correct 569 ms 23684 KB Output is correct
41 Correct 609 ms 24004 KB Output is correct
42 Correct 593 ms 24032 KB Output is correct
43 Correct 465 ms 22292 KB Output is correct
44 Correct 451 ms 22548 KB Output is correct
45 Correct 451 ms 22836 KB Output is correct
46 Correct 442 ms 20888 KB Output is correct
47 Correct 413 ms 20420 KB Output is correct
48 Correct 536 ms 23396 KB Output is correct
49 Correct 516 ms 23332 KB Output is correct