QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#662012 | #6303. Inversion | libantian | WA | 1ms | 3620kb | C++23 | 3.0kb | 2024-10-20 20:04:08 | 2024-10-20 20:04:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define fi first
#define se second
#define all(_a) _a.begin(), _a.end()
const int N=20010;
int a[N],tr[N];
int n;
map<pii,int>mp;
int query(int l,int r){
cout<<"? "<<l<<" "<<r<<endl;
cout.flush();
int x;
cin>>x;
return x;
//return x&1;
}
int lowbit(int x){
return x&-x;
}
void init(int len){
for(int i=1;i<=len;i++)tr[i]=0;
}
void add(int u,int len){
for(int i=u;i<=len;i+=lowbit(i))tr[i]++;
}
int get(int u){
int res=0;
for(int i=u;i;i-=lowbit(i))res+=tr[i];
return res;
}
int get(int l,int r){
int len=r-l+1;
init(len);
int res=0;
for(int i=l;i<=r;i++){
res+=get(len-a[i]+1);
add(len-a[i]+1,len);
}
return res&1;
}
void build(int l,int r){
// cout<<l<<" "<<r<<endl;
if(l==r){
a[l]=1;
return ;
}
int mid=(l+r)/2;
build(l,mid);
build(mid+1,r);
vector<int> stk,res;
stk.push_back(n+1);
map<int,int>pos,stt;
for(int i=mid;i>=l;i--){
while(a[i]<a[stk.back()])stk.pop_back();
pos[i]=stk.back();
stk.push_back(i);
}
priority_queue<pii>ql,qr;
for(int i=l;i<=mid;i++){
ql.push({a[i],i});
}
for(int i=mid+1;i<=r;i++){
qr.push({a[i],i});
}
while(qr.size()){
if(ql.size()==0){
res.push_back(qr.top().se);
qr.pop();
continue;
}
int st=ql.top().se;
int ed=pos[st];
int now=qr.top().se;
if(ed=n+1)ed=mid;
else ed--;
if(ed==n+1){
ed=mid;
int t;
if(mp.find({st,ed})==mp.end()){
mp[{st,ed}]=get(st,ed);
}
t=mp[{st,ed}];
if((query(st,now)^t)==query(mid+1,now)){
res.push_back(st);
ql.pop();
}else{
res.push_back(now);
qr.pop();
}
}else{
int t;
if(mp.find({st,ed-1})==mp.end()){
mp[{st,ed-1}]=get(st,ed-1);
}
t=mp[{st,ed-1}];
if((query(st,now)^1)==query(ed,now)){
res.push_back(st);
ql.pop();
}else{
res.push_back(now);
qr.pop();
}
}
}
while(ql.size()){
res.push_back(ql.top().se);
ql.pop();
}
for(int i=0;i<res.size();i++){
a[res[i]]=res.size()-i;
}
}
void solve(){
cin>>n;
a[n+1]=-INF;
build(1,n);
cout<<"! ";
for(int i=1;i<n;i++){
cout<<n-a[i]+1<<" ";
}
cout<<n-a[n]+1;
}
signed main(){
fflush(stdout);
cout.flush();
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T;
T=1;
// cin>>T;
while(T--)solve();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3620kb
input:
3 0 0 1 1
output:
? 1 2 ? 1 2 ? 2 3 ? 2 3 ! 3 2 1
result:
wrong answer Wa.