QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#62076 | #3850. DJ Darko | 2pal1rak | WA | 343ms | 19484kb | C++20 | 5.8kb | 2022-11-17 07:45:00 | 2022-11-17 07:45:03 |
Judging History
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 = [](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());
int64_t best_value = -1LL << 60;
int64_t cost = 1LL << 62;
int64_t rgt = 0, lft = 0, lftb = 0, rgtb = 0;
for(int i = 0; i < x.size(); i++) {
rgt += 1LL * x[i].first * x[i].second;
rgtb += x[i].second;
}
for(int i = 0; i < x.size(); i++) {
rgt -= 1LL * x[i].first * x[i].second;
rgtb -= x[i].second;
int64_t now_cost = (1LL * lftb * x[i].first) - lft + rgt - (1LL * rgtb * x[i].first);
if(now_cost < cost) {
cost = now_cost;
best_value = x[i].first;
}
lft += 1LL * x[i].first * x[i].second;
lftb += x[i].second;
}
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;
}
}
auto cst = check(low, x);
if(cst > cost) {
std::cout << "-1\n";
} else if(cst < cost) {
std::cout << "-2\n";
} else
std::cout << best_value << '\n';
s.insert(Interval(l, r - l + 1, best_value));
}
/**
for(auto &x : s) {
std::cout << " { " << x.l << ", " << x.len << ", " << x.val << " }, ";
}
std::cout << '\n';
*/
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 343ms
memory: 19484kb
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:
-1 -1 -1 -1 -1 308114278 -1 -1 -608408939 308114278 -1 -1 -1 -75900193 -75900193 -1 1346073367 1346073367 1346073367 1346073367 -321738530 -573710706 -573710706 -573710706 -2156489404 -1 -662228107 551224568 551224568 -1 551224568 292268055 551224568 -1 640352746 -1746577935 -1732459875 -1 243157577...
result:
wrong answer 1st lines differ - expected: '462406736', found: '-1'