제출 #1271015

#제출 시각아이디문제언어결과실행 시간메모리
1271015banhcuon14운세 보기 2 (JOI14_fortune_telling2)C++20
35 / 100
107 ms8240 KiB
//https://oj.uz/problem/view/JOI14_fortune_telling2
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOD(i, a, b) for (int i = (a); i >= (b); --i)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 7 + 1e9;
const int maxn = 5 + 2e5;

int n, m, q, a[maxn], b[maxn], k[maxn];
int bit[maxn], t[maxn << 2];
vector<int> c;

struct SegmentTree {
    vector<int> T;

    SegmentTree(int n) {
        T.resize(4 * n + 1);
    }

    void update(int s, int l, int r, int x, int v) {
        if (l == r) {
            T[s] = v;
            return;
        }
        int mid = (l + r) >> 1;
        if (x <= mid) update(s << 1, l, mid, x, v);
        else update(s << 1 | 1, mid + 1, r, x, v);
        T[s] = max(T[s << 1], T[s << 1 | 1]);
    }

    int get(int s, int l, int r, int u, int v) {
        if (l > v || r < u || u > v) return 0;
        if (l >= u && r <= v) return T[s];
        int mid = (l + r) >> 1;
        return max(get(s << 1, l, mid, u, v), get(s << 1 | 1, mid + 1, r, u, v));
    }
};

void up(int x) {
    for (; x; x -= x & -x) ++bit[x];
}

int calc(int x) {
    int res = 0;
    for (; x < maxn; x += x & -x) res += bit[x];
    return res;
}


// void up(int id) {
//     for (; id; id -= id & -id) ++bit[id];
// }

// int calc(int id) {
//     int res = 0;
//     for (; id <= m; id += id & -id) res += bit[id];
//     return res;
// }

signed main() {
    #define name "task"
    if (fopen(name".inp", "r")) {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    cin >> n >> q;
    vector<pair<int, int>> qq;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i] >> b[i];
        c.push_back(a[i]);
        c.push_back(b[i]);
    }
    for (int i = 1; i <= q; ++i) {
        cin >> k[i];
        qq.emplace_back(k[i], i);
        c.push_back(k[i]);
    }
    sort(c.begin(), c.end());
    c.resize(unique(c.begin(), c.end()) - c.begin());
    m = c.size();
    SegmentTree it(m);
    for (auto &[x, id]: qq) {
        it.update(1, 1, m, upper_bound(c.begin(), c.end(), x) - c.begin(), id);
    }
    
    vector<tuple<int, int, int>> qr;
    for (int i = 1; i <= n; ++i) {
        int u = upper_bound(c.begin(), c.end(), a[i]) - c.begin();
        int v = upper_bound(c.begin(), c.end(), b[i]) - c.begin();
        if (u > v) swap(u, v);
        qr.emplace_back(it.get(1, 1, m, u, v - 1), max(a[i], b[i]), i);
    }
    sort(qr.begin(), qr.end());
    reverse(qq.begin(), qq.end());
    for (auto &[x, id]: qq) {
        up(upper_bound(c.begin(), c.end(), x) - c.begin());
        while(qr.size() && get<0>(qr.back()) == id) {
            auto [_, val, i] = qr.back(); qr.pop_back();
            int cnt = calc(upper_bound(c.begin(), c.end(), val) - c.begin());
            if (cnt & 1) {
                if (a[i] > b[i]) swap(a[i], b[i]);
            }
            else {
                if (a[i] < b[i]) swap(a[i], b[i]);
            }
        }
    }
    while (qr.size()) {
        auto [_, val, i] = qr.back(); qr.pop_back();
        int cnt = calc(upper_bound(c.begin(), c.end(), val) - c.begin());
        if (cnt & 1) swap(a[i], b[i]);
    }
    ll res = 0;
    for (int i = 1; i <= n; ++i) res += a[i];
    cout << res;

    
    return 0;
}



컴파일 시 표준 에러 (stderr) 메시지

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