QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#453214 | #8718. 保区间最小值一次回归问题 | propane | WA | 509ms | 25260kb | C++20 | 5.5kb | 2024-06-23 21:34:59 | 2024-06-23 21:35:00 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<numeric>
#include<map>
#include<functional>
using namespace std;
using LL = long long;
struct DSU{
vector<int> p;
DSU(int n) : p(n + 1){
iota(p.begin(), p.end(), 0);
}
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool merge(int x, int y){
x = find(x), y = find(y);
if (x == y) return false;
p[y] = x;
return true;
}
};
const LL INF = 1e18;
struct Info {
LL mn = INF;
};
Info operator+(const Info &a, const Info &b){
return {min(a.mn, b.mn)};
}
using Tag = int;
void apply(Info &x, Tag &a, Tag f){
if (f){
a = 1;
x.mn = INF;
}
}
template<class Info, class Tag>
struct LazySegmentTree{
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() {}
LazySegmentTree(int n, Info _init = Info()){
init(vector<Info>(n, _init));
}
LazySegmentTree(const vector<Info> &_init){
init(_init);
}
void init(const vector<Info> &_init){
n = (int)_init.size();
info.assign((n << 2) + 1, Info());
tag.assign((n << 2) + 1, Tag());
function<void(int, int, int)> build = [&](int p, int l, int r){
if (l == r){
info[p] = _init[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m + 1, r);
pull(p);
};
build(1, 0, n - 1);
}
void pull(int p){
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v){
::apply(info[p], tag[p], v);
}
void push(int p){
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v){
if (l == r){
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x <= m){
modify(2 * p, l, m, x, v);
}
else{
modify(2 * p + 1, m + 1, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v){
modify(1, 0, n - 1, p, v);
}
Info query(int p, int l, int r, int x, int y){
if (l > y || r < x){
return Info();
}
if (l >= x && r <= y){
return info[p];
}
int m = (l + r) / 2;
push(p);
return query(2 * p, l, m, x, y) + query(2 * p + 1, m + 1, r, x, y);
}
Info query(int l, int r){
return query(1, 0, n - 1, l, r);
}
void modify(int p, int l, int r, int x, int y, const Tag &v){
if (l > y || r < x){
return;
}
if (l >= x && r <= y){
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
modify(2 * p, l, m, x, y, v);
modify(2 * p + 1, m + 1, r, x, y, v);
pull(p);
}
void modify(int l, int r, const Tag &v){
return modify(1, 0, n - 1, l, r, v);
}
};
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
map<int, vector<pair<int, int> > > mp;
for(int i = 0; i < m; i++){
int l, r, v;
cin >> l >> r >> v;
mp[-v].push_back({l, r});
}
DSU dsu(n + 1);
bool ok = true;
LL ans = 0;
for(auto &[x, v] : mp){
vector<int> p;
for(auto [l, r] : v){
for(int i = dsu.find(l); i <= r; i = dsu.find(i)){
p.push_back(i);
dsu.p[i] = i + 1;
}
}
sort(p.begin(), p.end());
const int m = p.size();
vector<int> cost(m + 1);
for(int i = 0; i < m; i++){
if (a[p[i]] > -x){
cost[i + 1] = a[p[i]] + x;
}
else{
ans += -x - a[p[i]];
}
}
auto get = [&](int x){
return int(lower_bound(p.begin(), p.end(), x) - p.begin()) + 1;
};
vector<int> pos(m + 1, 1e9);
for(auto [l, r] : v){
l = get(l);
r = get(r + 1) - 1;
if (l > r){
ok = false;
break;
}
pos[r] = min(pos[r], l);
}
if (!ok) break;
LazySegmentTree<Info, Tag> seg(m + 1);
seg.modify(0, {0});
for(int i = 1; i <= m; i++){
LL dp = seg.query(0, m).mn + cost[i];
seg.modify(i, {dp});
if (pos[i] <= i){
seg.modify(0, pos[i] - 1, 1);
}
}
ans += seg.query(0, m).mn;
}
if (!ok){
cout << -1 << '\n';
continue;
}
cout << ans << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3572kb
input:
1 3 2 2023 40 41 1 1 2022 2 3 39
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: -100
Wrong Answer
time: 509ms
memory: 25260kb
input:
1000 100 100 1 35141686 84105222 84105220 7273527 178494861 178494861 112519027 77833654 77833656 261586535 278472336 278472336 261586536 416361017 416361017 426649080 323519513 278472337 420127821 420127823 420127823 482516531 434108818 420127821 631535744 615930922 546346921 546346920 546346920 70...
output:
48 45 42 38 48 46 47 45 46 46 46 55 944 53 49 55 46 61 57 46 47 49 42 54 51 57 50 43 41 43 41 52 57 44 57 45 48 43 37 48 42 52 45 49 894 941 47 50 49 49 44 42 54 51 42 46 53 52 51 47 38 47 41 40 53 41 47 47 37 51 53 45 43 47 46 47 43 48 41 44 45 35 43 45 52 45 48 44 41 43 46 39 48 54 44 43 42 48 46 ...
result:
wrong answer 1st numbers differ - expected: '49', found: '48'