调了好久这道题目,尝试了把输出缓冲清除在各个地方进行加入,都不太对
在自己的编译器自己输入数据是可以得到正确的答案
但是上交到cf上就错了test1
如果有大佬能帮忙看一下的话万分感谢
题目链接

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 10010;
int ans[N];
int query(int l, int r)
{
int res;
cout << "? " << l << ' ' << r << ' ' << '\n';
cout.flush();
cin >> res;
return res;
}
void solve()
{
memset(ans, 0, sizeof ans);
int n;
cin >> n;
int t = query(1, n);
if (t == 0)
{
cout << "! IMPOSSIBLE" << '\n';
cout.flush();
return;
}
int cnt = 0, pre = 0; // pre记录先前的一次query得到的值
for (int i = n - 1; i >= 2; i -- )
{
int num = query(i, n);
int tmp = cnt;
int tt = num; // 先存起来两个量,防止被覆盖
num -= pre; // 减去先前的量
pre = tt; // 更新
if (!tmp) // 如果是从后往前第一次出现不为零的num, 这一位为0,后面num位为1
{
cnt = num - tmp;
for (int j = i + 1, num = num - tmp; num > 0; j ++ , num -- )
ans[j] = 1;
}
else if (num == cnt) ans[i] = 0; // 如果更新后的num 恰好为前面1的数量,这一位为1
else ans[i] = 1, cnt ++ ; // 否则为0,cnt更新
}
// 最后一位因为1,n 是开始的时候输入的,所以相当于全部再来一遍,和上面的区别就是把i换成1
int num = t;
int tmp = cnt;
int tt = num;
num -= pre;
pre = tt;
if (!tmp)
{
cnt += num - tmp;
for (int j = 1 + 1, num = num - tmp; num > 0; j ++ , num -- )
ans[j] = 1;
}
else if (num == cnt) ans[1] = 0;
else ans[1] = 1; cnt ++ ;
cout << "! ";
cout.flush();
for (int i = 1; i <= n; i ++ )
{
cout << ans[i];
cout.flush();
}
cout << '\n';
cout.flush();
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T -- )
{
solve();
}
}