QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#170898 | #7182. Very Sparse Table | ucup-team1477# | AC ✓ | 1231ms | 290260kb | C++17 | 7.6kb | 2023-09-09 16:10:45 | 2023-09-09 16:10:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,const T&y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,const T&y){if(y<x)x=y;}
#define pb push_back
#define ALL(x) (x).begin(),(x).end()
#define mem(x) memset((x),0,sizeof(x))
typedef double db;
typedef long long ll;
typedef vector<int>vi;
typedef pair<int,int>pii;
typedef unsigned u32;
typedef unsigned uint;
typedef unsigned long long u64;
namespace orzjk{
const int SZ=1e6;
char buf[SZ],*p1=buf,*p2=buf;
char nc(){
return p1==p2&&(p2=(p1=buf)+fread(buf,1,SZ,stdin),p1==p2)?EOF:*p1++;
}
char fub[SZ],*p3=fub,*p4=fub+SZ;
void pc(char c){
p3==p4&&(fwrite(fub,1,SZ,stdout),p3=fub);
*p3++=c;
}
void flush(){
fwrite(fub,1,p3-fub,stdout),p3=fub;
}
}
using orzjk::nc;
using orzjk::pc;
//#define nc getchar
//#define pc putchar
int read(){
int x=0,f=1;char c=nc();
while(c<48)c=='-'&&(f=-1),c=nc();
while(c>47)x=x*10+(c^48),c=nc();
return x*f;
}
template<class T>void write(T x){
static char st[41];
if(!x)return pc(48),void();
char*p=st;
if(x<0)x=-x,pc('-');
for(;x;x/=10)*p++=x%10|48;
do{
pc(*--p);
}while(p!=st);
}
//const int P=1e9+7;
const int P=998244353;
int qp(int a,int k){
int res=1;for(;k;k>>=1,a=1ll*a*a%P)if(k&1)res=1ll*res*a%P;return res;
}
// https://uoj.ac/submission/544079
// Skyqwq
#define pb push_back
#define fi first
#define se second
#define mp make_pair
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; }
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 = 1<<16|5, S = 4;
int n, k, m, f[S][N];
PII e[1000000];
bitset<N>g[N];
struct sto{
int a,b,c;
};
vector<sto>out;
void inline add(int i, int j, int mid) {
if (i + 1 >= j || g[i][j] || i < 0 || j >= n) return;
g[i][j] = 1;
e[++m] = mp(i, j);
out.pb({i,mid,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 (i >= 100) {
int ot = 50;
if (j >= 10) ot = 200;
if (j == 14) ot = 400;
if (j == 15) ot = 500;
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, n);
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 };
}
}
}
}
}
}
vector<int> ed[N];
int F[N];
void inline chk() {
for (int i = 1; i <= m; i++)
ed[e[i].fi].pb(e[i].se);
for (int i = 0; i + 1 < n; i++) ed[i].pb(i + 1);
for (int i = 0; i < n; i++) {
memset(F, 0x3f, sizeof F);
F[i] = 0;
for (int u = i; u < n; u++) {
if (F[u] > k) cout << i << " " << u << "???" << F[u] << endl;
assert(F[u] <= k);
for (int v: ed[u]) chkMin(F[v], F[u] + 1);
}
}
}
int tim;
vi anc[N];
vi twi[N];
void inline link(int k, vector<int> 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], a[j-1]);
return;
}
if (k == 2) {
assert(0);
// int mid = n >> 1;
// vector<int> A, B;
// per(i,mid)
// for (int i = 0; i < mid; i++) {
// add(a[i], a[mid]);
// A.pb(a[i]);
// }
// for (int i = mid + 1; i < n; i++)
// add(a[mid], a[i]), B.pb(a[i]);
// link(k, A), link(k, B);
return;
}
if (k > 1 && f[k][n] == f[k - 1][n]) {
link(k - 1, a);
return;
}
int orz=++tim;
for(int u:a)anc[u].pb(tim);
// printf("#%d\n",tim);
// for(int u:a)printf("%d ",u);
// puts("");
W o = G[k][n];
int m = o.m, u = o.u, v = o.v, P = o.P, Q = o.Q, Z = o.Z;
vector<int> key, le, re;
per(i,u-1,0)add(a[i],a[u],a[i+1]);
for (int i = 0; i < u; i++)
le.pb(a[i]);
link(k, le);
for (int i = n - v; i < n; i++)
add(a[n - v - 1], a[i],a[i-1]), 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]), add(a[X], a[j],a[j-1]);
per(j,Y-1,X+1)add(a[j],a[Y],a[j+1]);
link(k, now);
add(key.back(),a[num],a[Y-1]);
key.pb(a[num]);
}
// for (int i = 0; i + 1 < (int)key.size(); i++) add(key[i], key[i + 1]);
link(k - 2, key);
// if(orz==1){
// printf("### %d %d\n",n-v,n-1);
// printf("[%d %d]\n",key[0],key.back());
// }
twi[orz]=key;
}
void solve() {
read(n), k=3; ++n;
work();
vector<int> t;
for (int i = 0; i < n; i++) t.pb(i);
link(k, t);
printf("%d\n", m); //chk(); return 0;
//// static int dou[N];
//// rep(i,1,m)dou[e[i].fi]++;
// int mx=0;
//// rep(i,0,n-1)chkmax(mx,dou[i]);
// rep(i,0,n-1)chkmax(mx,(int)anc[i].size());
// cout<<mx<<endl;
// return;
rep(i,0,n-2)g[i][i+1]=1;
for(auto[a,b,c]:out)printf("%d %d %d\n",a,b,c);
// for (int i = 1; i <= m; i++) printf("%d %d\n", e[i].fi, e[i].se);
fflush(stdout);
int q;
read(q);
while(q--){
fflush(stdout);
int l,r;
read(l),read(r);
if(g[l][r]){
printf("%d %d\n",l,r);
continue;
}
if(l+2==r){
printf("%d %d %d\n",l,l+1,l+2);
continue;
}
if(l+3==r){
printf("%d %d %d %d\n",l,l+1,l+2,l+3);
continue;
}
int clo=0;
per(_,anc[l].size()-1,0){
int x=anc[l][_];
auto it=lower_bound(ALL(anc[r]),x);
if(it!=anc[r].end()&&*it==x){
clo=x;break;
}
}
// printf("!%d\n",clo);
vi&key=twi[clo];
int lef=*lower_bound(ALL(key),l);
// printf("#%d\n",lef);
int rig=*--upper_bound(ALL(key),r);
// printf("$%d\n",rig);
assert(l==lef||g[l][lef]);
assert(rig==r||g[rig][r]);
assert(lef==rig||g[lef][rig]);
vi vec;
vec.pb(l);
vec.pb(lef);
vec.pb(rig);
vec.pb(r);
vec.erase(unique(ALL(vec)),vec.end());
for(int x:vec)printf("%d ",x);
puts("");
}
fflush(stdout);
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// int T;cin>>T;while(T--)solve();
solve();
orzjk::flush();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 17524kb
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 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 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 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 7 5 8 5 9 ...
result:
ok edges: 6
Test #2:
score: 0
Accepted
time: 3ms
memory: 16144kb
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 7 11 12 12 13 14 12 14 15 12 15 16 15 16 17 14 15 17 13 14 17 12 16 17 17 18 19 17 19 20 17 20 21 2...
result:
ok edges: 44
Test #3:
score: 0
Accepted
time: 332ms
memory: 20420kb
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: 1166ms
memory: 290260kb
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: 1ms
memory: 15628kb
input:
0 0
output:
0
result:
ok edges: 0
Test #6:
score: 0
Accepted
time: 1ms
memory: 14812kb
input:
1 1 0 1
output:
0 0 1
result:
ok edges: 0
Test #7:
score: 0
Accepted
time: 3ms
memory: 14176kb
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: 3ms
memory: 15176kb
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: 1231ms
memory: 289476kb
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: 1089ms
memory: 287624kb
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