QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379553 | #8567. Cheap Construction | ucup-team3113# | WA | 131ms | 3908kb | C++20 | 2.1kb | 2024-04-06 17:50:50 | 2024-04-06 17:50:51 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define int int64_t
#define pii pair<int, int>
#define X first
#define Y second
#define all(x) (x).begin(),(x).end()
#define lb lower_bound
#define ub upper_bound
using namespace std;
const int inf = 1e18;
void p(auto A){
for(auto e : A)cout << e << ' ';
cout << '\n';
}
struct fenwick{
vector<int>A;
fenwick(int n){
A.assign(n+5, 0);
}
void upd(int k, int v){
k++;
for(;k<A.size(); k+=k&-k)A[k]+=v;
}
int qer(int k){
k++;
int ret = 0;
for(;k>0;k-=k&-k)ret+=A[k];
return ret;
}
};
struct segtree{
vector<pii>A;
segtree(int n){
A.assign(4*n, {0, 0});
}
pii conq(pii a, pii b){
if(a.X < b.X)return a;
else return b;
}
void upd(int v, int tl, int tr, int pos, int val){
if(tl > pos || tr < pos)return;
if(tl == pos && tr == pos){
A[v] = {val, pos};
return;
}
int tm = (tl+tr)/2;
upd(v*2, tl, tm, pos, val);
upd(v*2+1, tm+1, tr, pos, val);
A[v] = conq(A[v*2], A[v*2+1]);
}
pii qer(int v, int tl, int tr, int l, int r){
if(tl > r || tr < l)return{inf, -1};
if(tl >= l && tr <= r)return A[v];
int tm = (tl+tr)/2;
pii a1 = qer(v*2, tl, tm, l, r);
pii a2 = qer(v*2+1, tm+1, tr, l, r);
return conq(a1, a2);
}
};
void solve(){
int n; cin >> n;
vector<int>A(n+1);
fenwick ft(n);
vector<pii>ins(n);
for(int i = 0; i< n; i++)cin >> ins[i].X >> ins[i].Y;
reverse(all(ins));
for(auto[x, y] : ins){
A[x+ft.qer(x)] = y;
ft.upd(x, 1);
}
segtree seg(n+1);
for(int i = 1; i<= n; i++)seg.upd(1, 0, n, i, A[i]);
vector<pii> ans;
//cerr << "YES" << endl;
auto rec = [&](auto&& self, int l, int r, int id)->void{
if(r < l)return;
pii cur = seg.qer(1, 0, n, l, r);
ans.pb({id, cur.X});
self(self, l, cur.Y-1, id);
self(self, cur.Y+1, r, cur.Y+1);
};
rec(rec, 1, n, 1);
for(auto[x, y] : ans)cout << x << ' ' << y << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while(t--)solve();
}
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
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: 3604kb
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: 131ms
memory: 3908kb
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 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 ...
result:
wrong answer 1st lines differ - expected: '1 2', found: '1 0'