QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#62064#3850. DJ Darko2pal1rakWA 434ms20964kbC++205.2kb2022-11-17 06:24:222022-11-17 06:24:23

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 06:24:23]
  • 评测
  • 测评结果:WA
  • 用时:434ms
  • 内存:20964kb
  • [2022-11-17 06:24:22]
  • 提交

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));
    assert(it != s.begin());
    if(it != s.begin()) {
        it--;
        if(it->l != 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);
            s.insert(i1);
            s.insert(i2);
        }
    }

    it = s.lower_bound(Interval(r + 1, -1, 0));
    assert(it != s.begin());
    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);
            s.insert(i1);
            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 = [](int64_t middle, const std::vector<std::pair<int64_t, int64_t>> &x) {
        int64_t 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));
            std::set<int64_t> vals;
            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) });
                vals.insert(it->val + prefSum);

                auto jt = it;
                it++;

                s.erase(jt);
            }

            std::sort(x.begin(), x.end());
            std::vector<int64_t> values;
            for(auto x: vals)   values.push_back(x);

            int32_t low = 0, high = values.size() - 1;
            while(low < high) {
                int32_t mid1 = low + (high - low + 1) / 3;
                int32_t mid2 = low + (2 * (high - low + 1)) / 3;

//                std::cout << mid1 << " " << check(values[mid1], x) << " " << check(values[mid2], x) << '\n';
                if(check(values[mid1], x) <= check(values[mid2], x)) {
                    high = mid2 - 1;
                }
                else {
                    low = mid1 + 1;
                }
            }

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

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

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 434ms
memory: 20964kb

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:

230957982
1222888235
1657880566
720769108
909438616
909438616
877744833
323743687
2161390
909438616
-733522664
-1087993830
1791340400
-1125444731
-293004555
-293004555
-293004555
1753889499
-293004555
1753889499
-1333832167
-544976731
-544976731
-1333832167
-2084836812
-351587238
-351587238
-1082727...

result:

wrong answer 1st lines differ - expected: '462406736', found: '230957982'