QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#108861 | #4924. 蜘蛛爬树 | Reanap | 0 | 2704ms | 142400kb | C++14 | 7.3kb | 2023-05-26 19:52:39 | 2023-05-26 19:52:41 |
Judging History
answer
#include <bits/stdc++.h>
#define pii pair <int , int>
#define pll pair <LL , LL>
#define mp make_pair
#define fs first
#define sc second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//const int Mxdt=100000;
//static char buf[Mxdt],*p1=buf,*p2=buf;
//#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxdt,stdin),p1==p2)?EOF:*p1++;
template <typename T>
void read(T &x) {
T f=1;x=0;char s=getchar();
while(s<'0'||s>'9') {if(s=='-') f=-1;s=getchar();}
while(s>='0'&&s<='9') {x=(x<<3)+(x<<1)+(s^'0');s=getchar();}
x *= f;
}
template <typename T>
void write(T x , char s='\n') {
if(!x) {putchar('0');putchar(s);return;}
if(x<0) {putchar('-');x=-x;}
T t = 0 , tmp[25] = {};
while(x) tmp[t ++] = x % 10 , x /= 10;
while(t -- > 0) putchar(tmp[t] + '0');
putchar(s);
}
const int MAXN = 2e5 + 5;
int rt , ls[MAXN * 20] , rs[MAXN * 20] , num , n , m , q;
LL K[MAXN * 20] , B[MAXN * 20];
void Ins(int l , int r , int &now , LL k , LL b) {
if(!now) {
now = ++num;
K[now] = k , B[now] = b;
return;
}
LL Lval = K[now] * l + B[now] , Rval = K[now] * r + B[now];
LL lv = k * l + b , rv = k * r + b;
if(Lval <= lv && Rval <= rv) return;
if(Lval >= lv && Rval >= rv) return;
int mid = (l + r) >> 1;
LL Mval = mid * K[now] + B[now];
LL mv = mid * k + b;
if(Mval <= mv) {
if(Lval <= lv) Ins(mid + 1 , r , rs[now] , k , b);
else Ins(l , mid , ls[now] , k , b);
}
else {
swap(K[now] , k) , swap(B[now] , b);
if(lv <= Lval) Ins(mid + 1 , r , rs[now] , k , b);
else Ins(l , mid , ls[now] , k , b);
}
}
LL find(int l , int r , int now , int x) {
if(!now) return 1e18;
LL res = K[now] * x + B[now];
if(l == r) return res;
int mid = (l + r) >> 1;
if(x <= mid) res = min(res , find(l , mid , ls[now] , x));
else res = min(res , find(mid + 1 , r , rs[now] , x));
return res;
}
void clear(int l , int r , int now) {
if(!now) return;
int mid = (l + r) >> 1;
clear(l , mid , ls[now]);
clear(mid + 1 , r , rs[now]);
ls[now] = rs[now] = 0;
}
vector <pii> G[MAXN];
int siz[MAXN] , hson[MAXN] , dep[MAXN];
LL dis[MAXN] , hv[MAXN];
void dfs1(int x , int fa) {
siz[x] = 1;dep[x] = dep[fa] + 1;
for (auto v:G[x]) {
if(v.fs == fa) continue;
dis[v.fs] = dis[x] + v.sc;
dfs1(v.fs , x);
siz[x] += siz[v.fs];
if(siz[v.fs] > siz[hson[x]]) hson[x] = v.fs , hv[x] = v.sc;
}
}
int top[MAXN] , Num , dfn[MAXN] , f[MAXN] , bot[MAXN] , rf[MAXN];
void dfs2(int x , int fa , int tp) {
dfn[x] = ++Num;f[x] = fa;rf[Num] = x;
top[x] = tp;bot[tp] = x;
if(hson[x]) dfs2(hson[x] , x , tp);
for (auto v:G[x]) {
if(v.fs == fa || v.fs == hson[x]) continue;
dfs2(v.fs , x , v.fs);
}
}
int LCA(int x , int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x , y);
x = f[top[x]];
}
if(dep[x] > dep[y]) swap(x , y);
return x;
}
LL Ans[MAXN] , a[MAXN];
namespace For_LCA {
int siz[MAXN] , mx , Rt , vis[MAXN];
vector <pii> Q[MAXN];
void find_root(int x , int fa , int Tot) {
siz[x] = 1;
int cur = 0;
for (auto v:G[x]) {
if(v.fs == fa || vis[v.fs]) continue;
find_root(v.fs , x , Tot);
siz[x] += siz[v.fs];
cur = max(cur , siz[v.fs]);
}
cur = max(cur , Tot - siz[x]);
if(cur < mx) mx = cur , Rt = x;
}
void dfs(int x , int fa , LL d) {
Ins(0 , m , rt , a[x] , d * 2);
for (auto v:G[x]) {
if(v.fs == fa || vis[v.fs]) continue;
dfs(v.fs , x , d + v.sc);
}
}
void calc(int x , int fa , LL d) {
for (auto v:Q[x]) {
Ans[v.sc] = min(Ans[v.sc] , find(0 , m , rt , v.fs) + d * 2);
}
for (auto v:G[x]) {
if(v.fs == fa || vis[v.fs]) continue;
calc(v.fs , x , d + v.sc);
}
}
void work(int x) {
mx = Rt = 1e9;
find_root(x , 0 , n);
mx = Rt = 1e9;
find_root(x , 0 , siz[x]);
x = Rt;vis[x] = 1;
dfs(x , 0 , 0);
calc(x , 0 , 0);
clear(0 , m , rt);
num = rt = 0;
for (auto v:G[x]) if(!vis[v.fs]) {
work(v.fs);
}
}
}
namespace SubTree {
vector <pair <pii , LL> > Q[MAXN];
void calc(int x , int fa , int hs) {
Ins(0 , m , rt , a[x] , dis[x] * 2);
for (auto v:G[x]) {
if(v.fs == fa || v.fs == hs) continue;
calc(v.fs , x , hs);
}
}
void dfs(int x , int fa , int op) {
for (auto v:G[x]) {
if(v.fs == fa || v.fs == hson[x]) continue;
dfs(v.fs , x , 0);
}
if(hson[x]) dfs(hson[x] , x , 1);
calc(x , fa , hson[x]);
for (auto v:Q[x]) {
Ans[v.fs.fs] = min(Ans[v.fs.fs] , find(0 , m , rt , v.fs.sc) - dis[x] * 2 + v.sc);
}
if(!op) clear(0 , m , rt) , num = rt = 0;
}
}
namespace Divide {
vector <pair <int , pii> > LQ[MAXN] , RQ[MAXN];
void Add(int x , int gl , int id , int d) {
SubTree::Q[x].push_back(mp(mp(id , d) , 0));
while(top[x] != top[gl]) {
LQ[dfn[top[x]]].push_back(mp(dfn[x] , mp(id , d)));
RQ[dfn[x]].push_back(mp(dfn[top[x]] , mp(id , d)));
x = f[top[x]];
SubTree::Q[hson[x]].push_back(mp(mp(id , d) , hv[x] * 2));
}
if(x != gl) {
LQ[dfn[gl] + 1].push_back(mp(dfn[x] , mp(id , d)));
LQ[dfn[x]].push_back(mp(dfn[gl] + 1 , mp(id , d)));
}
}
void dfs(int x , int fa , LL s) {
Ins(0 , m , rt , a[x] , (dis[x] - s) * 2);
for (auto v:G[x]) {
if(v.fs == fa) continue;
dfs(v.fs , x , s);
}
}
void solve(int l , int r) {
if(l == r) {
if(!LQ[l].size()) return;
return;
}
int mid = (l + r) >> 1;
for (int i = mid; i >= l; --i) {
int x = rf[i];
for (auto v:G[x]) {
if(v.fs == hson[x] || v.fs == f[x]) continue;
dfs(v.fs , x , dis[x]);
}
while(LQ[i].size()) {
auto cur = (*LQ[i].rbegin());
if(cur.fs > mid) Ans[cur.sc.fs] = min(Ans[cur.sc.fs] , find(0 , m , rt , cur.sc.sc));
LQ[i].pop_back();
}
}
clear(0 , m , rt);num = rt = 0;
for (int i = mid + 1; i <= r; ++i) {
int x = rf[i];
for (auto v:G[x]) {
if(v.fs == hson[x] || v.fs == f[x]) continue;
dfs(v.fs , x , dis[x]);
}
while(RQ[i].size()) {
auto cur = (*RQ[i].rbegin());
if(cur.fs <= mid) Ans[cur.sc.fs] = min(Ans[cur.sc.fs] , find(0 , m , rt , cur.sc.sc));
RQ[i].pop_back();
}
}
clear(0 , m , rt);num = rt = 0;
solve(l , mid) , solve(mid + 1 , r);
}
void calc() {
for (int i = 1; i <= n; ++i) {
sort(LQ[i].begin() , LQ[i].end());
sort(RQ[i].begin() , RQ[i].end() , [&] (pair <int , pii> x , pair <int , pii> y) {return x > y;});
}
for (int i = 1; i <= n; ++i) if(top[i] == i) {
solve(dfn[i] , dfn[bot[i]]);
}
}
}
LL Bas[MAXN];
int main() {
read(n),read(m),read(q);
for (int i = 1; i <= n; ++i) read(a[i]);
for (int i = 1; i < n; ++i) {
int u , v , w;
read(u),read(v),read(w);
G[u].push_back(mp(v , w));
G[v].push_back(mp(u , w));
}
dfs1(1 , 0);
dfs2(1 , 0 , 1);
for (int i = 1; i <= q; ++i) {
int s , t;
read(s),read(t);
int d = (t - 1) / n - (s - 1) / n;
if(d < 0) d = -d;
s = (s - 1) % n + 1 , t = (t - 1) % n + 1;
int lca = LCA(s , t);
Bas[i] = dis[s] + dis[t] - dis[lca] * 2;
For_LCA::Q[lca].push_back(mp(d , i));
Divide::Add(s , lca , i , d);
Divide::Add(t , lca , i , d);
Ans[i] = 1e18;
}
For_LCA::work(1);
SubTree::dfs(1 , 0 , 0);
Divide::calc();
for (int i = 1; i <= q; ++i) write(Bas[i] + Ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 42536kb
input:
97 99 1000 763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...
output:
-11203888980 -2626545525 3263167662 -4656032656 7328394880 9197277452 13843999329 5351141274 -7036382637 -2417217418 -10923605843 -7297456374 -7153724068 6796538755 -520826462 836615407 -2035500001 4264571339 4780073659 6041114652 1200317933 3082206799 7974972013 10961011856 5824045178 -2812669435 1...
result:
wrong answer 1st lines differ - expected: '6130845802041', found: '-11203888980'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #20:
score: 0
Wrong Answer
time: 2704ms
memory: 142400kb
input:
200000 20 200000 679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...
output:
941435620 279857936 403059804 371544374 542000844 753057472 97870732 448355845 208910150 984523556 442140087 862762567 343228700 368700872 226335001 588630338 204547511 604918277 1063071080 283187558 645192085 295317572 732052614 573783312 407472333 520676007 911305289 1035734835 785641331 709117921...
result:
wrong answer 1st lines differ - expected: '920563165', found: '941435620'
Subtask #4:
score: 0
Dangerous Syscalls
Test #28:
score: 0
Dangerous Syscalls
input:
200000 1000000000 200000 28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...
output:
result:
Subtask #5:
score: 0
Runtime Error
Test #36:
score: 0
Runtime Error
input:
199918 1000000000 199903 1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...
output:
result:
Subtask #6:
score: 0
Runtime Error
Test #43:
score: 0
Runtime Error
input:
200000 1000000000 200000 81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...
output:
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%