QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#429503 | #8567. Cheap Construction | ucup-team2526# | WA | 179ms | 3840kb | C++20 | 2.9kb | 2024-06-02 16:12:44 | 2024-06-02 16:12:44 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) ((x)&-(x))
using namespace std;
#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)
void err() {
cout << endl;
}
template<class T, class... Ts>
void err(T arg, Ts ... args) {
cout << fixed << setprecision(10) << arg << ' ';
err(args...);
}
constexpr int Max=1e18;
void solve(){
int n;cin>>n;
vector<int>a(2*n+1);
for(int i=1;i<=2*n;i++)cin>>a[i];
vector<int>c(n+1);
{
vector<int>tr(4*n);
auto bulid = [&](auto self,int l,int r,int p)->void{
if(l==r){
tr[p]=1;
return ;
}
int mid=(l+r)>>1;
self(self,l,mid,p<<1);
self(self,mid+1,r,p<<1|1);
tr[p]=tr[p<<1]+tr[p<<1|1];
return ;
};
auto updata = [&](auto self,int l,int r,int x,int val,int p)->void{
if(l==r){
tr[p]=0;
return ;
}
int mid=(l+r)>>1;
if(x<=mid)self(self,l,mid,x,val,p<<1);
else self(self,mid+1,r,x,val,p<<1|1);
tr[p]=tr[p<<1]+tr[p<<1|1];
};
auto query = [&](auto self,int l,int r,int k,int p)->int{
if(l==r){
return l;
}
int mid=(l+r)>>1;
int num=tr[p<<1];
if(num>=k)return self(self,l,mid,k,p<<1);
else return self(self,mid+1,r,k-num,p<<1|1);
};
bulid(bulid,1,n,1);
for(int i=2*n;i>=1;i-=2){
int p=a[i-1];
int h=a[i];
int ps=query(query,1,n,p,1);
c[ps]=h;
updata(updata,1,n,ps,0,1);
}
}
vector<pair<int,int>>tr(4*n);
auto up = [&](pair<int,int> x,pair<int,int> y)->pair<int,int>{
pair<int,int>res;
if(x.first<y.first){
res=x;
}else if(y.first<x.first){
res=y;
}else{
res.first=x.first;
res.second=max(x.second,y.second);
}
return res;
};
auto bulid = [&](auto self,int l,int r,int p)->void{
if(l==r){
tr[p]={c[l],l};
return ;
}
int mid=(l+r)>>1;
self(self,l,mid,p<<1);
self(self,mid+1,r,p<<1|1);
tr[p]=up(tr[p<<1],tr[p<<1|1]);
return ;
};
auto query = [&](auto self,int l,int r,int x,int y,int p)->pair<int,int>{
if(x<=l&&r<=y){
return tr[p];
}
int mid=(l+r)>>1;
pair<int,int> res={Max,Max};
if(x<=mid)res=self(self,l,mid,x,y,p<<1);
else{
res=up(res,self(self,mid+1,r,x,y,p<<1|1));
}
return res;
};
bulid(bulid,1,n,1);
vector<int>f(n+1);
auto Set = [&](int x)->void{
while(x<=n){
f[x]++;
x+=lowbit(x);
}
return ;
};
auto ask = [&](int x)->int{
int res=0;
while(x){
res+=f[x];
x-=lowbit(x);
}
return res;
};
auto dfs = [&](auto &&self, int l, int r)->void{
if(l > r) return ;
int p = query(query,1,n,l,r,1).second;
//dbg(l,r,p);
Set(p);
std::cout << std::format("{} {}\n", ask(p-1) + 1, c[p]);
if(p != l) self(self, l, p - 1);
if(p != r) self(self, p + 1, r);
};
dfs(dfs, 1, n);
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;cin>>t;
while(t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3700kb
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: 3700kb
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: 179ms
memory: 3840kb
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 301 5 376 5 459 8 498 9 68 10 258 11 102 12 180 13 474 14 482 15 81 17 15 17 15 18 229 19 108 20 197 21 466 23 252 23 393 25 57 25 227 25 382 27 267 29 66 30 252 31 263 31 432 33 48 33 58 33 191 33 375 35 242 36 400 37 168 37 275 39 393 41 368 43 394 44 454 45 55 45 90 ...
result:
wrong answer 7th lines differ - expected: '5 68', found: '5 301'