QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#62050#3850. DJ Darko2pal1rakWA 344ms19384kbC++145.0kb2022-11-17 02:25:112022-11-17 02:25:15

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-17 02:25:15]
  • 评测
  • 测评结果:WA
  • 用时:344ms
  • 内存:19384kb
  • [2022-11-17 02:25:11]
  • 提交

answer

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <random>
#include <cstdlib>

#define debug(x) std::cout << #x << " " << (x) << '\n';
#define pb push_back
#define mp std::make_pair
#define remax(a, b) a = std::max((a), (b));
#define remin(a, b) a = std::min((a), (b));

class FenwickTree {
private:
    std::vector<int64_t> data;

public:
    FenwickTree(int32_t n): data(n + 1, 0) {}

    void add(int32_t ind, int64_t v) {
        while(ind < data.size()) {
            data[ind] += v;
            ind += (ind & (-ind));
        }
    }

    int64_t get(int32_t ind) {
        int64_t res = 0;
        while(ind > 0) {
            res += data[ind];
            ind -= (ind & (-ind));
        }
        return res;
    }
};

struct Interval {
    int32_t l, len;
    int64_t val;

    Interval(int32_t _l, int32_t _len, int64_t _val): l(_l), len(_len), val(_val) {}

    bool operator<(const Interval &o) const {
        if(l != o.l) return l < o.l;
        else if(len != o.len) return len < o.len;
        else return val < o.val;
    }
};

void splitIntervals(std::set<Interval> &s, int32_t l, int32_t r) {
    auto it = s.lower_bound(Interval(l + 1, -1, 0));
    if(it != s.begin()) {
        it--;
        if(it->l < l && it->l + it->len - 1 > l) {
            auto i1 = Interval(it->l, l - it->l, it->val);
            auto i2 = Interval(l, it->len - (l - it->l), it->val);
            s.erase(it);
            if(i1.len > 0)
            s.insert(i1);
            if(i2.len)
            s.insert(i2);
        }
    }

    it = s.lower_bound(Interval(r + 1, -1, 0));
    if(it != s.begin()) {
        it--;
        if(it->l + it->len > r + 1) {
            auto i1 = Interval(it->l, r - it->l + 1, it->val);
            auto i2 = Interval(r + 1, it->len - (r - it->l + 1), it->val);
            s.erase(it);
            if(i1.len)
            s.insert(i1);
            if(i2.len)
            s.insert(i2);
        }
    }
}

int main() {
#ifdef USEFOPEN
    freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
#endif
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int32_t n, q;
    std::cin >> n >> q;

    std::vector<int32_t> a(n);
    for(int32_t i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    std::vector<int64_t> b(n);
    for(int32_t i = 0; i < n; i++) {
        std::cin >> b[i];

        if(i != 0) {
            b[i] += b[i - 1];
        }
    }

    auto getIntervalB = [&b](int32_t l, int32_t r) {
        l--; r--;
        return b[r] - (l == 0 ? 0 : b[l - 1]);
    };

    auto check = [](int32_t m, const std::vector<std::pair<int64_t, int64_t>> &x) {
        int64_t middle = x[m].first, res = 0;
        for(int32_t i = 0; i < x.size(); i++) {
            res += abs(x[i].first - middle) * x[i].second;
        }
        return res;
    };

    std::set<Interval> s;
    s.insert(Interval(n + 1, 0, 0));
    s.insert(Interval(-1, 0, 0));
    for(int32_t i = 0; i < n; i++) {
        s.insert(Interval(i + 1, 1, a[i]));
    }

    FenwickTree fenwick(n);

    for(int32_t i = 0; i < q; i++) {
        int32_t type, l, r;
        std::cin >> type >> l >> r;

        if(type == 1) {
            int32_t x;
            std::cin >> x;

            fenwick.add(l, x);
            fenwick.add(r + 1, -x);

            splitIntervals(s, l, r);
        }
        else {
            splitIntervals(s, l, r);

            std::vector<std::pair<int64_t, int64_t>> x;
            auto it = s.lower_bound(Interval(l, -1, 0));
            while(it->l <= r) {
                int64_t prefSum = fenwick.get(it->l);
                fenwick.add(it->l, -prefSum);
                fenwick.add(it->l + it->len, prefSum);

                x.pb({ it->val + prefSum, getIntervalB(it->l, it->l + it->len - 1) });

                auto jt = it;
                it++;

                s.erase(jt);
            }

            std::sort(x.begin(), x.end());

            int32_t low = 0, high = x.size() - 1;
            while(low < high) {
                int32_t mid = (low + high) / 2;

                //std::cout << mid << " " << check(mid, x) << " " << check(mid + 1, x) << '\n';
                if(check(mid, x) <= check(mid + 1, x)) {
                    high = mid;
                }
                else {
                    low = mid + 1;
                }
            }

            assert(low == high);
            std::cout << x[low].first << '\n';

            s.insert(Interval(l, r - l + 1, x[low].first));
        }

        /**
        for(auto &x : s) {
            std::cout << " { " << x.l << ", " << x.len << ", " << x.val << " }, ";
        }
        std::cout << '\n';
        */
    }
}

详细

Test #1:

score: 0
Wrong Answer
time: 344ms
memory: 19384kb

input:

200000 200000
185413631 745038744 881479208 394948467 101727403 796960399 284541402 80768155 286582974 546327406 197495962 552359542 727479505 437895671 143092948 7626834 741609268 540494577 298656274 548860413 41137417 210529949 658779847 161355446 486548926 602900245 119414972 310187428 238177860 ...

output:

462406736
1749348519
1749348519
467651597
874061694
874061694
867109293
874061694
-8474150
874061694
-893932302
-556410550
251019443
238578725
238578725
213568542
213568542
213568542
213568542
213568542
-802248887
-38403634
-38403634
-38403634
-1621182332
-1575015857
-1575015857
682552505
682552505
...

result:

wrong answer 7th lines differ - expected: '874061694', found: '867109293'