QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#167599 | #7182. Very Sparse Table | ucup-team052# | AC ✓ | 3395ms | 40164kb | C++23 | 6.4kb | 2023-09-07 16:08:36 | 2023-09-07 16:08:36 |
Judging History
answer
// Skyqwq
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template <typename T> bool chkMax(T &x, T y) {
return (y > x) ? x = y, 1 : 0;
}
template <typename T> bool chkMin(T &x, T y) {
return (y < x) ? x = y, 1 : 0;
}
void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);}
template <typename T> void inline read(T &x) {
int f = 1;
x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-')
f = -1;
s = getchar();
}
while (s <= '9' && s >= '0')
x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N = 65540, S = 4;
int n, k, m, f[4][N];
vector<tuple<int,int,int>> ans;
map<pair<int,int>,int> vis;
void inline add(int i, int k, int j) {
if (i + 1 >= j) return ;
// printf("%d %d %d\n",i,k,j);
// fflush(stdout);
assert(vis[make_pair(i,k)]==1);
assert(vis[make_pair(k,j)]==1);
vis[{i,j}]=1;
ans.emplace_back(i,k,j);
}
struct W {
int m, u, v, P, Q, Z;
} G[S][N];
void inline work() {
memset(f, 0x3f, sizeof f);
for (int i = 0; i <= k; i++)
f[i][0] = f[i][1] = 0;
for (int i = 1; i <= n; i++)
f[1][i] = 1ll * (i - 1) * (i - 2) / 2;
for (int i = 2; i <= n; i++) {
if (i <= 3) {
f[2][i] = 0;
continue;
}
int A = (i - 1) / 2;
f[2][i] = f[2][A] + f[2][i - A - 1] + i - 3;
}
for (int j = 3; j <= k; j++) {
for (int i = 1; i <= n; i++) {
if (i <= j + 1) {
f[j][i] = 0;
continue;
}
f[j][i] = f[j - 1][i];
int le = 3, re = i;
// if (j <= 3) re = min(re, 35);
// else if (j <= 5) re = min(re, 400);
// else le = max(3, i / 2 - 30), re = min(i, i / 2 + 30);
int B = 2e3;
// if (j <= 3) B = 200;
// else if (j <= 4) B = 60;
// else if (j <= 5) B = 50;
if (i >= 100) {
int ot = 200;
chkMin(re, G[j][i - 1].m + ot);
chkMax(le, G[j][i - 1].m - ot);
}
for (int m = le; m <= re; m++) {
int L = max((i - (m - 1)) / m, 0), R = min((i - (m - 1)) / 2, L + B);
int lim = j <= 5 ? 5 : 1;
chkMax(L, G[j][i - 1].u - lim);
chkMin(R, G[j][i - 1].u + lim);
for (int u = L; u <= R; u++) {
for (int v = u; v <= u + 1; v++) {
int le = (i - (m - 1) - u - v);
if (le < 0)
continue;
int c = f[j][u] + f[j][v] + max(u - 1, 0) + max(v - 1, 0);
c += f[j - 2][m - 1] + m - 2;
int P = le / (m - 2), Q = le % (m - 2);
int Z = m - 2 - Q;
c += f[j][P] * Z + Z * (max(2 * P - 2, 0));
c += f[j][P + 1] * Q + Q * (2 * P);
if (chkMin(f[j][i], c))
G[j][i] = (W) {
m, u, v, P, Q, Z
};
}
}
}
}
}
// cout<< f[k][n] << endl;
// for (int i = 1; i <= k; i++)cout << G[i][n].u << " awa" <<G[i][n].m << " awa\n";
}
vector<int> ed[N];
void inline link(int k, vector<int> a) {
// print(a);
int n = a.size();
if (n <= k + 1)
return;
if (k == 1) {
for (int i = 0; i < n; i++)
for (int j = i + 2; j < n; j++)
add(a[i], a[j-1], a[j]);
return;
}
W o = G[k][n];
int m = o.m, u = o.u, v = o.v, P = o.P, Q = o.Q;
// printf("%d %d %d %d %d %d\n",n,m,u,v,P,a[u]);
vector<int> key, le, re;
for(int i=u-1;i>=0;i--)
{
add(a[i], a[i+1], a[u]), le.pb(a[i]);
}
reverse(le.begin(),le.end());
link(k, le);
for (int i = n - v; i < n; i++)
add(a[n - v - 1], a[i-1], a[i]), re.pb(a[i]);
link(k, re);
key.pb(a[u]);
int num = u;
for (int i = 1; i <= m - 2; i++) {
int la = num;
num += P + 1;
if (i <= Q)
num++;
vector<int> now;
int X = la, Y = num;
for (int j = X + 1; j < Y; j++)
now.pb(a[j]);
for(int j=X+1;j<Y;j++)
add(a[X], a[j-1], a[j]);
for(int j=Y-1;j>X;j--)
add(a[j], a[j+1], a[Y]);
link(k, now);
key.pb(a[num]);
}
for (int i = 0; i + 1 < (int)key.size(); i++)
add(key[i], key[i]+1, key[i + 1]);
link(k - 2, key);
}
void qry(int l,int r,int L,int R)
{
if(R-L<=3)
{
for(int i=L;i<=R;i++) printf("%d%c",i," \n"[i==R]);
fflush(stdout);
return ;
}
// printf("* %d %d %d %d\n",l,r,L,R);
int n=r-l+1;
W o = G[k][n];
int m = o.m, u = o.u, v = o.v, P = o.P, Q = o.Q;
vector<int> key;
key.pb(l-1);
key.pb(l+u);
int num = u;
for (int i = 1; i <= m - 2; i++) {
num += P + 1;
if (i <= Q)
num++;
key.pb(l+num);
}
key.pb(r+1);
int idl=key.size(),idr=0;
for(int i=(int)key.size()-1;i>=0;i--)
{
if(R>=key[i])
{
idr=i;
break;
}
}
for(int i=0;i<(int)key.size();i++)
{
if(L<=key[i])
{
idl=i;
break;
}
}
if(idl>idr)
{
qry(key[idr]+1,key[idl]-1,L,R);
}
else
{
vector<int> ve={L,key[idl],key[idr],R};
sort(ve.begin(),ve.end());
ve.erase(unique(ve.begin(),ve.end()),ve.end());
print(ve);
fflush(stdout);
for(int i=0;i+1<(int)ve.size();i++) assert(vis[make_pair(ve[i],ve[i+1])]==1);
}
}
int main() {
read(n); k=3;
++n;
for(int i=0;i<n;i++) vis[make_pair(i,i+1)]=1;
work();
vector<int> t;
for (int i = 0; i < n; i++)
t.pb(i);
link(k, t);
cout<<ans.size()<<endl;
for(auto [a,b,c]:ans) printf("%d %d %d\n",a,b,c);
fflush(stdout);
int Q; cin>>Q;
while(Q--)
{
int l,r; read(l),read(r);
qry(0,n-1,l,r);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 8208kb
input:
9 45 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 4 3 5 3 6 3 7 3 8 3 9 4 5 4 6 4 7 4 8 4 9 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9
output:
6 1 2 3 0 1 3 5 6 7 5 7 8 5 8 9 3 4 5 0 1 0 1 2 0 1 2 3 0 3 4 0 3 5 0 3 5 6 0 3 5 7 0 3 5 8 0 3 5 9 1 2 1 2 3 1 2 3 4 1 3 5 1 3 5 6 1 3 5 7 1 3 5 8 1 3 5 9 2 3 2 3 4 2 3 4 5 2 3 5 6 2 3 5 7 2 3 5 8 2 3 5 9 3 4 3 4 5 3 4 5 6 3 5 7 3 5 8 3 5 9 4 5 4 5 6 4 5 6 7 4 5 8 4 5 9 5 6 5 6 7 5 6 7 8 5 9 6 7 6 ...
result:
ok edges: 6
Test #2:
score: 0
Accepted
time: 7ms
memory: 8124kb
input:
30 465 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 2 3 2 4 2 5 2 6...
output:
44 5 6 7 4 5 7 3 4 7 2 3 7 1 2 7 0 1 7 0 1 2 4 5 6 2 3 4 22 23 24 22 24 25 22 25 26 22 26 27 22 27 28 22 28 29 22 29 30 23 24 25 27 28 29 27 29 30 25 26 27 7 8 9 7 9 10 7 10 11 10 11 12 9 10 12 8 9 12 12 13 14 12 14 15 12 15 16 15 16 17 14 15 17 13 14 17 17 18 19 17 19 20 17 20 21 20 21 22 19 20 22 ...
result:
ok edges: 44
Test #3:
score: 0
Accepted
time: 649ms
memory: 8468kb
input:
736 200000 170 268 126 166 565 723 664 735 61 524 226 234 146 314 217 272 294 713 115 381 563 706 74 567 552 614 120 211 472 620 213 432 488 623 447 564 96 129 331 354 79 677 50 547 174 568 56 129 189 227 55 701 244 253 264 715 154 220 380 657 46 390 53 161 325 537 666 696 64 465 391 659 284 448 207...
output:
2512 69 70 71 68 69 71 67 68 71 66 67 71 65 66 71 64 65 71 63 64 71 62 63 71 61 62 71 60 61 71 59 60 71 58 59 71 57 58 71 56 57 71 55 56 71 54 55 71 53 54 71 52 53 71 51 52 71 50 51 71 49 50 71 48 49 71 47 48 71 46 47 71 45 46 71 44 45 71 43 44 71 42 43 71 41 42 71 40 41 71 39 40 71 38 39 71 37 38 7...
result:
ok edges: 2512
Test #4:
score: 0
Accepted
time: 3325ms
memory: 39376kb
input:
65536 200000 51949 58727 7943 43298 6290 7369 41493 53070 24229 36675 28087 49947 11703 48217 19923 24739 2144 59777 53830 56793 13509 37211 2300 38595 27415 42879 24616 48531 58341 63327 20628 38407 48616 60290 7450 61685 37010 47595 22164 42732 19181 29850 35383 43587 39257 44397 19340 45183 34523...
output:
333988 2033 2034 2035 2032 2033 2035 2031 2032 2035 2030 2031 2035 2029 2030 2035 2028 2029 2035 2027 2028 2035 2026 2027 2035 2025 2026 2035 2024 2025 2035 2023 2024 2035 2022 2023 2035 2021 2022 2035 2020 2021 2035 2019 2020 2035 2018 2019 2035 2017 2018 2035 2016 2017 2035 2015 2016 2035 2014 201...
result:
ok edges: 333988
Test #5:
score: 0
Accepted
time: 2ms
memory: 8080kb
input:
0 0
output:
0
result:
ok edges: 0
Test #6:
score: 0
Accepted
time: 2ms
memory: 8168kb
input:
1 1 0 1
output:
0 0 1
result:
ok edges: 0
Test #7:
score: 0
Accepted
time: 2ms
memory: 7860kb
input:
2 3 0 1 0 2 1 2
output:
0 0 1 0 1 2 1 2
result:
ok edges: 0
Test #8:
score: 0
Accepted
time: 1ms
memory: 7540kb
input:
3 6 0 1 0 2 0 3 1 2 1 3 2 3
output:
0 0 1 0 1 2 0 1 2 3 1 2 1 2 3 2 3
result:
ok edges: 0
Test #9:
score: 0
Accepted
time: 3395ms
memory: 39380kb
input:
65535 200000 35006 46944 17075 57351 24605 50445 5938 60705 15221 40233 28599 38915 1132 35574 8555 31494 13644 35806 44940 55401 9503 59206 21011 26540 41156 62487 57510 64305 9254 25610 17301 47249 34083 49167 48018 64394 38855 62175 15464 22525 23728 60275 54028 63810 22711 53902 5984 48625 5838 ...
output:
333982 2033 2034 2035 2032 2033 2035 2031 2032 2035 2030 2031 2035 2029 2030 2035 2028 2029 2035 2027 2028 2035 2026 2027 2035 2025 2026 2035 2024 2025 2035 2023 2024 2035 2022 2023 2035 2021 2022 2035 2020 2021 2035 2019 2020 2035 2018 2019 2035 2017 2018 2035 2016 2017 2035 2015 2016 2035 2014 201...
result:
ok edges: 333982
Test #10:
score: 0
Accepted
time: 3386ms
memory: 40164kb
input:
64800 200000 55124 62263 24992 39760 32262 37059 25987 42889 10413 64701 7223 43221 45810 63205 11437 29357 10814 52096 1154 36319 10730 54157 18473 26729 9152 23374 5426 12744 3502 37577 5559 37160 30503 62433 12426 47332 14933 62086 8781 21527 27180 53773 29658 46742 20592 61553 8337 27197 8024 38...
output:
330048 2061 2062 2063 2060 2061 2063 2059 2060 2063 2058 2059 2063 2057 2058 2063 2056 2057 2063 2055 2056 2063 2054 2055 2063 2053 2054 2063 2052 2053 2063 2051 2052 2063 2050 2051 2063 2049 2050 2063 2048 2049 2063 2047 2048 2063 2046 2047 2063 2045 2046 2063 2044 2045 2063 2043 2044 2063 2042 204...
result:
ok edges: 330048
Extra Test:
score: 0
Extra Test Passed