답안 #1100896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100896 2024-10-15T02:11:15 Z cpismylifeOwO 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
143 ms 31048 KB
#include <bits/stdc++.h>

using namespace std;

const long long mod = 1e9 + 7;
const int MaxN = 2e5 + 5;
const int MaxLog = 19;

int n, q;
pair<long long, long long> arr[MaxN];
pair<long long, int> query[MaxN];

void Inp()
{
    cin >> n >> q;
    for (int x = 1; x <= n; x++)
    {
        cin >> arr[x].first >> arr[x].second;
    }
    for (int x = 1; x <= q; x++)
    {
        cin >> query[x].first;
        query[x].second = x;
    }
    sort(query + 1, query + q + 1);
}

int Sparse[MaxLog][MaxN];

void Build()
{
    for (int x = 1; x <= q; x++)
    {
        Sparse[0][x] = query[x].second;
    }
    for (int x = 1; x < MaxLog; x++)
    {
        for (int y = 1; y + (1 << x) - 1 <= q; y++)
        {
            Sparse[x][y] = max(Sparse[x - 1][y], Sparse[x - 1][y + (1 << (x - 1))]);
        }
    }
}

int Get(int l, int r)
{
    if (r < l)
    {
        return 0;
    }
    int p = __lg(r - l + 1);
    return max(Sparse[p][l], Sparse[p][r - (1 << p) + 1]);
}

int Fenwick[MaxN];

void Update(int u)
{
    int idx = u;
    while (idx <= q)
    {
        Fenwick[idx]++;
        idx += (idx & (-idx));
    }
}

int Query(int u)
{
    int idx = u, res = 0;
    while (idx > 0)
    {
        res += Fenwick[idx];
        idx -= (idx & (-idx));
    }
    return res;
}

vector<int> add[MaxN];

void Exc()
{
    Build();
    for (int x = 1; x <= n; x++)
    {
        int l = 1, r = q, mid, res = q + 1;
        while (l <= r)
        {
            mid = (l + r) / 2;
            if (query[mid].first >= max(arr[x].first, arr[x].second))
            {
                res = mid;
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        add[res].push_back(x);
    }
    long long ans = 0;
    for (int w = q + 1; w > 0; w--)
    {
        if (w <= q)
        {
            Update(query[w].second);
        }
        for (int x : add[w])
        {
            int l = 1, r = q, mid, res = q + 1;
            while (l <= r)
            {
                mid = (l + r) / 2;
                if (min(arr[x].first, arr[x].second) <= query[mid].first)
                {
                    res = mid;
                    r = mid - 1;
                }
                else
                {
                    l = mid + 1;
                }
            }
            int k = Get(res, w - 1), p = Query(q) - Query(k);
            if (k != 0)
            {
                if (p & 1)
                {
                    ans += min(arr[x].first, arr[x].second);
                }
                else
                {
                    ans += max(arr[x].first, arr[x].second);
                }
            }
            else
            {
                if (p & 1)
                {
                    ans += arr[x].second;
                }
                else
                {
                    ans += arr[x].first;
                }
            }
            //cout << x << " " << w << " " << res << " " << k << " " << p << " " << ans << "\n";
        }
    }
    cout << ans;
}

int main()
{
    //freopen("A.INP", "r", stdin);
    //freopen("A.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int test = 1;
    //cin >> test;
    for (int w = 1; w <= test; w++)
    {
        Inp();
        Exc();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19024 KB Output is correct
2 Correct 3 ms 19024 KB Output is correct
3 Correct 3 ms 19024 KB Output is correct
4 Correct 3 ms 19024 KB Output is correct
5 Correct 3 ms 19024 KB Output is correct
6 Correct 3 ms 19104 KB Output is correct
7 Correct 3 ms 19024 KB Output is correct
8 Correct 3 ms 19024 KB Output is correct
9 Correct 3 ms 19024 KB Output is correct
10 Correct 3 ms 19024 KB Output is correct
11 Correct 3 ms 19024 KB Output is correct
12 Correct 3 ms 19024 KB Output is correct
13 Correct 3 ms 19024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19024 KB Output is correct
2 Correct 3 ms 19024 KB Output is correct
3 Correct 3 ms 19024 KB Output is correct
4 Correct 3 ms 19024 KB Output is correct
5 Correct 3 ms 19024 KB Output is correct
6 Correct 3 ms 19104 KB Output is correct
7 Correct 3 ms 19024 KB Output is correct
8 Correct 3 ms 19024 KB Output is correct
9 Correct 3 ms 19024 KB Output is correct
10 Correct 3 ms 19024 KB Output is correct
11 Correct 3 ms 19024 KB Output is correct
12 Correct 3 ms 19024 KB Output is correct
13 Correct 3 ms 19024 KB Output is correct
14 Correct 8 ms 21240 KB Output is correct
15 Correct 13 ms 21460 KB Output is correct
16 Correct 19 ms 23632 KB Output is correct
17 Correct 24 ms 25680 KB Output is correct
18 Correct 25 ms 25852 KB Output is correct
19 Correct 23 ms 25680 KB Output is correct
20 Correct 24 ms 25664 KB Output is correct
21 Correct 21 ms 25424 KB Output is correct
22 Correct 16 ms 25680 KB Output is correct
23 Correct 17 ms 25680 KB Output is correct
24 Correct 18 ms 25936 KB Output is correct
25 Correct 16 ms 25680 KB Output is correct
26 Correct 18 ms 25680 KB Output is correct
27 Correct 20 ms 25680 KB Output is correct
28 Correct 20 ms 25680 KB Output is correct
29 Correct 23 ms 25680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19024 KB Output is correct
2 Correct 3 ms 19024 KB Output is correct
3 Correct 3 ms 19024 KB Output is correct
4 Correct 3 ms 19024 KB Output is correct
5 Correct 3 ms 19024 KB Output is correct
6 Correct 3 ms 19104 KB Output is correct
7 Correct 3 ms 19024 KB Output is correct
8 Correct 3 ms 19024 KB Output is correct
9 Correct 3 ms 19024 KB Output is correct
10 Correct 3 ms 19024 KB Output is correct
11 Correct 3 ms 19024 KB Output is correct
12 Correct 3 ms 19024 KB Output is correct
13 Correct 3 ms 19024 KB Output is correct
14 Correct 8 ms 21240 KB Output is correct
15 Correct 13 ms 21460 KB Output is correct
16 Correct 19 ms 23632 KB Output is correct
17 Correct 24 ms 25680 KB Output is correct
18 Correct 25 ms 25852 KB Output is correct
19 Correct 23 ms 25680 KB Output is correct
20 Correct 24 ms 25664 KB Output is correct
21 Correct 21 ms 25424 KB Output is correct
22 Correct 16 ms 25680 KB Output is correct
23 Correct 17 ms 25680 KB Output is correct
24 Correct 18 ms 25936 KB Output is correct
25 Correct 16 ms 25680 KB Output is correct
26 Correct 18 ms 25680 KB Output is correct
27 Correct 20 ms 25680 KB Output is correct
28 Correct 20 ms 25680 KB Output is correct
29 Correct 23 ms 25680 KB Output is correct
30 Correct 40 ms 25488 KB Output is correct
31 Correct 62 ms 26440 KB Output is correct
32 Correct 85 ms 27212 KB Output is correct
33 Correct 134 ms 29860 KB Output is correct
34 Correct 36 ms 25252 KB Output is correct
35 Correct 136 ms 30036 KB Output is correct
36 Correct 133 ms 30024 KB Output is correct
37 Correct 143 ms 30028 KB Output is correct
38 Correct 135 ms 29608 KB Output is correct
39 Correct 136 ms 30372 KB Output is correct
40 Correct 107 ms 28320 KB Output is correct
41 Correct 116 ms 30024 KB Output is correct
42 Correct 123 ms 30024 KB Output is correct
43 Correct 76 ms 27972 KB Output is correct
44 Correct 69 ms 27976 KB Output is correct
45 Correct 76 ms 27976 KB Output is correct
46 Correct 97 ms 30024 KB Output is correct
47 Correct 85 ms 31048 KB Output is correct
48 Correct 105 ms 29320 KB Output is correct
49 Correct 107 ms 29260 KB Output is correct