Submission #591885

# Submission time Handle Problem Language Result Execution time Memory
591885 2022-07-08T06:06:12 Z Do_you_copy Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
219 ms 18076 KB
#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 2e5 + 2;

int n, k;

struct TCard{
    int high, low, idx;
};

struct TQ{
    int val, idx1, idx2;
};

TQ flip[maxN];
TCard b[maxN];
int bit[maxN];
int bit2[maxN];
vector <pii> t;
pii a[maxN];
int c[maxN];
int cnt[maxN];
int cnt1[maxN];

void update(int xx, int val){
    int x = upper_bound(t.begin(), t.end(), xx,[](const auto &i, const auto &j){
        return i < j.first;
    }) - t.begin();
    for (; x; x -= x & -x){
        bit[x] = max(bit[x], val);
    }
}

void update2(int xx, int val){
    int x = upper_bound(t.begin(), t.end(), xx,[](const auto &i, const auto &j){
        return i < j.first;
    }) - t.begin();
    x = t.size() - x + 1;
    for (; x <= t.size(); x += x & -x){
        bit2[x] += val;
    }
}

int get(int xx){
    int x = lower_bound(t.begin(), t.end(), xx,[](const auto &i, const auto &j){
        return i.fi < j;
    }) - t.begin() + 1;
    ll res = 0;
    for (; x <= t.size(); x += x & -x){
        res = max(res, bit[x]);
    }
    return res;
}

int get2(int xx){
    int x = lower_bound(t.begin(), t.end(), xx,[](const auto &i, const auto &j){
        return i.fi < j;
    }) - t.begin() + 1;
    x = t.size() - x + 1;
    ll res = 0;
    for (; x; x -= x & -x){
        res += bit2[x];
    }
    return res;
}

void Init(){
    cin >> n >> k;
    for (int i = 1; i <= n; ++i){
        cin >> a[i].fi >> a[i].se;
        b[i].low = min(a[i].fi, a[i].se);
        b[i].high = max(a[i].fi, a[i].se);
        b[i].idx = i;
    }
    for (int i = 1; i <= k; ++i){
        cin >> c[i];
        t.pb({c[i], i});
    }
    sort(t.begin(), t.end());
    t.resize(unique(t.begin(), t.end()) - t.begin());
    sort(b + 1, b + n + 1,[](const auto &i, const auto &j){
        return i.high < j.high;
    });
    int j = 1;
    for (int i = 1; i <= n; ++i){
        while (j <= k && t[j - 1].fi < b[i].high){
            update(t[j - 1].fi, t[j - 1].se);
            ++j;
        }
        flip[b[i].idx] = {get(b[i].low), b[i].idx, i};
    }
    sort(flip + 1, flip + n + 1,[](const auto &i, const auto &j){
        return i.val > j.val;
    });
    j = k;
    for (int i = 1; i <= n; ++i){
        while (j > 0 && j > flip[i].val){
            update2(c[j], 1);
            --j;
        }
        cnt[flip[i].idx1] = get2(b[flip[i].idx2].high);
        cnt1[flip[i].idx1] = flip[i].val;
    }
    ll answer = 0;
    for (int i = 1; i <= n; ++i){
        if (cnt1[i]){
            if (cnt[i] % 2) answer += min(a[i].fi, a[i].se);
            else answer += max(a[i].fi, a[i].se);
        }
        else{
            if (cnt[i] % 2) answer += a[i].se;
            else answer += a[i].fi;
        }
    }
    cout << answer;
}


int main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        //freopen(taskname".out", "w", stdout);
    }
    faster;
    ll tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
}

Compilation message

fortune_telling2.cpp: In function 'void update2(int, int)':
fortune_telling2.cpp:63:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for (; x <= t.size(); x += x & -x){
      |            ~~^~~~~~~~~~~
fortune_telling2.cpp: In function 'int get(int)':
fortune_telling2.cpp:73:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for (; x <= t.size(); x += x & -x){
      |            ~~^~~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 10 ms 1064 KB Output is correct
15 Correct 16 ms 1692 KB Output is correct
16 Correct 26 ms 2264 KB Output is correct
17 Correct 41 ms 2960 KB Output is correct
18 Correct 36 ms 2760 KB Output is correct
19 Correct 29 ms 2632 KB Output is correct
20 Correct 36 ms 2764 KB Output is correct
21 Correct 28 ms 2600 KB Output is correct
22 Correct 23 ms 2640 KB Output is correct
23 Correct 21 ms 2768 KB Output is correct
24 Correct 21 ms 2756 KB Output is correct
25 Correct 19 ms 2644 KB Output is correct
26 Correct 30 ms 2528 KB Output is correct
27 Correct 31 ms 2772 KB Output is correct
28 Correct 30 ms 2800 KB Output is correct
29 Correct 34 ms 2784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 10 ms 1064 KB Output is correct
15 Correct 16 ms 1692 KB Output is correct
16 Correct 26 ms 2264 KB Output is correct
17 Correct 41 ms 2960 KB Output is correct
18 Correct 36 ms 2760 KB Output is correct
19 Correct 29 ms 2632 KB Output is correct
20 Correct 36 ms 2764 KB Output is correct
21 Correct 28 ms 2600 KB Output is correct
22 Correct 23 ms 2640 KB Output is correct
23 Correct 21 ms 2768 KB Output is correct
24 Correct 21 ms 2756 KB Output is correct
25 Correct 19 ms 2644 KB Output is correct
26 Correct 30 ms 2528 KB Output is correct
27 Correct 31 ms 2772 KB Output is correct
28 Correct 30 ms 2800 KB Output is correct
29 Correct 34 ms 2784 KB Output is correct
30 Correct 55 ms 4672 KB Output is correct
31 Correct 113 ms 9268 KB Output is correct
32 Correct 146 ms 12172 KB Output is correct
33 Correct 202 ms 17992 KB Output is correct
34 Correct 44 ms 5372 KB Output is correct
35 Correct 219 ms 17948 KB Output is correct
36 Correct 203 ms 18048 KB Output is correct
37 Correct 204 ms 17984 KB Output is correct
38 Correct 162 ms 17216 KB Output is correct
39 Correct 194 ms 17944 KB Output is correct
40 Correct 177 ms 16952 KB Output is correct
41 Correct 147 ms 17480 KB Output is correct
42 Correct 156 ms 17744 KB Output is correct
43 Correct 105 ms 16564 KB Output is correct
44 Correct 104 ms 16532 KB Output is correct
45 Correct 102 ms 16444 KB Output is correct
46 Correct 112 ms 15368 KB Output is correct
47 Correct 117 ms 15960 KB Output is correct
48 Correct 170 ms 18076 KB Output is correct
49 Correct 178 ms 17836 KB Output is correct