Submission #656755

# Submission time Handle Problem Language Result Execution time Memory
656755 2022-11-08T07:21:25 Z uylulu Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
1136 ms 89852 KB
#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define ll long long
#define endl "\n"

const int N = 6e5 + 100,K = 20;

int a[N + 1],b[N + 1];
int val[N + 1],lst[N + 1],cnt = 0;
int sparse[N + 1][K + 1],lg[N + 1];

bool sign[N + 1];

map<int,int> mp;
vector<int> query;

int bit[N + 1];

void add(int pos) {
    while(pos > 0) {
        bit[pos]++;
        pos -= pos&(-pos);
    }
}

int queryBIT(int pos) {
    int res = 0;
    for(int i = pos;i <= cnt;i += i&(-i)) {
        res += bit[i];
    }
    return res;
}

void buildSP() {
    for(int i = 2;i <= N;i++) {
        lg[i] = lg[i/2] + 1;
    }
    for(int i = 0;i <= cnt;i++) {
        sparse[i][0] = lst[i];
    }
    for(int j = 1;j <= K;j++) {
        for(int i = 0;i + (1<<j) <= N;i++) {
            sparse[i][j] = max(sparse[i][j - 1],sparse[i + (1<<(j - 1))][j - 1]);
        }
    }
}

int querySP(int l,int r) {
    int len = lg[r - l + 1];
    return max(sparse[l][len],sparse[r - (1<<len) + 1][len]);
}

signed main() {
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int n,k;
    cin>>n>>k;
    for(int i = 1;i <= n;i++) {
        cin>>a[i]>>b[i];
        sign[i] = 0;
        if(a[i] > b[i]) {
            sign[i] = 1;
            swap(a[i],b[i]);
        }
        mp[a[i]] = -1;
        mp[b[i]] = -1;
    }
    for(int i = 0;i < k;i++) {
        int x;
        cin>>x;
        mp[x] = -1;
        query.push_back(x);
    }

    for(auto u : mp) {
        mp[u.first] = cnt;
        val[cnt] = u.first;
        cnt++;
    }
    for(int i = 1;i <= n;i++) {
        a[i] = mp[a[i]];
        b[i] = mp[b[i]];
    }
    for(int i = 0;i < query.size();i++) {
        query[i] = mp[query[i]];
    }
    for(int i = 0;i <= cnt;i++) {
        lst[i] = -1;
    }
    for(int i = 0;i < query.size();i++) {
        lst[query[i]] = max(lst[query[i]],i);
    }
    mp.clear();

    ll res = 0;
    buildSP();

    vector<pair<int,int>> asd;
    
    for(int i = 1;i <= n;i++) {
        if(a[i] == b[i]) {
            res += val[a[i]];
            continue;
        }
        int nw = querySP(a[i],b[i] - 1);
        asd.push_back({nw + 1,i});
    }
    
    sort(asd.begin(),asd.end());
    reverse(asd.begin(),asd.end());

    int ptr = query.size() - 1;

    for(auto u : asd) {
        while(ptr >= u.first) {
            add(query[ptr]);
            ptr--;
        }
        int w = sign[u.second];
        if(u.first > 0) {
            w = 1;
        }

        int cnt = queryBIT(b[u.second]);
        w ^= (cnt%2);

        if(w == 0) {
            res += val[a[u.second]];
        } else {
            res += val[b[u.second]];
        }
    }
    cout<<res<<endl;

    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:88:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for(int i = 0;i < query.size();i++) {
      |                   ~~^~~~~~~~~~~~~~
fortune_telling2.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for(int i = 0;i < query.size();i++) {
      |                   ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 104 ms 52096 KB Output is correct
2 Correct 115 ms 52144 KB Output is correct
3 Correct 120 ms 52232 KB Output is correct
4 Correct 108 ms 52196 KB Output is correct
5 Correct 113 ms 52212 KB Output is correct
6 Correct 101 ms 52104 KB Output is correct
7 Correct 102 ms 52132 KB Output is correct
8 Correct 103 ms 52204 KB Output is correct
9 Correct 102 ms 52196 KB Output is correct
10 Correct 100 ms 52044 KB Output is correct
11 Correct 102 ms 52144 KB Output is correct
12 Correct 114 ms 52140 KB Output is correct
13 Correct 111 ms 52144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 52096 KB Output is correct
2 Correct 115 ms 52144 KB Output is correct
3 Correct 120 ms 52232 KB Output is correct
4 Correct 108 ms 52196 KB Output is correct
5 Correct 113 ms 52212 KB Output is correct
6 Correct 101 ms 52104 KB Output is correct
7 Correct 102 ms 52132 KB Output is correct
8 Correct 103 ms 52204 KB Output is correct
9 Correct 102 ms 52196 KB Output is correct
10 Correct 100 ms 52044 KB Output is correct
11 Correct 102 ms 52144 KB Output is correct
12 Correct 114 ms 52140 KB Output is correct
13 Correct 111 ms 52144 KB Output is correct
14 Correct 133 ms 53816 KB Output is correct
15 Correct 141 ms 55796 KB Output is correct
16 Correct 167 ms 57452 KB Output is correct
17 Correct 222 ms 59124 KB Output is correct
18 Correct 209 ms 59380 KB Output is correct
19 Correct 212 ms 59232 KB Output is correct
20 Correct 195 ms 59096 KB Output is correct
21 Correct 196 ms 59052 KB Output is correct
22 Correct 177 ms 57060 KB Output is correct
23 Correct 154 ms 56052 KB Output is correct
24 Correct 148 ms 55272 KB Output is correct
25 Correct 172 ms 57548 KB Output is correct
26 Correct 157 ms 56776 KB Output is correct
27 Correct 174 ms 57196 KB Output is correct
28 Correct 173 ms 57044 KB Output is correct
29 Correct 188 ms 58024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 52096 KB Output is correct
2 Correct 115 ms 52144 KB Output is correct
3 Correct 120 ms 52232 KB Output is correct
4 Correct 108 ms 52196 KB Output is correct
5 Correct 113 ms 52212 KB Output is correct
6 Correct 101 ms 52104 KB Output is correct
7 Correct 102 ms 52132 KB Output is correct
8 Correct 103 ms 52204 KB Output is correct
9 Correct 102 ms 52196 KB Output is correct
10 Correct 100 ms 52044 KB Output is correct
11 Correct 102 ms 52144 KB Output is correct
12 Correct 114 ms 52140 KB Output is correct
13 Correct 111 ms 52144 KB Output is correct
14 Correct 133 ms 53816 KB Output is correct
15 Correct 141 ms 55796 KB Output is correct
16 Correct 167 ms 57452 KB Output is correct
17 Correct 222 ms 59124 KB Output is correct
18 Correct 209 ms 59380 KB Output is correct
19 Correct 212 ms 59232 KB Output is correct
20 Correct 195 ms 59096 KB Output is correct
21 Correct 196 ms 59052 KB Output is correct
22 Correct 177 ms 57060 KB Output is correct
23 Correct 154 ms 56052 KB Output is correct
24 Correct 148 ms 55272 KB Output is correct
25 Correct 172 ms 57548 KB Output is correct
26 Correct 157 ms 56776 KB Output is correct
27 Correct 174 ms 57196 KB Output is correct
28 Correct 173 ms 57044 KB Output is correct
29 Correct 188 ms 58024 KB Output is correct
30 Correct 345 ms 65724 KB Output is correct
31 Correct 539 ms 70956 KB Output is correct
32 Correct 774 ms 77244 KB Output is correct
33 Correct 1122 ms 87528 KB Output is correct
34 Correct 288 ms 63700 KB Output is correct
35 Correct 1113 ms 89756 KB Output is correct
36 Correct 1136 ms 87520 KB Output is correct
37 Correct 1112 ms 89852 KB Output is correct
38 Correct 1109 ms 87440 KB Output is correct
39 Correct 1080 ms 87496 KB Output is correct
40 Correct 942 ms 87296 KB Output is correct
41 Correct 1090 ms 87436 KB Output is correct
42 Correct 1079 ms 88532 KB Output is correct
43 Correct 556 ms 87444 KB Output is correct
44 Correct 569 ms 87356 KB Output is correct
45 Correct 579 ms 87428 KB Output is correct
46 Correct 526 ms 71100 KB Output is correct
47 Correct 427 ms 66392 KB Output is correct
48 Correct 627 ms 76648 KB Output is correct
49 Correct 649 ms 77980 KB Output is correct