QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380630 | #8567. Cheap Construction | ucup-team1766# | WA | 391ms | 50456kb | C++23 | 2.8kb | 2024-04-07 06:58:36 | 2024-04-07 06:58:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
struct Node {
Node *l = 0, *r = 0;
int lo, hi, mset = inf, madd = 0, val = -inf;
Node(int loo, int hii): lo(loo),hi(hii){}
Node (vector<int>& v, int loo, int hii):lo(loo), hi(hii) {
if (lo+1 < hi) {
int mid = lo+(hi-lo)/2;
l = new Node(v, lo, mid); r = new Node(v, mid, hi);
val = max(l->val, r->val);
}
else val = v[lo];
}
int query(int L, int R) {
if (R <= lo || hi <= L) return -inf;
if (L <= lo and hi <= R) return val;
push();
return max(l->query(L,R),r->query(L,R));
}
void set(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo and hi <= R) mset = val = x, madd = 0;
else {
push(), l->set(L, R, x), r->set(L,R,x);
val = max(l->val, r->val);
}
}
void add(int L, int R, int x) {
if (R <= lo || hi <= L) return;
if (L <= lo and hi <= R) {
if (mset != inf) mset += x;
else madd += x;
val += x;
}
else {
push(), l->add(L,R,x), r->add(L,R,x);
val = max(l->val, r->val);
}
}
void push() {
if (!l) {
int mid = lo + (hi - lo)/2;
l = new Node(lo, mid); r = new Node(mid ,hi);
}
if (mset != inf)
l->set(lo,hi,mset), r->set(lo,hi,mset),mset=inf;
else if (madd)
l->add(lo,hi,madd),r->add(lo,hi,madd), madd=0;
}
};
template<class T>
struct RMQ {
vector<vector<T>> jmp;
RMQ(const vector<T>& V) : jmp(1,V) {
for (int pw=1, k=1; pw*2 <= V.size(); pw *= 2, ++k) {
jmp.emplace_back(V.size()-pw*2+1);
for (int j = 0; j < jmp[k].size(); j++)
jmp[k][j] = min(jmp[k-1][j],jmp[k-1][j+pw]);
}
}
T query(int a, int b) {
assert(a<b);
int dep = 31-__builtin_clz(b-a);
return min(jmp[dep][a],jmp[dep][b-(1<<dep)]);
}
};
void build(int l, int r, vector<pair<int,int>>& seq, RMQ<pair<int,int>>& rmq, bool left) {
if (l == r) return;
if (!left) {
int i = -rmq.query(l,r).second;
cout << "1 " << seq[i].first << '\n';
build(0,i,seq,rmq,false);
build(i+1,r,seq,rmq,true);
return;
}
for (int i = l; i < r; i++) {
cout << i+1 << ' ' << seq[i].first << '\n';
}
}
void run() {
int n; cin >> n;
vector<pair<int,int>> seq(n); for (auto &[p,h] : seq) cin >> p >> h, p--;
vector<int> _(n); iota(_.begin(),_.end(),0);
Node *tr = new Node(_, 0, n);
auto find = [&](int p) {
int l = 0, r = n;
while (l+2 < r) {
int m = (l+r)/2;
int x = tr->query(m,m+1);
if (x == p) {
r = m+1;
} else if (x > p) r = m;
else l = m+1;
}
if (tr->query(l,l+1) == p) return l;
assert(tr->query(l+1,l+2) == p);
return l+1;
};
reverse(seq.begin(),seq.end());
vector<pair<int,int>> act(n);
for (auto &[p,h] : seq) {
int x = find(p);
act[x].first = h;
tr->add(x,n,-1);
}
for (int i = 0; i< n; i++) {
act[i].second = -i;
}
RMQ rmq(act);
build(0, n, act,rmq,false);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t; while (t--) run();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3640kb
input:
1 3 1 1 2 2 1 3
output:
1 1 1 3 3 2
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
2 3 1 1 1 2 3 1 3 1 3 2 1 3 3
output:
1 1 1 1 1 2 1 1 1 3 3 3
result:
ok 6 lines
Test #3:
score: -100
Wrong Answer
time: 391ms
memory: 50456kb
input:
1000 500 1 25 2 115 2 356 4 396 5 417 3 416 1 376 8 302 5 475 8 134 5 470 2 191 9 443 9 483 7 311 6 415 14 422 6 288 9 411 9 318 18 406 20 213 16 292 8 351 8 150 20 199 3 311 22 321 22 221 16 364 7 316 17 79 23 160 23 369 6 209 36 9 35 490 2 498 30 391 31 175 10 322 16 484 4 63 44 304 39 300 13 309 ...
output:
1 2 1 2 1 40 1 68 3 86 4 65 5 459 6 376 7 301 8 498 9 68 10 258 11 102 12 180 13 474 14 482 15 81 17 15 18 229 19 108 20 197 21 466 22 15 23 393 24 252 25 382 26 227 27 267 28 57 29 66 30 252 31 432 32 263 33 375 34 191 35 242 36 400 37 275 38 168 39 393 40 58 41 368 42 48 43 394 44 454 45 90 46 55 ...
result:
wrong answer 5th lines differ - expected: '3 65', found: '3 86'