Submission #317592

# Submission time Handle Problem Language Result Execution time Memory
317592 2020-10-30T03:41:57 Z ThaiBaHung Fortune Telling 2 (JOI14_fortune_telling2) C++14
35 / 100
681 ms 54868 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 3;
int n, k;
struct ok2
{
    long long A;
    long long B;
    int cs;
};
ok2 a[N];
int pos[N][3];
int b[N];
int pos2[N];

vector < pair < int, int > > num;
pair < int, int > roirac[N];

struct ok
{
    int lo;
    int hi;
    int pos;
    int cnt;
};
ok Node[4 * N], Sum[4 * N];

void build(int id, int l, int r, ok Node[])
{
    Node[id].lo = l; Node[id].hi = r;
    if (l == r) return;
    int mid = (l + r) / 2;

    build(id * 2,l, mid, Node);
    build(id * 2 + 1, mid + 1, r, Node);
}

void update(int id, int val, int pos, ok Node[])
{
    if (Node[id].lo > pos || Node[id].hi < pos) return;

    if (Node[id].lo == Node[id].hi)
    {
        Node[id].pos = val;
        Node[id].cnt = 1;
        return;
    }

    update(id * 2, val, pos, Node);
    update(id * 2 + 1, val, pos, Node);

    Node[id].pos = max(Node[id * 2].pos, Node[id * 2 + 1].pos);
    Node[id].cnt = Node[id * 2].cnt + Node[id * 2 + 1].cnt;
}

int getpos(int id, int l, int r)
{
    if (l > r) return 0;
    if (Node[id].lo > r || Node[id].hi < l) return 0;

    if (l <= Node[id].lo && Node[id].hi <= r)
        return Node[id].pos;

    return max(getpos(id * 2,l,r), getpos(id * 2 + 1,l,r));
}

int getsum(int id, int pos)
{
    if (Sum[id].hi < pos) return 0;

    if (Sum[id].lo >= pos)
        return Sum[id].cnt;

    return getsum(id * 2, pos) + getsum(id * 2 + 1, pos);
}

bool cmp(ok2 a, ok2 b)
{
    return max(a.A, a.B) > max(b.A, b.B);
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("t.inp","r",stdin); freopen("t.out","w",stdout);

    cin >> n >> k;

    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].A >> a[i].B;
        num.push_back({a[i].A,i});
        num.push_back({a[i].B,i});
        a[i].cs = i;
    }

    for (int i = 1; i <= k; i++)
    {
        cin >> b[i];
        num.push_back({b[i],i + n});
        roirac[i].first = b[i];
        roirac[i].second = i;
    }

    sort(num.begin(), num.end());

    int m = num.size();
    int dem = 1;

    for (int i = 0; i < m; i++)
    {
        int cur = num[i].first;
        int vt = num[i].second;

        if (i >= 1 && cur != num[i - 1].first) dem++;

        if (vt <= n)
        {
            if (pos[vt][1]) pos[vt][2] = dem;
            else
                if (pos[vt][2]) pos[vt][1] = dem;
                else
                    if (a[vt].A > a[vt].B) pos[vt][2] = dem;
                    else pos[vt][1] = dem;
        }
        else
            pos2[vt - n] = dem;
    }

    build(1,1,k,Sum);
    build(1,1,m,Node);

    sort(a + 1, a + n + 1, cmp);

    for (int i = 1; i <= k; i++)
        update(1,i,pos2[i],Node);

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

    int lo = k;

    long long res = 0;

//    for (int i = 1; i <= n; i++)
//    {
//        cout << a[i].A << " " << a[i].B << '\n';
//    }

    for (int i = 1; i <= n; i++)
    {
        while (lo && roirac[lo].first >= max(a[i].A, a[i].B))
        {
            update(1,1,roirac[lo].second,Sum);
            lo--;
        }

        int vt = getpos(1, min(pos[a[i].cs][1], pos[a[i].cs][2]), max(pos[a[i].cs][1], pos[a[i].cs][2]) - 1);

        int bonus = 0;
        if (vt && a[i].A < a[i].B) bonus = 1;

        int to = getsum(1, vt + 1);
        //cout << getsum(1,10) << '\n';
        //cout << min(pos[a[i].cs][1], pos[a[i].cs][2]) << " " << max(pos[a[i].cs][1], pos[a[i].cs][2]) - 1 << ' ';
        //cout << bonus << " " << vt << " " << to << ' ';

        if ((to + bonus) % 2)
        {
            res += a[i].B;
            //cout << a[i].B << '\n';
        }
        else
        {
            res += a[i].A;
            //cout << a[i].A << '\n';
        }
    }

    cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 692 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 692 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 21 ms 2848 KB Output is correct
15 Correct 48 ms 5116 KB Output is correct
16 Correct 74 ms 7920 KB Output is correct
17 Correct 104 ms 9580 KB Output is correct
18 Correct 104 ms 9584 KB Output is correct
19 Correct 93 ms 9712 KB Output is correct
20 Correct 97 ms 9584 KB Output is correct
21 Correct 81 ms 9584 KB Output is correct
22 Correct 71 ms 9592 KB Output is correct
23 Correct 73 ms 9720 KB Output is correct
24 Correct 76 ms 9716 KB Output is correct
25 Correct 69 ms 9584 KB Output is correct
26 Correct 86 ms 9324 KB Output is correct
27 Correct 88 ms 9584 KB Output is correct
28 Correct 82 ms 9584 KB Output is correct
29 Correct 85 ms 9580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 692 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 21 ms 2848 KB Output is correct
15 Correct 48 ms 5116 KB Output is correct
16 Correct 74 ms 7920 KB Output is correct
17 Correct 104 ms 9580 KB Output is correct
18 Correct 104 ms 9584 KB Output is correct
19 Correct 93 ms 9712 KB Output is correct
20 Correct 97 ms 9584 KB Output is correct
21 Correct 81 ms 9584 KB Output is correct
22 Correct 71 ms 9592 KB Output is correct
23 Correct 73 ms 9720 KB Output is correct
24 Correct 76 ms 9716 KB Output is correct
25 Correct 69 ms 9584 KB Output is correct
26 Correct 86 ms 9324 KB Output is correct
27 Correct 88 ms 9584 KB Output is correct
28 Correct 82 ms 9584 KB Output is correct
29 Correct 85 ms 9580 KB Output is correct
30 Correct 313 ms 22156 KB Output is correct
31 Correct 399 ms 32480 KB Output is correct
32 Correct 496 ms 34912 KB Output is correct
33 Incorrect 681 ms 54868 KB Output isn't correct
34 Halted 0 ms 0 KB -