voidcalc(int s, longlong x){ la = lb = 0; int k = 0; for (int i = 18; i >= 0; i--) { if (f[i][s][k] && da[i][s][k] + db[i][s][k] <= x) { x -= da[i][s][k] + db[i][s][k]; la += da[i][s][k], lb += db[i][s][k]; if (!i)k ^= 1; s = f[i][s][k]; } } }
intcho(int a, int b, int c){ if (!a) return g[b].id; if (!b) return g[a].id; if (g[c].h - g[a].h <= g[b].h - g[c].h) return g[a].id; elsereturn g[b].id; }
voidcalc(int s, longlong x){ la = lb = 0; int k = 0; for (int i = 18; i >= 0; i--) { if (f[i][s][k] && da[i][s][k] + db[i][s][k] <= x) { x -= da[i][s][k] + db[i][s][k]; la += da[i][s][k], lb += db[i][s][k]; if (!i)k ^= 1; s = f[i][s][k]; } } }
boolcmp(nod a, nod b){ return a.h < b.h; }
intmain(){ n = read(); for (int i = 1; i <= n; i++) { g[i].h = read(); g[i].id = i; } //建双向链表 sort(g + 1, g + n + 1, cmp); for (int i = 1; i <= n; i++) { g[i].l = i - 1; g[i].r = i + 1; pos[g[i].id] = i; } g[1].l = g[n].r = 0; //预处理第一和第二近的 for (int i = 1; i < n; i++) { int p = pos[i], p1 = g[p].l, p2 = g[p].r; if (p1 && (g[p].h - g[p1].h <= g[p2].h - g[p].h || !p2)) { min1[i] = g[p1].id; min2[i] = cho(g[p1].l, p2, p); } else { min1[i] = g[p2].id; min2[i] = cho(p1, g[p2].r, p); } del(p); } //倍增dp //初始化 for (int i = 1; i <= n; i++) { if (min2[i]) { f[0][i][0] = min2[i]; da[0][i][0] = abs(g[pos[i]].h - g[pos[min2[i]]].h); db[0][i][0] = 0; } if (min1[i]) { f[0][i][1] = min1[i]; da[0][i][1] = 0; db[0][i][1] = abs(g[pos[i]].h - g[pos[min1[i]]].h); } } //转移 for (int i = 1; i <= 18; i++) { for (int j = 1; j <= n; j++) { for (int k = 0; k <= 1; k++) { int l; if (i == 1) l = k ^ 1; else l = k; if (f[i - 1][j][k]) f[i][j][k] = f[i - 1][f[i - 1][j][k]][l]; if (f[i][j][k]) { da[i][j][k] = da[i - 1][j][k] + da[i - 1][f[i - 1][j][k]][l]; if (f[i][j][k]) { da[i][j][k] = da[i - 1][j][k] + da[i - 1][f[i - 1][j][k]][l]; db[i][j][k] = db[i - 1][j][k] + db[i - 1][f[i - 1][j][k]][l]; }
} } } } //第一问:枚举起点 longlong x; int s; scanf("%lld", &x); int p = 0; longlong ansa = 1, ansb = 0; for (int i = 1; i <= n; i++) { calc(i, x); if (!lb) la = 1; if (la * ansb < lb * ansa || (la * ansb == lb * ansa && g[pos[i]].h > g[pos[p]].h)) ansa = la, ansb = lb, p = i; } printf("%d\n", p); //第二问:a,b行驶的距离 int m = read(); while (m--) { s = read(), x = read(); calc(s, x); printf("%lld %lld\n", la, lb); } return0; }