QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#512870 | #8819. CNOI Knowledge | PetroTarnavskyi# | AC ✓ | 103ms | 3964kb | C++20 | 3.3kb | 2024-08-10 16:08:32 | 2024-08-10 16:08:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int LOG = 12;
struct SparseTable
{
VI t[LOG];
void push_back(int v)
{
int i = SZ(t[0]);
t[0].PB(v);
FOR (j, 0, LOG - 1)
t[j + 1].PB(min(t[j][i], t[j][max(0, i - (1 << j))]));
}
int query(int l, int r)
{
assert(l < r && r <= SZ(t[0]));
int i = 31 - __builtin_clz(r - l);
return min(t[i][r - 1], t[i][l + (1 << i) - 1]);
}
};
void countSort(VI& p, const VI& c)
{
int n = SZ(p);
VI cnt(n);
FOR (i, 0, n)
cnt[c[i]]++;
VI pos(n);
FOR (i, 1, n)
pos[i] = pos[i - 1] + cnt[i - 1];
VI p2(n);
for (auto x : p)
{
int i = c[x];
p2[pos[i]++] = x;
}
p = p2;
}
VI suffixArray(VI s)
{
s.PB(-1);
int n = SZ(s);
VI p(n), c(n);
iota(ALL(p), 0);
sort(ALL(p), [&](int i, int j)
{
return s[i] < s[j];
});
int x = 0;
c[p[0]] = 0;
FOR (i, 1, n)
{
if (s[p[i]] != s[p[i - 1]])
x++;
c[p[i]] = x;
}
int k = 0;
while ((1 << k) < n)
{
FOR (i, 0, n)
p[i] = (p[i] - (1 << k) + n) % n;
countSort(p, c);
VI c2(n);
PII pr = {c[p[0]], c[(p[0] + (1 << k)) % n]};
FOR (i, 1, n)
{
PII nx = {c[p[i]], c[(p[i] + (1 << k)) % n]};
c2[p[i]] = c2[p[i - 1]];
if (pr != nx)
c2[p[i]]++;
pr = nx;
}
c = c2;
k++;
}
p.erase(p.begin());
return p;
}
struct LCP
{
int n;
VI s, sa, rnk, lcp;
SparseTable st;
LCP(VI _s): n(SZ(_s)), s(_s)
{
sa = suffixArray(s);
rnk.resize(n);
FOR (i, 0, n)
rnk[sa[i]] = i;
lcpArray();
FOR (i, 0, n - 1)
st.PB(lcp[i]);
}
void lcpArray()
{
lcp.resize(n - 1);
int h = 0;
FOR (i, 0, n)
{
if (h > 0)
h--;
if (rnk[i] == 0)
continue;
int j = sa[rnk[i] - 1];
for (; j + h < n && i + h < n; h++)
{
if (s[j + h] != s[i + h])
break;
}
lcp[rnk[i] - 1] = h;
}
}
int queryLcp(int i, int j)
{
if (i == n || j == n)
return 0;
assert(i != j);
i = rnk[i];
j = rnk[j];
if (i > j)
swap(i, j);
return st.query(i, j);
}
// [l, r];
LL query(int l, int r)
{
LL sz = r - l + 1;
LL ans = sz * (sz + 1) / 2 + sz + 1;
int last = -1;
FOR (j, 0, n)
{
if (l <= sa[j] && sa[j] <= r)
{
if (last != -1)
ans -= queryLcp(last, sa[j]);
last = sa[j];
}
}
return ans;
}
};
LL ask(int l, int r)
{
cout << "? " << l + 1 << ' ' << r + 1 << endl;
LL ans;
cin >> ans;
return ans;
}
int main()
{
//ios::sync_with_stdio(0);
//cin.tie(0);
int n;
cin >> n;
int cur = 2;
VI ans(1, 1);
FOR (i, 1, n)
{
LCP lcp(ans);
int l = -1, r = i;
while (l + 1 < r)
{
int m = (l + r) / 2;
LL ansQ = ask(m, i);
LL ansLcp = lcp.query(m, i - 1);
if (ansQ == ansLcp)
r = m;
else
l = m;
}
if (l == -1)
ans.PB(cur++);
else
ans.PB(ans[l]);
}
cout << "!";
FOR (i, 0, n)
cout << ' ' << ans[i];
cout << endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3600kb
input:
12 3 6 6 10 10 15 10 21 15 27 20 14 6 9 20 34 43 19 9 5 2 25 8 5 25 9 19
output:
? 1 2 ? 1 3 ? 2 4 ? 1 4 ? 2 5 ? 1 5 ? 3 6 ? 1 6 ? 3 7 ? 1 7 ? 2 7 ? 4 8 ? 6 8 ? 5 8 ? 4 9 ? 2 9 ? 1 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 8 11 ? 9 11 ? 6 12 ? 9 12 ? 7 12 ! 1 2 3 4 5 6 2 5 7 7 5 6
result:
ok Accepted. 27 queries used.
Test #2:
score: 0
Accepted
time: 84ms
memory: 3964kb
input:
1000 3 5 2 3 2 7 11 8 5 2 11 3 2 11 5 7 15 8 5 3 15 8 5 2 19 7 3 2 19 4 3 2 23 5 3 2 20 5 3 2 23 5 3 2 23 9 13 15 31 14 8 5 3 31 15 7 5 3 41 16 8 5 2 45 15 7 3 2 55 20 7 15 11 58 21 8 5 2 68 21 8 5 69 21 8 5 2 80 26 12 5 8 79 24 12 5 2 89 24 12 5 8 87 26 11 5 3 100 32 11 5 2 100 31 11 5 8 112 31 12 ...
output:
? 1 2 ? 1 3 ? 2 3 ? 2 4 ? 3 4 ? 2 5 ? 1 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 6 7 ? 4 8 ? 6 8 ? 5 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ?...
result:
ok Accepted. 8880 queries used.
Test #3:
score: 0
Accepted
time: 80ms
memory: 3964kb
input:
1000 2 3 2 5 7 8 5 2 7 3 2 11 5 7 11 5 2 15 7 3 2 14 4 3 2 17 4 3 2 13 4 3 2 15 5 3 2 15 51 32 23 23 11 5 2 28 12 5 8 36 16 8 5 2 38 16 7 3 2 45 14 4 3 2 44 14 7 9 50 20 8 5 2 54 19 7 3 2 64 19 4 3 2 65 17 4 3 2 76 24 9 17 11 76 24 11 5 2 88 24 12 5 8 87 26 12 5 2 100 32 12 5 8 102 33 11 5 3 116 32 ...
output:
? 1 2 ? 1 3 ? 2 3 ? 2 4 ? 1 4 ? 2 5 ? 3 5 ? 4 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 4 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 3 14 ? 5 14 ? 6 14 ? 7 15 ? 11 15 ? 13 15 ? 14 1...
result:
ok Accepted. 8879 queries used.
Test #4:
score: 0
Accepted
time: 94ms
memory: 3804kb
input:
1000 3 5 3 5 2 8 5 8 5 2 12 5 8 12 5 2 16 7 3 2 16 7 11 21 8 5 3 20 7 5 3 27 11 5 2 27 11 3 2 33 9 3 2 31 5 3 2 36 6 4 3 2 31 6 4 3 2 35 6 4 3 2 35 11 17 26 44 17 8 5 3 44 19 8 5 2 53 19 7 3 2 53 19 4 3 2 62 24 9 19 14 63 24 11 5 3 71 24 11 5 2 69 24 11 3 2 81 29 11 5 7 81 27 11 5 3 96 30 11 5 3 99 ...
output:
? 1 2 ? 1 3 ? 2 3 ? 2 4 ? 3 4 ? 2 5 ? 3 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 4 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 7 10 ? 6 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ? 8 16 ?...
result:
ok Accepted. 8882 queries used.
Test #5:
score: 0
Accepted
time: 71ms
memory: 3944kb
input:
1000 2 3 2 5 7 8 5 3 8 5 2 11 3 2 9 3 2 11 4 3 2 6 4 3 2 13 34 27 20 17 8 5 2 24 12 5 8 26 11 5 3 32 11 5 2 31 11 5 8 36 14 8 5 3 36 15 7 5 3 44 14 7 5 3 41 11 7 5 3 52 17 8 5 2 55 19 8 5 65 21 8 5 2 65 21 8 5 76 26 12 5 2 75 24 12 5 8 87 26 11 5 3 87 26 11 5 2 98 31 11 5 8 95 31 12 5 2 105 31 12 5 ...
output:
? 1 2 ? 1 3 ? 2 3 ? 2 4 ? 1 4 ? 2 5 ? 3 5 ? 4 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 6 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 2 11 ? 3 11 ? 4 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 10 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14...
result:
ok Accepted. 8863 queries used.
Test #6:
score: 0
Accepted
time: 71ms
memory: 3940kb
input:
1000 2 5 5 2 8 5 8 5 2 12 5 8 12 5 2 16 8 5 16 8 5 2 20 8 5 21 8 5 3 27 11 5 3 26 9 5 3 31 9 5 3 27 9 5 3 34 14 8 5 2 34 15 8 5 41 14 8 5 3 41 15 7 5 3 48 19 7 5 3 48 17 7 5 3 60 17 8 5 2 63 19 7 3 2 75 25 11 5 7 77 27 11 5 2 90 27 11 3 2 91 27 11 5 7 104 33 11 5 2 104 33 12 5 8 117 31 12 5 2 115 31...
output:
? 1 2 ? 1 3 ? 2 4 ? 3 4 ? 2 5 ? 3 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 4 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 7 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 8 11 ? 9 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ? 8 16 ? 12 16 ? 14 1...
result:
ok Accepted. 8885 queries used.
Test #7:
score: 0
Accepted
time: 79ms
memory: 3940kb
input:
1000 3 5 2 3 2 7 12 8 5 2 11 3 2 11 5 7 15 8 5 3 15 8 5 2 19 7 3 2 22 41 61 50 29 11 5 3 29 11 5 3 37 11 5 2 37 13 23 29 46 18 8 5 3 45 17 7 5 3 53 14 7 5 3 51 11 7 5 3 58 13 7 5 3 57 18 38 29 69 21 9 6 69 22 9 6 80 27 12 6 9 80 28 13 5 3 93 28 13 6 9 94 30 13 6 9 109 38 13 5 2 108 38 13 5 9 122 36 ...
output:
? 1 2 ? 1 3 ? 2 3 ? 2 4 ? 3 4 ? 2 5 ? 1 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 6 7 ? 4 8 ? 6 8 ? 5 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 3 12 ? 1 12 ? 2 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ? 8...
result:
ok Accepted. 8815 queries used.
Test #8:
score: 0
Accepted
time: 89ms
memory: 3824kb
input:
1000 3 5 2 5 9 9 13 9 5 3 12 5 3 11 5 2 15 7 3 2 17 37 29 22 23 8 5 2 22 7 3 2 27 9 3 2 27 63 44 35 35 12 21 15 35 13 6 9 42 17 9 5 2 42 18 8 5 51 17 8 5 3 52 15 7 5 3 63 21 8 5 2 65 23 46 37 29 76 23 8 5 3 76 23 7 5 3 89 30 12 23 17 89 29 13 6 9 101 29 13 5 2 100 29 12 3 2 114 37 11 5 7 115 38 12 2...
output:
? 1 2 ? 1 3 ? 2 3 ? 2 4 ? 1 4 ? 2 5 ? 1 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 6 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 2 10 ? 3 10 ? 4 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 3 14 ? 5 14 ? 6 14 ? 7 15 ? 11 15 ? 9 15 ? 10 15 ? 8 1...
result:
ok Accepted. 8823 queries used.
Test #9:
score: 0
Accepted
time: 103ms
memory: 3952kb
input:
1000 3 6 5 2 8 5 8 5 2 13 24 18 13 5 2 18 9 13 18 8 5 3 23 7 5 3 23 9 17 30 13 5 2 29 12 3 2 35 9 3 2 32 5 3 2 37 6 4 3 2 33 6 4 3 2 43 11 24 33 42 15 24 33 51 21 9 6 51 22 9 5 2 61 21 9 17 13 63 23 8 5 2 75 29 11 3 2 80 30 11 5 7 93 29 11 5 2 95 26 12 5 8 107 35 71 53 44 110 37 13 5 3 125 37 13 5 2...
output:
? 1 2 ? 1 3 ? 2 4 ? 3 4 ? 2 5 ? 3 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 1 7 ? 2 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 5 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 9 12 ? 7 12 ? 6 13 ? 9 13 ? 11 13 ? 12 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ? 8 16 ? 12 16 ? 14 16...
result:
ok Accepted. 8815 queries used.
Test #10:
score: 0
Accepted
time: 98ms
memory: 3876kb
input:
1000 2 5 6 9 9 6 9 5 2 12 3 2 12 22 17 17 8 5 3 15 7 5 3 22 43 36 29 23 9 5 2 30 13 5 9 30 13 5 3 37 11 5 2 36 11 5 8 42 14 8 5 3 41 15 7 5 3 50 17 32 41 51 17 9 5 2 62 23 8 5 61 23 9 18 13 71 24 9 5 2 74 24 9 18 13 86 30 13 6 9 87 30 13 5 3 100 29 12 5 3 100 28 9 5 3 115 36 12 21 15 115 35 13 5 3 1...
output:
? 1 2 ? 1 3 ? 2 4 ? 1 4 ? 2 5 ? 3 5 ? 3 6 ? 4 6 ? 5 6 ? 3 7 ? 5 7 ? 6 7 ? 4 8 ? 2 8 ? 3 8 ? 4 9 ? 6 9 ? 7 9 ? 8 9 ? 5 10 ? 7 10 ? 8 10 ? 9 10 ? 5 11 ? 2 11 ? 3 11 ? 4 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 11 13 ? 10 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ? 8 16 ?...
result:
ok Accepted. 8811 queries used.
Test #11:
score: 0
Accepted
time: 63ms
memory: 3944kb
input:
1000 2 5 5 2 8 5 9 18 13 6 9 13 5 2 18 8 5 18 9 13 23 9 5 3 24 8 5 2 30 13 24 18 30 13 5 3 37 11 5 2 36 11 3 2 44 17 29 22 45 17 9 12 55 17 9 5 3 55 17 7 5 3 64 21 7 5 3 64 21 9 15 75 21 9 6 76 23 9 5 2 88 30 13 5 8 88 31 13 24 18 102 31 13 5 2 102 31 13 5 9 116 38 13 5 2 116 38 12 5 8 131 37 12 5 2...
output:
? 1 2 ? 1 3 ? 2 4 ? 3 4 ? 2 5 ? 3 5 ? 3 6 ? 1 6 ? 3 7 ? 5 7 ? 4 7 ? 4 8 ? 6 8 ? 7 8 ? 4 9 ? 6 9 ? 7 9 ? 5 10 ? 7 10 ? 6 10 ? 5 11 ? 8 11 ? 9 11 ? 10 11 ? 6 12 ? 9 12 ? 10 12 ? 11 12 ? 6 13 ? 9 13 ? 7 13 ? 8 13 ? 7 14 ? 10 14 ? 12 14 ? 13 14 ? 7 15 ? 11 15 ? 13 15 ? 14 15 ? 8 16 ? 12 16 ? 14 16 ? 15 ...
result:
ok Accepted. 8804 queries used.
Extra Test:
score: 0
Extra Test Passed