QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#721117 | #9492. 树上简单求和 | liujiameng | 5 | 1802ms | 91332kb | C++14 | 4.0kb | 2024-11-07 15:18:37 | 2024-11-07 15:18:38 |
Judging History
answer
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N = 2e5 + 10, D = 18;
vector<int>g1[N],g2[N];
int n;
int sqn, dl[N], dr[N], dfn, l[N], r[N], bel[N], fa[N][D+1], dep1[N];
ull a1[N], a2[N];
void dfs(int x, int f)
{
dl[x] = ++dfn, fa[x][0] = f, dep1[x] = dep1[f] + 1;
for(int i=1;i<=D;i++) fa[x][i] = fa[fa[x][i-1]][i-1];
for(auto j:g1[x]) if(j!=f) dfs(j,x);
dr[x] = dfn;
}
int LCA(int x, int y)
{
if(dep1[x]>dep1[y]) swap(x,y);
int i;
for(i=D;i>=0;i--) if(dep1[fa[y][i]]>=dep1[x]) y = fa[y][i];
if(x==y) return x;
for(i=D;i>=0;i--) if(fa[x][i]!=fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
void init()
{
dfs(1,0);
sqn = (int)sqrt(n);
int i, j;
for(i=1;r[i-1]!=n;i++) l[i] = r[i-1] + 1, r[i] = min(l[i]+sqn-1,n);
sqn = i - 1;
for(i=1;i<=sqn;i++) for(j=l[i];j<=r[i];j++) bel[j] = i;
}
void add(int a, ull x)
{
a = dl[a];
int p = bel[a], i;
for(i=a;i<=r[p];i++) a1[i] += x;
for(i=p+1;i<=sqn;i++) a2[i] += x;
}
ull ask(int x)
{
return a1[dr[x]] + a2[bel[dr[x]]] - a1[dl[x]-1] - a2[bel[dl[x]-1]];
}
ull ans[N], val[N];
struct qq{int id,k,x;}qu[N*4],fd[N*4];int cq;
struct op{int x,id;ull y;}ad[N*8];int co;
void ins(int x, int id, ull y){add(x,y),ad[++co]={x,id,y};}
int cnt[N];
int dep[N], p[N], is[N], fa2[N], up[N], faa[N][D+1];
int LCA2(int x, int y)
{
int i;
if(dep[x]>dep[y]) swap(x,y);
for(i=D;i>=0;i--) if(dep[faa[y][i]]>=dep[x]) y = faa[y][i];
if(x==y) return x;
for(i=D;i>=0;i--) if(faa[x][i]!=faa[y][i]) x = faa[x][i], y = faa[y][i];
return faa[x][0];
}
void dfs2(int x, int f)
{
dep[x] = dep[f] + 1, fa2[x] = faa[x][0] = f;
for(int i=1;i<=D;i++) faa[x][i] = faa[faa[x][i-1]][i-1];
for(auto j:g2[x]) if(j!=f) dfs2(j,x);
}
void init2()
{
int sqn = (int)sqrt(n);
sqn = sqn * 2;
dfs2(1,0);
int i;
for(i=1;i<=n;i++) p[i] = i;
sort(&p[1],&p[n]+1,[](int a,int b){return dep[a]>dep[b];});
for(i=n-1;i>=1;i--)
{
int x = p[i], y = fa2[x], z = sqn;
while(y>1&&z)
{
if(is[y]) break;
y = fa2[y], z--;
}
if(!z) is[y] = 1;
}
is[1] = 1;
for(i=2;i<=n;i++) if(is[i])
{
int x = fa2[i];
while(!is[x]) x = fa2[x];
up[i] = x;
}
}
void dfsp(int x, int f)
{
for(auto j:g1[x]) if(j!=f) cnt[j] += cnt[x], dfsp(j,x);
}
int ww[N];
int main()
{
int m, i, a, b;
scanf("%d%d", &n, &m);
for(i=1;i<=n;i++) scanf("%llu", &val[i]);
for(i=1;i<n;i++) scanf("%d%d", &a, &b), g1[a].emplace_back(b), g1[b].emplace_back(a);
for(i=1;i<n;i++) scanf("%d%d", &a, &b), g2[a].emplace_back(b), g2[b].emplace_back(a);
init(), init2();
for(i=1;i<=n;i++)
{
ins(i,0,val[i]);
if(i>1) ins(fa[i][0],0,-val[i]);
}
for(i=1;i<=m;i++)
{
int x, y, xx, yy, l;
ull k;
scanf("%d%d%llu", &x, &y, &k), xx = x, yy = y;
l = LCA(x,y), ins(x,i,k), ins(y,i,k), ins(l,i,-k);
if(l!=1) ins(fa[l][0],i,-k);
l = LCA2(x,y);
while(!is[x]&&x!=l) ans[i] += ask(x), x = fa2[x];
while(!is[y]&&y!=l) ans[i] += ask(y), y = fa2[y];
int x1 = x, y1 = y;
while(dep[up[x]]>=dep[l]) x = up[x];
while(dep[up[y]]>=dep[l]) y = up[y];
if(x!=x1) qu[++cq] = {i,1,x1}, qu[++cq] = {i,-1,x};
if(y!=y1) qu[++cq] = {i,1,y1}, qu[++cq] = {i,-1,y};
if(dep[x]>dep[y]) swap(x,y);
while(dep[x]<dep[y]) ans[i] += ask(y), y = fa2[y];
while(x!=y) ans[i] += ask(x) + ask(y), x = fa2[x], y = fa2[y];
ans[i] += ask(l);
}
for(i=1;i<=cq;i++) ww[qu[i].x]++;
for(i=1;i<=n;i++) ww[i] += ww[i-1];
for(i=cq;i>=1;i--) fd[ww[qu[i].x]--] = qu[i];
for(i=1;i<=cq;i++) qu[i] = fd[i];
for(i=1;i<cq;i++) assert(qu[i].x<qu[i+1].x||(qu[i].x==qu[i+1].x&&qu[i].id<=qu[i+1].id));
int l, r;
for(i=1;i<=cq;i=r+1)
{
l = i, r = i;
int x, j;
ull res = 0;
while(r<=cq&&qu[r].x==qu[l].x) r++;
r--, x = qu[l].x;
for(i=1;i<=n;i++) cnt[i] = 0;
for(i=x;i;i=fa2[i]) cnt[i] = 1;
dfsp(1,0);
for(i=l,j=1;i<=r;i++)
{
while(j<=co&&ad[j].id<=qu[i].id) res += ad[j].y * cnt[ad[j].x], j++;
ans[qu[i].id] += res * qu[i].k;
}
}
for(i=1;i<=m;i++) printf("%llu\n", ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 4ms
memory: 29884kb
input:
3000 3000 7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...
output:
12105153858659381124 18367442707572066757 11668241962484097878 11288238120352358472 1742468310074622166 9942835997686093671 3305677510569607477 17741602000425004088 14984128302052618266 1075081718074605786 6509217537832509095 16750513627843273113 8569443169249732820 14475184194298579044 156111071108...
result:
ok 3000 lines
Test #2:
score: 5
Accepted
time: 12ms
memory: 25016kb
input:
3000 3000 1612333876155866602 8538417838700679227 6080765231437578796 17905224638340228394 12270907925903144224 17944105326358594564 17302041033966840611 1006351124625222126 496336153231744288 9393087977687876980 9553975238547373621 9361882717200384390 15051881329169144319 9757999873162420435 882725...
output:
11133131376095771981 7909873024850695144 16250639243139481926 14562550655578101207 8274205996508264973 178549413271904466 2368406276743327913 7464009386554813982 9439464815411774627 1471756740732097060 15201641099137019227 6774030298556871576 18156105511913219667 1553508745644446823 4225137078364117...
result:
ok 3000 lines
Test #3:
score: 5
Accepted
time: 82ms
memory: 27036kb
input:
3000 3000 9709246061666095435 1861649101703072889 10620139893353930613 17635186539135419482 710209455559527146 6075101384669982511 1120305006358459674 9703156967435388252 1397046737759839382 5259056712870179169 8253156305433022999 710199693203327302 15130650033641744675 10720111924616886955 15543351...
output:
7834604406305153073 5037061270969117785 16481572776620825702 15177894197606565804 3120320619896892806 18008650876379132344 7417108723176816402 13515164814425439399 3299769942258542105 15897528270699011770 11642805469843844638 16764682282380318054 4824039114054405772 4859834102876213962 1234210473247...
result:
ok 3000 lines
Test #4:
score: 5
Accepted
time: 4ms
memory: 22580kb
input:
3000 3000 16538965545220923528 18062192327708400751 10422465150728338588 3471522151129113073 1236650672072793692 1942240200040301168 13090729759591037952 15335798523677372669 9912100622761466753 11177948788405690381 3710859061697501523 4984944638666762977 17278589713462878008 6371292801024547050 868...
output:
8182453933067329108 13535217473847106938 17067385337010269798 3806121648880466130 11322569288575153037 11079197311131660121 9670138330007803226 6554062218199796758 965954569567598779 18055887214749050688 6142620503089407421 8690117812667761187 9547139298346295115 8890987597519353054 1755036654049586...
result:
ok 3000 lines
Test #5:
score: 5
Accepted
time: 61ms
memory: 29820kb
input:
3000 3000 17759588706587888497 10550000524636484378 11601004513528075994 7150322911283804521 4459707248078569712 10692395730842402625 8940418793863522991 12967068928670540447 9954278250450015940 13702413838608801301 10598390500439869870 15110245227553613794 490862872212325709 15164980555660957366 94...
output:
9743736929788175512 16812303667256960040 14694223512340829897 550204232580650311 1175342872438242313 17622261358285047637 7413682703975031220 12643066512274062227 1868985217436232595 5471830334855681322 8070132260376389587 3970361922096052085 218281824643752746 991917103472727104 2960248244218479023...
result:
ok 3000 lines
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #6:
score: 12
Accepted
time: 0ms
memory: 22400kb
input:
5 7 0 3 2 6 4 1 2 2 4 1 5 5 3 3 4 4 2 2 5 5 1 5 3 0 3 2 5 4 4 4 4 4 3 5 2 0 3 4 3 5 5 6
output:
15 21 10 13 17 26 18
result:
ok 7 lines
Test #7:
score: 12
Accepted
time: 481ms
memory: 50924kb
input:
70000 70000 3805295436278888199 9842309351516174725 1566744796319231180 2206519284152256579 2715928675931950447 6346821976624501261 16020972671480798719 14702021753902144915 17127828773798978442 15779168055669690475 4964561323934614661 9395102787554964450 6377076753365184543 15167378195767668817 288...
output:
5971729064136092190 6457394048987305727 13604212649915736394 8639973959364892219 437861319070967556 16133076880026962355 5384937395694479961 4591478439775690843 16071919565966962790 15485626634068969082 10235993901046758372 3449528613427081475 8064280362779764074 12784984512326434905 424951714880051...
result:
ok 70000 lines
Test #8:
score: 0
Time Limit Exceeded
input:
70000 70000 17769190865915081913 3772925482507158804 10559962993069063712 16307277356502651642 12014171661057147061 1923543107882042577 13408785599350410314 17786178374951015816 2038922879833426794 2540043772647346461 15419977514837351390 5175974305273838292 16815288359165841441 6295059675346852046 ...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #21:
score: 14
Accepted
time: 1802ms
memory: 91332kb
input:
200000 200000 622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...
output:
9042998055336671259 11611293489264521142 5835924579879681322 9187084356907537870 17810346410706951073 565636160725988981 837626748701483168 16059573289829807099 7246210357888652619 7725251776483176497 17088098442183693937 9042305714006927228 10907378739216215456 6526772063609981609 51578202456469609...
result:
ok 200000 lines
Test #22:
score: 0
Time Limit Exceeded
input:
200000 200000 13175752638648662841 17926176333479943540 18069418271192836667 7662981418770774166 17432280672869071045 9361466030141569604 17336291298429915451 758279154724011577 10229986883918215412 16695796270233481895 1104033984864960726 9768530369533627193 7121962912997584423 8574667967472399164 ...
output:
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #27:
score: 0
Time Limit Exceeded
input:
200000 200000 1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...
output:
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #34:
score: 0
Time Limit Exceeded
input:
200000 200000 6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...
output:
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%