Submission #1335186

#TimeUsernameProblemLanguageResultExecution timeMemory
1335186adscodingFortune Telling 2 (JOI14_fortune_telling2)C++20
100 / 100
172 ms23220 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 2e5 + 3;
int n, q, a[maxn][2];

// --------------------------------------------------------------------------------------------

namespace sub1
{
    int ans[maxn];

    void solve()
    {
        while (q--)
        {
            int x; cin >> x;
            FOR(i, 1, n)
                if (a[i][ans[i]] <= x)
                    ans[i] ^= 1;
        }

        ll res = 0;
        FOR(i, 1, n)
            res += a[i][ans[i]];
        cout << res;
    }
}

namespace AC
{
    int T[maxn], state[maxn], Ma[maxn][20], lg[maxn];
    vector<int> ids;

    struct FenTree
    {
        vector<int> fw;
        void init(int n)
        {
            fw.assign(n + 3, 0);
        }
        void upd(int pos, int val)
        {
            for (; pos; pos &= pos - 1)
                fw[pos] ^= val;
        }
        int get(int pos)
        {
            int res = 0;
            for (; pos < int(fw.size()); pos += pos & -pos)
                res ^= fw[pos];
            return res;
        }
    };

    int lower(int x) {return lower_bound(all(ids), x) - ids.begin() + 1;}
    int upper(int x) {return upper_bound(all(ids), x) - ids.begin() + 1;}

    int get_max(int l, int r)
    {
        l = max(l, 1);
        r = min(r, int(ids.size()));
        if (l > r) return 0;
        int k = lg[r - l + 1];
        return max(Ma[l][k], Ma[r - (1 << k) + 1][k]);
    }

    void solve()
    {
        FOR(i, 1, n)
        {
            if (a[i][0] > a[i][1])
            {
                state[i] = 1;
                swap(a[i][0], a[i][1]);
            }
        }

        FOR(iq, 1, q)
            cin >> T[iq];
        vector<int> sortT(q);
        iota(all(sortT), 1);
        sort(all(sortT), [&](const int &x, const int &y) {
            return T[x] > T[y];
        });

        vector<int> sortA(n);
        iota(all(sortA), 1);
        sort(all(sortA), [&](const int &x, const int &y) {
            return a[x][1] > a[y][1];
        });

        FOR(i, 1, q)
        {
            ids.push_back(T[i]);
        }
        uni(ids);

        FOR(iq, 1, q)
        {
            int id = lower(T[iq]);
            maximize(Ma[id][0], iq);
        }

        FOR(j, 1, 19)
            FOR(i, 1, int(ids.size()) - (1 << j) + 1)
                Ma[i][j] = max(Ma[i][j - 1], Ma[i + (1 << j - 1)][j - 1]);
        FOR(i, 2, q)
            lg[i] = lg[i >> 1] + 1;

        FenTree fw;
        fw.init(q);

        ll res = 0;
        int j = 0;
        for (int iA : sortA)
        {
            while (j < int(sortT.size()) && T[sortT[j]] >= a[iA][1])
            {
                fw.upd(sortT[j], 1);
                ++j;
            }

            int l = lower(a[iA][0]), r = lower(a[iA][1]) - 1;
            int pos = get_max(l, r);
            if (pos == 0)
            {
                res += a[iA][fw.get(1) ^ state[iA]];
            }
            else
            {
                res += a[iA][fw.get(pos) ^ 1];
            }
        }

        cout << res;
    }
}

void solve()
{
    cin >> n >> q;
    FOR(i, 1, n)
        cin >> a[i][0] >> a[i][1];

    AC :: solve();
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

Compilation message (stderr)

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:224:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  224 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:225:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  225 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...