QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#621442 | #8553. Exchanging Kubic | ucup-team3651 | TL | 0ms | 0kb | C++20 | 3.7kb | 2024-10-08 14:34:46 | 2024-10-08 14:34:53 |
answer
#include<bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define N 5005
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void write(ll x){
if(x<0)x=-x,putchar('-');
if(x/10)write(x/10);
putchar(x%10+'0');
}
struct Node{
ll l,r,val;
Node(ll l=0,ll r=0,ll val=0):l(l),r(r),val(val){}
};
vector<Node> S;
ll res[N];
void solve(){
ll n;cin>>n;S.clear();
for(ll i=1;i<=n;i++)res[i]=0;
for(ll i=1;i<=n;i++){
cout<<'?'<<' '<<i<<' '<<i<<endl;
res[i]=read();S.pb(Node(i,i,res[i]));
}
for(ll i=0;i<S.size();i++){
while(i+1<S.size() && (S[i+1].val<=0)==(S[i].val<=0)){
S[i].r=S[i+1].r;S[i].val+=S[i+1].val;
S.erase(S.begin()+i+1);
}
}
while(S.size()!=1){
if(S.back().val==0)S.pop_back();
else if(S[0].val==0)S.erase(S.begin());
else{
pi minn=mp(inf,0);
for(ll i=0;i<S.size();i+=2){
(minn>mp(S[i].val,i))?minn=mp(S[i].val,S[i].l):minn;
}
if(minn.se==0){
cout<<'?'<<' '<<S[0].l<<' '<<S[2].r<<endl;
ll now;cin>>now;
if(now!=S[0].val && now!=S[2].val){
res[S[1].l]=now-S[0].val-S[2].val;
ll L=S[0].l,R=S[2].r;S.erase(S.begin());S.erase(S.begin());
S[0].l=L;S[0].val=now;
}
else{
res[S[1].l]=-S[0].val;S.erase(S.begin());S.erase(S.begin());
}
}
else if(minn.se==S.size()-1){
ll las=S.size()-1;
cout<<'?'<<' '<<S[las-2].l<<' '<<S[las].r<<endl;
ll now;cin>>now;
if(now!=S[las-2].val && now!=S[las].val){
res[S[las-1].l]=now-S[las-2].val-S[las].val;
ll L=S[las-2].l,R=S[las].r;S.pop_back();S.pop_back();
S[S.size()-1].r=R;S[S.size()-1].val=now;
}
else{
res[S[las-1].l]=-S[las].val;S.pop_back();S.pop_back();
}
}
else{
ll pos=minn.se;
cout<<'?'<<' '<<S[pos-2].l<<' '<<S[pos].r<<endl;
ll now;cin>>now;
if(now!=S[pos-2].val && now!=S[pos].val){
res[S[pos-1].l]=now-S[pos].val-S[pos-2].val;
ll R=S[pos].r;S.erase(S.begin()+pos-1);S.erase(S.begin()+pos-1);
S[pos-2].val=now;S[pos-2].r=R;continue;
}
cout<<'?'<<' '<<S[pos].l<<' '<<S[pos+2].r<<endl;
if(now!=S[pos].val && now!=S[pos+2].val){
res[S[pos+1].l]=now-S[pos].val-S[pos+2].val;
ll R=S[pos+2].r;S.erase(S.begin()+pos+1);S.erase(S.begin()+pos+1);
S[pos].val=now;S[pos].r=R;continue;
}
res[S[pos+1].l]=-S[pos].val;ll R=S[pos+1].r;
S.erase(S.begin()+pos);S.erase(S.begin()+pos);
S[pos-1].r=R;
}
}
}
cout<<'!'<<' ';
for(ll i=1;i<=n;i++)cout<<res[i]<<' ';
cout<<endl;
}
int main(){
ll T;cin>>T;while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
2 3 1 0 1
output:
? 1 1 ? 2 2 ? 3 3 ? 0 2