Submission #153829

# Submission time Handle Problem Language Result Execution time Memory
153829 2019-09-16T15:46:06 Z georgerapeanu Fortune Telling 2 (JOI14_fortune_telling2) C++11
100 / 100
2053 ms 83064 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

const int NMAX = 2e5;

int n,k;
pair<int,int> v[NMAX + 5];
int t[NMAX + 5];
int last;

map<int,int> to_norm;

vector<pair<pair<int,int>,int> > query[3 * NMAX + 5];
vector<int> update[3 * NMAX + 5];
int aib[3 * NMAX + 5];
int aint[6 * NMAX + 5];

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

int query_aib(int pos){
    if(pos <= 0){
        return 0;
    }
    int ans = 0;
    for(;pos;pos -= (-pos) & pos){
        ans += aib[pos];
    }
    return ans;
}

void update_aint(int pos,int v){
    pos += last;
    aint[pos] = max(aint[pos],v);
    for(;pos > 1;pos >>= 1){
        aint[pos >> 1] = max(aint[pos],aint[pos ^ 1]);
    }
}

int query_aint(int l,int r){
    int ans = 0;

    for(l += last,r += (last + 1);l < r;(l >>= 1),(r >>= 1)){
        if(l % 2)ans = max(ans,aint[l++]);
        if(r % 2)ans = max(ans,aint[--r]);
    }

    return ans;
}


int main(){

    scanf("%d %d",&n,&k);

    for(int i = 1;i <= n;i++){
        scanf("%d %d",&v[i].first,&v[i].second);
        to_norm[v[i].first] = 1;
        to_norm[v[i].second] = 1;
    }

    for(int i = 1;i <= k;i++){
        scanf("%d",&t[i]);
        to_norm[t[i]] = 1;
    }

    last = 0;
    for(auto &it:to_norm){
        it.second = ++last;
    }

    for(int i = 1;i <= k;i++){
        update_aint(to_norm[t[i]],i);
    }

    for(int i = 1;i <= n;i++){
        int n_a = to_norm[v[i].first];
        int n_b = to_norm[v[i].second];

        if(n_a == n_b){
            query[n_a].push_back({v[i],0});
        }
        else if(n_a < n_b){
            int tmp = query_aint(n_a,n_b - 1);
            if(tmp > 0){
                swap(v[i].first,v[i].second);
            }
            query[n_b].push_back({v[i],tmp});
        }
        else{
            int tmp = query_aint(n_b,n_a - 1);
            query[n_a].push_back({v[i],tmp});
        }
    }

    for(int i = 1;i <= k;i++){
        update[to_norm[t[i]]].push_back(i);
    }

    int cnt = 0;
    long long ans = 0;

    for(int i = last;i >= 0;i--){
        for(auto it:update[i]){
            update_aib(it,1);
            cnt++;
        }
        for(auto it:query[i]){
            if((cnt - query_aib(it.second - 1)) & 1){
                ans += it.first.second;
            }
            else{
                ans += it.first.first;
            }
        }
    }

    printf("%lld\n",ans);

    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&v[i].first,&v[i].second);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&t[i]);
         ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 28792 KB Output is correct
2 Correct 37 ms 28664 KB Output is correct
3 Correct 31 ms 28792 KB Output is correct
4 Correct 31 ms 28792 KB Output is correct
5 Correct 31 ms 28792 KB Output is correct
6 Correct 31 ms 28792 KB Output is correct
7 Correct 32 ms 28792 KB Output is correct
8 Correct 31 ms 28796 KB Output is correct
9 Correct 31 ms 28792 KB Output is correct
10 Correct 35 ms 28672 KB Output is correct
11 Correct 30 ms 28664 KB Output is correct
12 Correct 31 ms 28664 KB Output is correct
13 Correct 31 ms 28792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 28792 KB Output is correct
2 Correct 37 ms 28664 KB Output is correct
3 Correct 31 ms 28792 KB Output is correct
4 Correct 31 ms 28792 KB Output is correct
5 Correct 31 ms 28792 KB Output is correct
6 Correct 31 ms 28792 KB Output is correct
7 Correct 32 ms 28792 KB Output is correct
8 Correct 31 ms 28796 KB Output is correct
9 Correct 31 ms 28792 KB Output is correct
10 Correct 35 ms 28672 KB Output is correct
11 Correct 30 ms 28664 KB Output is correct
12 Correct 31 ms 28664 KB Output is correct
13 Correct 31 ms 28792 KB Output is correct
14 Correct 63 ms 31352 KB Output is correct
15 Correct 112 ms 33888 KB Output is correct
16 Correct 167 ms 36764 KB Output is correct
17 Correct 237 ms 39340 KB Output is correct
18 Correct 250 ms 39416 KB Output is correct
19 Correct 240 ms 39416 KB Output is correct
20 Correct 269 ms 39544 KB Output is correct
21 Correct 240 ms 39292 KB Output is correct
22 Correct 167 ms 36504 KB Output is correct
23 Correct 148 ms 35388 KB Output is correct
24 Correct 132 ms 34288 KB Output is correct
25 Correct 180 ms 37112 KB Output is correct
26 Correct 159 ms 36472 KB Output is correct
27 Correct 185 ms 37268 KB Output is correct
28 Correct 164 ms 37144 KB Output is correct
29 Correct 221 ms 38264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 28792 KB Output is correct
2 Correct 37 ms 28664 KB Output is correct
3 Correct 31 ms 28792 KB Output is correct
4 Correct 31 ms 28792 KB Output is correct
5 Correct 31 ms 28792 KB Output is correct
6 Correct 31 ms 28792 KB Output is correct
7 Correct 32 ms 28792 KB Output is correct
8 Correct 31 ms 28796 KB Output is correct
9 Correct 31 ms 28792 KB Output is correct
10 Correct 35 ms 28672 KB Output is correct
11 Correct 30 ms 28664 KB Output is correct
12 Correct 31 ms 28664 KB Output is correct
13 Correct 31 ms 28792 KB Output is correct
14 Correct 63 ms 31352 KB Output is correct
15 Correct 112 ms 33888 KB Output is correct
16 Correct 167 ms 36764 KB Output is correct
17 Correct 237 ms 39340 KB Output is correct
18 Correct 250 ms 39416 KB Output is correct
19 Correct 240 ms 39416 KB Output is correct
20 Correct 269 ms 39544 KB Output is correct
21 Correct 240 ms 39292 KB Output is correct
22 Correct 167 ms 36504 KB Output is correct
23 Correct 148 ms 35388 KB Output is correct
24 Correct 132 ms 34288 KB Output is correct
25 Correct 180 ms 37112 KB Output is correct
26 Correct 159 ms 36472 KB Output is correct
27 Correct 185 ms 37268 KB Output is correct
28 Correct 164 ms 37144 KB Output is correct
29 Correct 221 ms 38264 KB Output is correct
30 Correct 759 ms 51136 KB Output is correct
31 Correct 886 ms 57684 KB Output is correct
32 Correct 1486 ms 66168 KB Output is correct
33 Correct 1953 ms 83064 KB Output is correct
34 Correct 570 ms 49308 KB Output is correct
35 Correct 1907 ms 82812 KB Output is correct
36 Correct 2050 ms 83028 KB Output is correct
37 Correct 1899 ms 82908 KB Output is correct
38 Correct 1856 ms 82928 KB Output is correct
39 Correct 1909 ms 82992 KB Output is correct
40 Correct 1724 ms 82612 KB Output is correct
41 Correct 2053 ms 82960 KB Output is correct
42 Correct 1860 ms 82804 KB Output is correct
43 Correct 1203 ms 80632 KB Output is correct
44 Correct 1199 ms 80760 KB Output is correct
45 Correct 1258 ms 81536 KB Output is correct
46 Correct 970 ms 63020 KB Output is correct
47 Correct 835 ms 56644 KB Output is correct
48 Correct 1153 ms 71976 KB Output is correct
49 Correct 1152 ms 72012 KB Output is correct