QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#402099 | #8553. Exchanging Kubic | 2745518585 | WA | 27ms | 10244kb | C++20 | 3.4kb | 2024-04-29 21:26:28 | 2024-04-29 21:26:29 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1000001;
int n,q,fa[N];
ll a[N];
bool h[N];
vector<int> d[N];
struct
{
int l,r;
}b[N];
struct str
{
int x;
ll w;
str() {}
str(int x,ll w):x(x),w(w) {}
friend bool operator<(str a,str b)
{
return a.w>b.w;
}
};
ll query(int l,int r)
{
printf("? %d %d\n",l,r);
fflush(stdout);
ll x;
scanf("%lld",&x);
return x;
}
int find(int x)
{
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void dfs(int x)
{
// printf(" %d %d %lld\n",b[x].l,b[x].r,a[x]);
ll s=0;
int p=0;
for(auto i:d[x])
{
s+=a[i];
if(a[i]<=0) p=i;
}
if(s>a[x]) a[p]-=s-a[x];
for(auto i:d[x]) dfs(i);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
a[i]=query(i,i);
b[i].l=b[i].r=i;
}
for(int i=1;i<=n*2+1;++i) fa[i]=i;
fa[0]=fa[n+1]=0;
b[0].l=1,b[0].r=-1;
q=n+1;
for(int i=1;i<=n-1;++i)
{
if(!((a[i]>0)^(a[i+1]>0)))
{
int x1=find(i),x2=find(i+1);
++q;
a[q]=a[x1]+a[x2];
b[q].l=b[x1].l,b[q].r=b[x2].r;
d[q]={x1,x2};
fa[x1]=fa[x2]=q;
}
}
priority_queue<str> Q;
for(int i=1;i<=q;++i)
{
if(find(i)==i&&a[i]>0) Q.push(str(i,a[i]));
}
while(!Q.empty())
{
// for(int i=1;i<=q;++i)
// {
// if(find(i)==i) printf("%d %d %d\n",b[i].l,b[i].r,a[i]);
// }
// printf("\n");
int x=Q.top().x;
Q.pop();
if(x!=find(x)) continue;
// printf(" %d %d %lld\n",b[x].l,b[x].r,a[x]);
auto check=[&](int x1,int x2,int x3)
{
ll z=query(b[x1].l,b[x3].r);
if(z!=a[x1]&&z!=a[x3])
{
a[x2]=z-a[x1]-a[x3];
++q;
a[q]=z;
b[q].l=b[x1].l,b[q].r=b[x3].r;
fa[x1]=fa[x2]=fa[x3]=q;
d[q]={x1,x2,x3};
Q.push(str(q,a[q]));
return true;
}
a[x2]=min(a[x2],-min(a[x1],a[x3]));
return false;
};
int x2=find(b[x].l-1),x1=find(b[x2].l-1),x3=find(b[x].r+1),x4=find(b[x3].r+1);
if(x1)
{
if(check(x1,x2,x)) continue;
}
if(x4)
{
if(check(x,x3,x4)) continue;
}
if(x2&&x3)
{
++q;
a[q]=a[x2]+a[x]+a[x3];
b[q].l=b[x2].l;
b[q].r=b[x3].r;
fa[x]=fa[x2]=fa[x3]=q;
d[q]={x2,x,x3};
}
}
for(int i=1;i<=q;++i)
{
if(find(i)==i) dfs(i);
}
printf("! ");
for(int i=1;i<=n;++i)
{
printf("%d ",a[i]);
}
printf("\n");
fflush(stdout);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 10244kb
input:
2 3 1 0 1 1 1 5 2 0 3 0 5 4 5 5
output:
? 1 1 ? 2 2 ? 3 3 ? 1 3 ? 1 3 ! 1 -1 1 ? 1 1 ? 2 2 ? 3 3 ? 4 4 ? 5 5 ? 1 3 ? 1 5 ? 1 5 ! 2 -1 3 -4 5
result:
ok ok (2 test cases)
Test #2:
score: 0
Accepted
time: 27ms
memory: 9916kb
input:
10000 1 718876398 1 0 1 0 1 0 1 938356223 1 857157125 1 0 1 0 1 0 1 0 1 0 1 965894497 1 0 1 626061677 1 642094813 1 107398046 1 0 1 0 1 0 1 287188651 1 0 1 345108193 1 0 1 0 1 714952783 1 0 1 325760098 1 0 1 800487422 1 322806679 1 0 1 0 1 0 1 866952779 1 741483102 1 492063609 1 0 1 833448280 1 0 1 ...
output:
? 1 1 ! 718876398 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 938356223 ? 1 1 ! 857157125 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 965894497 ? 1 1 ! 0 ? 1 1 ! 626061677 ? 1 1 ! 642094813 ? 1 1 ! 107398046 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 0 ? 1 1 ! 287188651 ? 1 1 ! 0 ? 1 1...
result:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 16ms
memory: 9956kb
input:
1052 9 167536100 0 373372185 0 427949326 442758705 102715118 0 0 373372185 373372185 973423149 973423149 9 248442934 306962195 570791475 593033322 0 582850731 559429390 0 120053133 1142280121 2780526396 2780526396 10 785691778 0 981032824 0 0 592503870 0 0 0 0 1112480382 1112480382 1112480382 10 0 7...
output:
? 1 1 ? 2 2 ? 3 3 ? 4 4 ? 5 5 ? 6 6 ? 7 7 ? 8 8 ? 9 9 ? 1 3 ? 1 3 ? 3 7 ? 1 7 ! 167536100 -167536100 373372185 -373372185 427949326 442758705 102715118 0 0 ? 1 1 ? 2 2 ? 3 3 ? 4 4 ? 5 5 ? 6 6 ? 7 7 ? 8 8 ? 9 9 ? 6 9 ? 1 7 ? 1 9 ! 248442934 306962195 570791475 593033322 -80983651 582850731 559429390...
result:
ok ok (1052 test cases)
Test #4:
score: -100
Wrong Answer
time: 17ms
memory: 4116kb
input:
105 98 0 622130364 0 603542943 491665548 0 535594695 169182905 269002770 437838930 534070706 783210752 0 914335037 560159875 0 216552904 666995724 0 0 0 753649796 0 0 0 779352417 0 121063647 0 496743470 0 4104890 0 648149367 0 965790695 732089017 0 0 0 0 6701195 0 0 0 0 750231085 0 0 511641740 36964...
output:
? 1 1 ? 2 2 ? 3 3 ? 4 4 ? 5 5 ? 6 6 ? 7 7 ? 8 8 ? 9 9 ? 10 10 ? 11 11 ? 12 12 ? 13 13 ? 14 14 ? 15 15 ? 16 16 ? 17 17 ? 18 18 ? 19 19 ? 20 20 ? 21 21 ? 22 22 ? 23 23 ? 24 24 ? 25 25 ? 26 26 ? 27 27 ? 28 28 ? 29 29 ? 30 30 ? 31 31 ? 32 32 ? 33 33 ? 34 34 ? 35 35 ? 36 36 ? 37 37 ? 38 38 ? 39 39 ? 40 4...
result:
wrong answer mss of [1, 46] is incorrect, expected=4696934508, found=4988377453 (test case 1)