QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#379707 | #8567. Cheap Construction | ucup-team3113# | WA | 217ms | 3780kb | C++20 | 2.4kb | 2024-04-06 18:24:25 | 2024-04-06 18:24:25 |
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));
set<int>S;
for(int i = 1; i<= n; i++)S.insert(i);
for(auto[x,y]:ins){
int id = x + ft.qer(x);
A[*S.lb(id)] = y;
S.erase(S.lb(id));
ft.upd(x, 1);
}
/*
for(auto[x, y] : ins){
A[x+ft.qer(x)] = y;
ft.upd(x, 1);
}
set<int>S;
for(int i = 1; i<= n; i++)S.insert(i);
for(auto[x,y]:ins){
A[*S.lb(x)]=y;
S.erase(S.lb(x));
}*/
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: 1ms
memory: 3780kb
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: 0ms
memory: 3540kb
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: 217ms
memory: 3636kb
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 65 3 86 5 68 5 301 5 459 8 81 8 102 8 258 8 311 8 498 12 180 13 474 14 482 17 6 17 6 17 6 17 9 17 15 17 15 17 108 19 48 19 57 19 197 19 229 21 227 22 252 22 263 23 267 23 368 23 393 25 382 29 66 30 168 30 375 30 432 33 242 35 55 35 58 36 90 36 209 36 275 36 400 39 265 40 394 40 4...
result:
wrong answer 9th lines differ - expected: '5 376', found: '5 459'