QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#444173 | #8522. Waterfall Matrix | ucup-team266# | TL | 2ms | 16632kb | C++17 | 3.5kb | 2024-06-15 17:36:48 | 2024-06-15 17:36:49 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
class node{
public:
int x, y, c;
inline node() {}
inline node(int x0, int y0, int c0){
x = x0, y = y0, c = c0;
}
};
class section{
public:
int l, r;
inline section() {}
inline section(int l0, int r0){
l = l0, r = r0;
}
};
inline bool operator < (node u, node v){
if(u.x != v.x) return u.x < v.x;
else return u.y < v.y;
}
vector<node> mat[N] = {};
int sum[N << 1] = {}, pre[N << 1] = {};
vector<section> opt[N] = {};
inline void modify(int n, int p, int x){
p += n, sum[p] += x, pre[p] = max(sum[p], 0);
while(p > 1){
p >>= 1;
sum[p] = sum[p << 1] + sum[p << 1 | 1], pre[p] = max(pre[p << 1], sum[p << 1] + pre[p << 1 | 1]);
}
}
inline int find(int n, int p, int cur){
if(p > n) return p - n;
else{
if(cur + pre[p << 1] >= 0) return find(n, p << 1, cur);
else return find(n, p << 1 | 1, cur + sum[p << 1]);
}
}
inline int qry(int n, int p){
if(sum[p]){
int ret = sum[p];
if(p < n) qry(n, p << 1), qry(n, p << 1 | 1);
else modify(n, p - n, -ret);
return ret;
}
else return 0;
}
inline int query(int n, int l, int r){
int ret = 0;
for(l += n, r += n ; l < r ; l >>= 1, r >>= 1){
if(l & 1) ret += qry(n, l ++);
if(r & 1) ret += qry(n, -- r);
}
return ret;
}
inline void inc(int n, int i, int l){
int cur = sum[n + l], r = -1;
for(int p = l + 1 + n, q = n + n ; p < q ; p >>= 1, q >>= 1) if(p & 1){
if(pre[p] + cur >= 0){
r = find(n, p, cur);
break;
}
else cur += sum[p];
p ++;
}
if(r == -1){
int fuck = 0;
while(1) fuck = 1;
cout << fuck;
}
int x = query(n, l, r + 1); modify(n, r, x);
opt[i].push_back(section(l, r));
}
inline void split(vector<node> w, vector<node> &u, vector<node> &v, int n, int m, int k){
for(int i = 0 ; i < n ; i ++) mat[i].clear(), opt[i].clear(); for(int i = 1 ; i < 2 * m ; i ++) sum[i] = pre[i] = 0;
for(node p : w) mat[p.x].push_back(p);
modify(m, m - 1, n);
for(int i = 0 ; i < n ; i ++){
for(node p : mat[i]){
int j = p.y, c = (p.c <= k) ? 1 : -1;
modify(m, j, c);
}
for(node p : mat[i]){
int j = p.y;
if(sum[m + j] < 0) inc(m, i, j);
}
}
int j = m - 1;
for(int i = n - 1 ; i >= 0 ; i --){
for(section sec : opt[i]) if(j >= sec.l && j <= sec.r) j = sec.l;
for(node p : mat[i]){
if(p.y < j) u.push_back(p);
else v.push_back(p);
}
}
sort(u.begin(), u.end()), sort(v.begin(), v.end());
}
inline int len(int n){
int m = 1;
while(m <= n) m <<= 1;
return m;
}
inline long long solve(vector<node> w, int l, int r){
if(w.size() == 0) return 0;
if(l == r){
long long ans = 0;
for(node p : w) ans += abs(p.c - l);
return ans;
}
vector<node> u, v;
vector<int> a, b;
int o = (l + r) / 2;
for(node p : w) a.push_back(p.x), b.push_back(p.y);
sort(a.begin(), a.end()), sort(b.begin(), b.end());
a = vector<int>(a.begin(), unique(a.begin(), a.end())), b = vector<int>(b.begin(), unique(b.begin(), b.end()));
for(node &p : w) p.x = lower_bound(a.begin(), a.end(), p.x) - a.begin(), p.y = lower_bound(b.begin(), b.end(), p.y) - b.begin();
split(w, u, v, a.size(), len(b.size()), o);
return solve(u, l, o) + solve(v, o + 1, r);
}
int main(){
int n = 0;
vector<node> v;
scanf("%d", &n);
for(int i = 0, a = 0, b = 0, c = 0 ; i < n ; i ++){
scanf("%d %d %d", &a, &b, &c);
v.push_back(node(a, -b, c));
}
sort(v.begin(), v.end());
printf("%lld\n", solve(v, 1, 1000000000));
return 0;
}
/*
4
3 2 1
3 3 3
4 4 1
3 5 4
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 16336kb
input:
5 1 3 5 3 2 1 3 3 3 4 4 1 3 5 4
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 16324kb
input:
1 1 1 58383964
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 16268kb
input:
2 1 1 204909414 2 2 807091086
output:
602181672
result:
ok 1 number(s): "602181672"
Test #4:
score: 0
Accepted
time: 0ms
memory: 16632kb
input:
4 2 4 107744934 3 2 143843719 4 4 908851567 2 2 307161330
output:
801106633
result:
ok 1 number(s): "801106633"
Test #5:
score: 0
Accepted
time: 2ms
memory: 16632kb
input:
7 1 2 140766572 5 3 795389698 3 6 279722536 2 6 442413348 4 2 475716910 5 4 704493410 5 2 423494885
output:
935621651
result:
ok 1 number(s): "935621651"
Test #6:
score: -100
Time Limit Exceeded
input:
11 1 5 104443431 4 9 692130290 4 1 18812720 7 3 381505729 7 7 706418558 11 9 242808397 10 4 490383412 3 2 72839501 10 7 883914647 10 6 738589165 11 10 556539693