QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#333599 | #8235. Top Cluster | Dualqwq# | WA | 2497ms | 215152kb | C++17 | 3.9kb | 2024-02-20 09:37:43 | 2024-02-20 09:37:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FI first
#define SE second
#define mkp make_pair
namespace FastIO {
#define iL (1 << 20)
char ibuf[iL],*iS = ibuf + iL,*iT = ibuf + iL;
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread(ibuf,1,iL,stdin),iS == iT ? EOF : *iS++) : *iS++)
template<typename T>
inline void read(T &a)
{
char ch;int sign = 0;
for(ch = gc();!isdigit(ch);ch = gc())
if(ch == '-') sign = 1;
a = ch & 15;
for(ch = gc();isdigit(ch);ch = gc())
a = (a << 3) + (a << 1) + (ch & 15);
if(sign) a = -a;
}
char Out[iL],*iter = Out;
#define flush() fwrite(Out,1,iter - Out,stdout),iter = Out
template<typename T>
inline void write(T x,char end = '\n')
{
int c[40],l = 0;if(x < 0) *iter++ = '-',x = -x;
do c[++l] = x % 10,x /= 10; while(x);
while(l) *iter++ = c[l--] + '0';
*iter++ = end;flush();
}
#undef iL
#undef gc
#undef flush
}
using namespace FastIO;
const int N = 2e6 + 5;
int n,Q;
vector<pair<int,int> > G[N];
int p[N];
set<int> vals;
int fir[N],nxt[N << 1],to[N << 1],w[N << 1],ect = 1;
inline void addedge(int u1,int v1,int w1) {
nxt[++ect] = fir[u1];fir[u1] = ect;to[ect] = v1;w[ect] = w1;
}
int sze[N],pminn,pid;
bool sgn[N << 1];
void findroot(int x,int S,int fa) {
sze[x] = 1;
for(int i = fir[x],y;y = to[i],i;i = nxt[i])
if(y != fa && !sgn[i]) {
findroot(y,S,x);
sze[x] += sze[y];
if(min(sze[y],S - sze[y]) > pminn)
pminn = min(sze[y],S - sze[y]),pid = i;
}
}
long long dep[N];
vector<int> V1,V2;
void dfs(int x,int fa,vector<int> &V) {
V.push_back(x);
for(int i = fir[x],y;y = to[i],i;i = nxt[i])
if(!sgn[i] && y != fa) dep[y] = dep[x] + w[i],dfs(y,x,V);
}
vector<pair<long long,int> > Qs[N];
int Ans[N];
void Divide(int x,int S) {
if(S <= 1) return;
pminn = 0;findroot(x,S,0);
int pu = to[pid],pv = to[pid ^ 1],pw = w[pid];
int szeu = sze[pu],szev = S - sze[pu];
V1.clear();V2.clear();
sgn[pid] = sgn[pid ^ 1] = true;
dep[pu] = dep[pv] = 0;
dfs(pu,0,V1);dfs(pv,0,V2);
// printf("pu,pv,pw:%d,%d,%d\n",pu,pv,pw);
sort(V1.begin(),V1.end(),[&](const int &x,const int &y) { return dep[x] > dep[y];});
sort(V2.begin(),V2.end(),[&](const int &x,const int &y) { return dep[x] > dep[y];});
// printf("V1: ");for(auto i : V1) printf("(%d,%lld) ",i,dep[i]);printf("\n");
// printf("V2: ");for(auto i : V2) printf("(%d,%lld) ",i,dep[i]);printf("\n");
vector<pair<long long,int> > nQ;
for(auto i : V2) for(auto [k,id] : Qs[i]) if(k >= dep[i] + pw) nQ.emplace_back(k - dep[i] - pw,id);
sort(nQ.begin(),nQ.end(),[&](const pair<long long,int> &x,const pair<long long,int> &y) { return x.FI > y.FI;});
int pos = 0,mn = 1e9;
for(auto [v,id] : nQ) {
while(pos < V1.size() && dep[V1[pos]] > v) mn = min(mn,p[V1[pos]]),++pos;
Ans[id] = min(Ans[id],mn);
}
nQ.clear();
for(auto i : V1) for(auto [k,id] : Qs[i]) if(k >= dep[i] + pw) nQ.emplace_back(k - dep[i] - pw,id);
sort(nQ.begin(),nQ.end(),[&](const pair<long long,int> &x,const pair<long long,int> &y) { return x.FI > y.FI;});
pos = 0,mn = 1e9;
for(auto [v,id] : nQ) {
while(pos < V2.size() && dep[V2[pos]] > v) mn = min(mn,p[V2[pos]]),++pos;
// printf("pos:%d,%d,%d\n",pos,id,mn);
Ans[id] = min(Ans[id],mn);
}
Divide(pu,szeu);Divide(pv,szev);
}
int tot;
void dfs0(int x,int fa) {
int lst = x;
for(auto [y,w] : G[x]) if(y != fa) {
int now = ++tot;p[now] = 1e9;
addedge(lst,now,0),addedge(now,lst,0);
addedge(now,y,w);addedge(y,now,w);
lst = now;
dfs0(y,x);
}
}
int main() {
read(n);read(Q);
for(int i = 1;i <= n;i++) read(p[i]),vals.insert(p[i]);
int allmex = 0;
while(vals.count(allmex)) ++allmex;
tot = n;
for(int i = 1,u,v,w;i < n;i++)
read(u),read(v),read(w),G[u].emplace_back(v,w),G[v].emplace_back(u,w);
for(int i = 1;i <= Q;i++) {
int x;long long k;
read(x);read(k);
Qs[x].emplace_back(k,i);
Ans[i] = allmex;
}
dfs0(1,0);Divide(1,tot);
for(int i = 1;i <= Q;i++) write(Ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 16ms
memory: 97608kb
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: 2497ms
memory: 215152kb
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:
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 249999...
result:
wrong answer 1st numbers differ - expected: '0', found: '249999'