Submission #1344683

#TimeUsernameProblemLanguageResultExecution timeMemory
1344683iamhereforfunFortune Telling 2 (JOI14_fortune_telling2)C++20
0 / 100
0 ms464 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 3e5 + 5;
const int M = 1e6 + 5;
const int K = 20;
const int LG = 20;
const long long INF = 1e18 + 5;
const int C = 26;
const int B = 1000;
const int MOD = 998244353;

struct Fenwick
{
    vector<int> ft;
    int n;
    Fenwick(int len)
    {
        n = len;
        ft.assign(n + 1, 0);
    }
    void update(int pos, int val)
    {
        while (pos <= n)
        {
            ft[pos] += val;
            pos += LSOne(pos);
        }
    }
    int get(int pos)
    {
        int sum = 0;
        while (pos > 0)
        {
            sum += ft[pos];
            pos -= LSOne(pos);
        }
        return sum;
    }
    int get(int l, int r)
    {
        return get(r) - get(l - 1);
    }
};

int n, q, val[LG][N], mx[N], mask[N], pos[N], query[N];
long long ans;
pair<int, int> card[N], compos[N];
vector<pair<int, int>> ask[N];
vector<int> v;

void dnc(int l, int r, int v)
{
    if (l > r)
        return;
    if (l == r)
    {
        val[v][l] = mx[l];
        return;
    }
    int m = (l + r) / 2;
    val[v][m] = mx[m];
    for (int x = m - 1; x >= l; x--)
    {
        val[v][x] = max(val[v][x + 1], mx[x]);
    }
    val[v][m + 1] = mx[m + 1];
    for (int x = m + 2; x <= r; x++)
    {
        val[v][x] = max(val[v][x - 1], mx[x]);
    }
    for (int x = m + 1; x <= r; x++)
    {
        mask[x] ^= (1 << x);
    }
    dnc(l, m, v + 1);
    dnc(m + 1, r, v + 1);
}

int get(int l, int r)
{
    if (l == r)
        return mx[l];
    int i = __builtin_ctz(mask[l] ^ mask[r]);
    return max(val[i][l], val[i][r]);
}

inline void solve()
{
    cin >> n >> q;
    ans = 0;
    for (int x = 0; x < n; x++)
    {
        cin >> card[x].first >> card[x].second;
        v.push_back(card[x].first);
        v.push_back(card[x].second);
    }
    for (int x = 1; x <= q; x++)
    {
        int i;
        cin >> i;
        query[x] = i;
        v.push_back(i);
        mx[i] = max(mx[i], x);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for (int x = 0; x < n; x++)
    {
        compos[x].first = lower_bound(v.begin(), v.end(), card[x].first) - v.begin() + 1;
        compos[x].second = lower_bound(v.begin(), v.end(), card[x].first) - v.begin() + 1;
    }
    for (int x = 1; x <= q; x++)
    {
        int p = lower_bound(v.begin(), v.end(), query[x]) - v.begin() + 1;
        // cout << p << " " << query[x] << "\n";
        mx[p] = max(mx[p], x);
    }
    dnc(1, v.size() + 1, 0);
    for (int x = 0; x < n; x++)
    {
        auto [i, j] = card[x];
        auto [ii, jj] = compos[x];
        if (i > j)
        {
            swap(i, j);
            swap(ii, jj);
        }
        pos[x] = get(ii, jj - 1);
        // cout << pos[x] << " " << i << " " << j << "\n";
        if (pos[x] == q)
        {
            ans += card[x].first;
        }
        else
        {
            ask[pos[x] + 1].push_back({j, x});
        }
    }
    Fenwick ft(n + 1);
    for (int x = q; x >= 1; x--)
    {
        ft.update(query[x], 1);
        for (auto &[i, id] : ask[x])
        {
            if (card[id].first > card[id].second)
                swap(card[id].first, card[id].second);
            i = ft.get(i, v.size());
            if (x == 1)
            {
                if (i % 2 == 1)
                {
                    ans += card[id].second;
                    // cout << card[id].second << " " << id << "\n";
                }
                else
                {
                    ans += card[id].first;
                    // cout << card[id].first << " " << id << "\n";
                }
            }
            else
            {
                if (i % 2 == 0)
                {
                    ans += card[id].second;
                    // cout << card[id].second << " " << id << "\n";
                }
                else
                {
                    ans += card[id].first;
                    // cout << card[id].first << " " << id << "\n";
                }
            }
        }
    }
    cout << ans << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...