답안 #317598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
317598 2020-10-30T03:46:17 Z ThaiBaHung 운세 보기 2 (JOI14_fortune_telling2) C++14
35 / 100
766 ms 104640 KB
#include<bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 3;
long long n, k;
struct ok2
{
    long long A;
    long long B;
    long long cs;
};
ok2 a[N];
long long pos[N][3];
long long b[N];
long long pos2[N];

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

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

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

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

void update(long long id, long long val, long long 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;
}

long long getpos(long long id, long long l, long long 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));
}

long long getsum(long long id, long long 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 (long long 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 (long long 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());

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

    for (long long i = 0; i < m; i++)
    {
        long long cur = num[i].first;
        long long 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 (long long i = 1; i <= k; i++)
        update(1,i,pos2[i],Node);

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

    long long lo = k;

    long long res = 0;

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

    for (long long 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--;
        }

        long long 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);

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

        long long 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 896 KB Output is correct
4 Correct 2 ms 896 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 2 ms 896 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 2 ms 896 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 2 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 896 KB Output is correct
4 Correct 2 ms 896 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 2 ms 896 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 2 ms 896 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 2 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 25 ms 4860 KB Output is correct
15 Correct 52 ms 9208 KB Output is correct
16 Correct 85 ms 14568 KB Output is correct
17 Correct 120 ms 17896 KB Output is correct
18 Correct 114 ms 17900 KB Output is correct
19 Correct 110 ms 17768 KB Output is correct
20 Correct 112 ms 17772 KB Output is correct
21 Correct 94 ms 17780 KB Output is correct
22 Correct 83 ms 17896 KB Output is correct
23 Correct 86 ms 17896 KB Output is correct
24 Correct 88 ms 17768 KB Output is correct
25 Correct 77 ms 17768 KB Output is correct
26 Correct 91 ms 17256 KB Output is correct
27 Correct 95 ms 17768 KB Output is correct
28 Correct 93 ms 17868 KB Output is correct
29 Correct 99 ms 17908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 896 KB Output is correct
4 Correct 2 ms 896 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 2 ms 896 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 2 ms 896 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 2 ms 896 KB Output is correct
12 Correct 2 ms 896 KB Output is correct
13 Correct 3 ms 896 KB Output is correct
14 Correct 25 ms 4860 KB Output is correct
15 Correct 52 ms 9208 KB Output is correct
16 Correct 85 ms 14568 KB Output is correct
17 Correct 120 ms 17896 KB Output is correct
18 Correct 114 ms 17900 KB Output is correct
19 Correct 110 ms 17768 KB Output is correct
20 Correct 112 ms 17772 KB Output is correct
21 Correct 94 ms 17780 KB Output is correct
22 Correct 83 ms 17896 KB Output is correct
23 Correct 86 ms 17896 KB Output is correct
24 Correct 88 ms 17768 KB Output is correct
25 Correct 77 ms 17768 KB Output is correct
26 Correct 91 ms 17256 KB Output is correct
27 Correct 95 ms 17768 KB Output is correct
28 Correct 93 ms 17868 KB Output is correct
29 Correct 99 ms 17908 KB Output is correct
30 Correct 359 ms 43488 KB Output is correct
31 Correct 463 ms 63184 KB Output is correct
32 Correct 552 ms 67028 KB Output is correct
33 Incorrect 766 ms 104640 KB Output isn't correct
34 Halted 0 ms 0 KB -