Submission #1100897

#TimeUsernameProblemLanguageResultExecution timeMemory
1100897vjudge1Fortune Telling 2 (JOI14_fortune_telling2)C++17
100 / 100
167 ms31048 KiB
#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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...