QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#749249 | #8235. Top Cluster | sandforce | WA | 537ms | 156176kb | C++23 | 4.5kb | 2024-11-14 23:12:06 | 2024-11-14 23:12:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define il inline
#define C const
#define R register
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ld long double
#define db double
#define F(name,l,r) for(int name=l;name<=r;name++)
#define RF(name,l,r) for(int name=r;name>=l;name--)
#define W(condition) while(condition)
#define PB push_back
#define PF push_front
#define I iterator
#define V(type) vector< type >
#define P(type1,type2) pair< type1 , type2 >
#define Pint pair<int,int>
#define Pll pair<long long,long long>
#define mp(x,y) make_pair(x,y)
#define Q(type) queue< type >
#define DQ(type) deque< type >
#define S(type) stack< type >
#define DGD(type) priority_queue< type >
#define XGD(type) priority_queue< type ,vector< type >,greater< type > >
#define gc getchar();
#define pc(x) putchar(x)
#define O(x) cout<<x<<'\n';
template<typename T> inline void fr(T& num) {
num = 0;
short sign = 1;
char ch = std::getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')sign = -1;
ch = std::getchar();
}
while (ch >= '0' && ch <= '9')num = num * 10 + ch - '0', ch = getchar();
num = num*sign;
}
template<typename T>inline void fw(T x) {
if (x < 0)std::putchar('-'), x = -x;
if (x > 9)fw(x / 10);
std::putchar(x % 10 + '0');
}
const int N = 1 << 19;
int n, q, w[N], pos[N], dfn[N << 1], st[N << 1][25], dep[N], l2[N], cnt;
ll dis[N];
pair<int, int> pir[N];
pair<int, int> diam[N];
vector<pair<int, int> > t[N];
inline void dfs(int x, int die) {
dfn[x] = ++cnt, st[cnt][0] = x;
for (auto [y, z] : t[x]) {
if (y == die)continue;
dep[y] = dep[x] + 1, dis[y] = dis[x] + z;
dfs(y, x), st[++cnt][0] = x;
}
}
inline void pre() {
l2[2] = 1;
for (int i = 3; i <= cnt; i++)l2[i] = l2[i >> 1] + 1;
for (int j = 1; j < 20; j++) {
for (int i = 1; i + (1 << j) - 1 <= cnt; i++) {
st[i][j] = dep[st[i][j - 1]] < dep[st[i + (1 << (j - 1))][j - 1]] ?
st[i][j - 1] : st[i + (1 << (j - 1))][j - 1];
}
}
}
inline int o1lca(int x, int y) {
x = dfn[x], y = dfn[y];
if (x > y)swap(x, y);
int lg = l2[y - x + 1];
// cerr<<"l2="<<lg<<'\n';
return dep[st[x][lg]] < dep[st[y - (1 << lg) + 1][lg]] ?
st[x][lg] : st[y - (1 << lg) + 1][lg];
}
inline ll dist(int x, int y) {
// cerr<<"lca="<<o1lca(x,y)<<'\n';
return dis[x] + dis[y] - 2 * dis[o1lca(x, y)];
}
void solve() {
fr(n), fr(q);
for (int i = 1; i <= n; i++) {
fr(w[i]);
pir[i] = {w[i], i};
}
for (int i = 1; i < n; i++) {
int u, v, w;
fr(u), fr(v), fr(w);
t[u].emplace_back(v, w);
t[v].emplace_back(u, w);
}
sort(pir + 1, pir + n + 1);
int mex = n;
for (int i = 1; i <= n; i++) {
if (pir[i].first != i - 1) {
mex = i - 1;
break;
}
}
dfs(1, 0);
pre();
// cerr<<"\ndfn=";
// for(int i=1;i<=n;i++){
// cerr<<dfn[i]<<' ';
// }cerr<<'\n';
diam[1] = {pir[1].second, pir[1].second};
for (int i = 2; i <= n; i++) {
diam[i] = diam[i - 1];
ll len = dist(diam[i].first, diam[i].second);
// cerr<<len<<'\n';
int cur = pir[i].second;
if (dist(diam[i - 1].first, cur) > len)len = dist(diam[i - 1].first, cur), diam[i] = {diam[i - 1].first, cur};
if (dist(diam[i - 1].second, cur) > len)len = dist(diam[i - 1].second, cur), diam[i] = {diam[i - 1].second, cur};
}
// for(int i=1;i<=n;i++)cerr<<diam[i].first<<' '<<diam[i].second<<'\n';
while (q-->0) {
int x;
ll k;
fr(x), fr(k);
int l = 1, r = n, res = n + 1, mid;
while (l <= r) {
mid = (l + r) >> 1;
// cerr<<max(dist(x,diam[mid].first),dist(x,diam[mid].second))<<'\n';
if (max(dist(x, diam[mid].first), dist(x, diam[mid].second)) > k) {
res = mid, r = mid - 1;
} else l = mid + 1;
}
int ans = mex;
// fw(res),pc(' ');
if (res != n + 1)ans = min(ans, pir[res].first);
fw(ans), pc('\n');
}
// cerr<<"MEX="<<mex<<'\n';
}
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int Count = 1; //fr(Count);
while (Count--)solve();
}
/*
多测不清空,OI见祖宗。
multitesting without clearing,oier meets the LCA.
十年OI一场空,不开LL见祖宗。
Ten years of OI just AFO,no #define int long long sees the LCA.
似是神犇成才处,实为蒟蒻黄泉路。
It is likely to be the Au medal for the big old,but in fact it is the Si medal for me.
黄题有恨无正解,码力不若小学生。
A yellow problem I can't AC,codeforces is not as NB as EUlar Shai.
今生无奈入OI,来世不做信竞人。
This life I am a Silly Being in oi,next life I won't f**k the sh*t of infomatics.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 13904kb
input:
5 4 3 9 0 1 2 1 2 10 3 1 4 3 4 3 3 5 2 3 0 1 0 4 6 4 7
output:
1 0 3 4
result:
ok 4 number(s): "1 0 3 4"
Test #2:
score: -100
Wrong Answer
time: 537ms
memory: 156176kb
input:
500000 500000 350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...
output:
0 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 2499...
result:
wrong answer 20939th numbers differ - expected: '4', found: '8'