QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#717096 | #9492. 树上简单求和 | konata2828 | Compile Error | / | / | C++98 | 3.7kb | 2024-11-06 16:52:05 | 2024-11-06 16:52:05 |
Judging History
answer
// Hydro submission #672b2e33a059678966637f83@1730883124509
#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()
{
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);
for(i=1;i<=sqn;i++) for(j=l[i];j<=r[i];j++) bel[j] = i;
sqn = i - 1;
}
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[r[x]] + a2[r[x]] - a1[l[x]-1] - a2[l[x]-1];
}
ull ans[N], val[N];
struct qq{int id,k,x;}qu[N*4];int cq;
struct op{int x,id;ull y;}ad[N*4];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];
void dfs2(int x, int f)
{
dep[x] = dep[f] + 1, fa2[x] = f;
for(auto j:g2[x]) if(j!=f) dfs2(j,x);
}
void init2()
{
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=1;i<n;i++) if(!is[p[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=1;i<n;i++) if(is[p[i]])
{
int x = p[i], z = fa2[x];
while(!is[z]) z = fa2[z];
up[x] = z;
}
}
void dfsp(int x, int f)
{
for(auto j:g2[x]) if(j!=f) cnt[j] += cnt[x], dfsp(j,x);
}
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]);
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), ins(fa[l][0],i,-k);
while(!is[x]) ans[i] += ask(x), x = fa2[x];
while(!is[y]) ans[i] += ask(y), y = fa2[y];
if(x==y)
{
ans[i] = 0;
x = xx, y = yy;
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(x);
continue;
}
qu[++cq] = {i,1,x}, qu[++cq] = {i,1,y};
while(up[x]!=up[y])
{
if(dep[up[x]]>dep[up[y]]) x = up[x];
else y = up[y];
}
qu[++cq] = {i,-1,x}, 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(x);
}
sort(&qu[1],&qu[cq]+1,[](qq a,qq b){return a.x<b.x;});
for(i=1;i<=cq;)
{
int l = i, r = i, 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);
sort(&qu[l],&qu[r]+1,[](qq a,qq b){return a.id<b.id;});
for(i=l,j=1;i<=r;i++)
{
while(ad[j].id<=qu[i].id) res += ad[j].y * cnt[ad[j].x];
ans[qu[j].id] += res * qu[j].k;
}
i = r + 1;
}
for(i=1;i<=n;i++) printf("%llu\n", ans[i]);
return 0;
}
详细
answer.code: In function ‘void dfs(int, int)’: answer.code:15:18: error: ‘j’ does not name a type 15 | for(auto j:g1[x]) if(j!=f) dfs(j,x); | ^ answer.code:15:45: error: expected ‘;’ before ‘dr’ 15 | for(auto j:g1[x]) if(j!=f) dfs(j,x); | ^ | ; 16 | dr[x] = dfn; | ~~ answer.code:17:1: error: expected primary-expression before ‘}’ token 17 | } | ^ answer.code:16:21: error: expected ‘)’ before ‘}’ token 16 | dr[x] = dfn; | ^ | ) 17 | } | ~ answer.code:15:12: note: to match this ‘(’ 15 | for(auto j:g1[x]) if(j!=f) dfs(j,x); | ^ answer.code:17:1: error: expected primary-expression before ‘}’ token 17 | } | ^ answer.code: In function ‘void ins(int, int, long long unsigned int)’: answer.code:49:57: warning: extended initializer lists only available with ‘-std=c++11’ or ‘-std=gnu++11’ [-Wc++11-extensions] 49 | void ins(int x, int id, ull y){add(x,y),ad[++co]={x,id,y};} | ^ answer.code:49:57: warning: extended initializer lists only available with ‘-std=c++11’ or ‘-std=gnu++11’ [-Wc++11-extensions] answer.code: In function ‘void dfs2(int, int)’: answer.code:56:18: error: ‘j’ does not name a type 56 | for(auto j:g2[x]) if(j!=f) dfs2(j,x); | ^ answer.code:56:46: error: expected ‘;’ before ‘}’ token 56 | for(auto j:g2[x]) if(j!=f) dfs2(j,x); | ^ | ; 57 | } | ~ answer.code:57:1: error: expected primary-expression before ‘}’ token 57 | } | ^ answer.code:56:46: error: expected ‘;’ before ‘}’ token 56 | for(auto j:g2[x]) if(j!=f) dfs2(j,x); | ^ | ; 57 | } | ~ answer.code:57:1: error: expected primary-expression before ‘}’ token 57 | } | ^ answer.code:56:46: error: expected ‘)’ before ‘}’ token 56 | for(auto j:g2[x]) if(j!=f) dfs2(j,x); | ~ ^ | ) 57 | } | ~ answer.code:57:1: error: expected primary-expression before ‘}’ token 57 | } | ^ answer.code: In function ‘void init2()’: answer.code:63:65: warning: lambda expressions only available with ‘-std=c++11’ or ‘-std=gnu++11’ [-Wc++11-extensions] 63 | sort(&p[1],&p[n]+1,[](int a,int b){return dep[a]>dep[b];}); | ^ answer.code:63:13: error: no matching function for call to ‘sort(int*, int*, init2()::<lambda(int, int)>)’ 63 | sort(&p[1],&p[n]+1,[](int a,int b){return dep[a]>dep[b];}); | ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/13/algorithm:61, from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:51, from answer.code:2: /usr/include/c++/13/bits/stl_algo.h:4851:5: note: candidate: ‘template<class _RAIter> void std::sort(_RAIter, _RAIter)’ 4851 | sort(_RandomAccessIterator __first, _RandomAccessIterator __last) | ^~~~ /usr/include/c++/13/bits/stl_algo.h:4851:5: note: template argument deduction/substitution failed: answer.code:63:13: note: candidate expects 2 arguments, 3 provided 63 | sort(&p[1],&p[n]+1,[](int a,int b){return dep[a]>dep[b];}); | ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/include/c++/13/bits/stl_algo.h:4882:5: note: candidate: ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’ 4882 | sort(_RandomAccessIterator __first, _RandomAccessIterator __last, | ^~~~ /usr/include/c++/13/bits/stl_algo.h:4882:5: note: template argument deduction/substitution failed: answer.code: In substitution of ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int*; _Compare = init2()::<lambda(int, int)>]’: answer.code:63:6: required from here answer.code:63:13: error: template argument for ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’ uses local type ‘init2()::<lambda(int, int)>’ 63 | sort(&p[1],&p[n]+1,[](int a,int b){return dep[a]>dep[b];}); | ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ answer.code:63:13: error: trying to instantiate ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _...