QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#410582 | #6662. 기지 간소화 | tuanlinh123 | 0 | 570ms | 101824kb | C++20 | 3.2kb | 2024-05-14 10:10:29 | 2024-05-14 10:10:29 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=300005, Mod=1e9+7;
struct SegTree
{
struct Node
{
ll sum=0, pre=0, suf=0, len=0;
Node(){}
Node (ll x): sum(x), pre(x), suf(x), len(1){}
Node operator + (const Node &o) const
{
Node ans;
ans.len=len+o.len;
ans.sum=sum+o.sum+suf*o.pre;
ans.pre=pre==len?pre+o.pre:pre;
ans.suf=o.suf==o.len?o.suf+suf:o.suf;
return ans;
}
};
ll n;
vector <Node> St;
void build(ll i, ll Start, ll End, ll v)
{
if (Start==End) {St[i]=Node(v); return;}
ll mid=(Start+End)/2;
build(i*2, Start, mid, v);
build(i*2+1, mid+1, End, v);
St[i]=St[i*2]+St[i*2+1];
}
SegTree(ll n, ll v): n(n){St.resize(n*4+1), build(1, 0, n-1, v);}
void update(ll i, ll Start, ll End, ll idx, ll v)
{
if (Start==End) {St[i]=Node(v); return;}
ll mid=(Start+End)/2;
if (mid>=idx) update(i*2, Start, mid, idx, v);
else update(i*2+1, mid+1, End, idx, v);
St[i]=St[i*2]+St[i*2+1];
}
void update(ll idx, ll v){update(1, 0, n-1, idx, v);}
ll query(){return St[1].sum;}
};
int maintenance_costs_sum(std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
ll n=sz(W)+1;
vector <vector <pll>> A(n);
for (ll i=0; i<sz(W); i++)
A[U[i]].pb({V[i], W[i]}), A[V[i]].pb({U[i], W[i]});
// all nodes in subtree i is marked with val, all nodes outside marked with val^1
auto solve=[&](ll val)
{
SegTree S(n, val^1);
vector <ll> ans(n, 0), child(n, 0), big(n, -1);
function <void(ll, ll)> predfs=[&](ll u, ll pa)
{
child[u]=1;
for (auto [v, w]:A[u])
{
if (v==pa) continue;
predfs(v, u), child[u]+=child[v];
if (big[u]==-1 || child[big[u]]<child[v]) big[u]=v;
}
}; predfs(0, 0);
function <void(ll, ll, ll)> mark=[&](ll u, ll pa, ll c)
{
S.update(u, c);
for (auto [v, w]:A[u])
if (v!=pa)
mark(v, u, c);
};
function <void(ll, ll)> calc=[&](ll u, ll pa)
{
for (auto [v, w]:A[u])
if (v!=pa && v!=big[u])
calc(v, u), mark(v, u, val^1);
if (big[u]!=-1) calc(big[u], u);
for (auto [v, w]:A[u])
if (v!=pa && v!=big[u])
mark(v, u, val);
S.update(u, val);
ans[u]=S.query();
}; calc(0, 0);
return ans;
};
ll answer=0;
vector <ll> cnt[2];
cnt[0]=solve(0), cnt[1]=solve(1);
function <void(ll, ll)> getans=[&](ll u, ll pa)
{
for (auto [v, w]:A[u])
if (v!=pa)
{
answer+=w*(n*(n+1)/2-cnt[0][v]-cnt[1][v]);
getans(v, u);
}
}; getans(0, 0);
return answer;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 0ms
memory: 4072kb
input:
2 1 0 1
output:
1
result:
ok single line: '1'
Test #2:
score: -5
Wrong Answer
time: 1ms
memory: 3904kb
input:
299 72 263 662908099 170 230 583287081 181 87 461245480 117 116 41098264 282 218 300936390 238 128 561969086 175 105 305200697 152 28 927649982 211 58 290232523 233 188 304617152 246 74 61325892 283 120 724838352 207 94 123021801 138 241 893659708 171 283 82846115 104 250 142703714 111 63 547249068 ...
output:
1721042624
result:
wrong answer 1st lines differ - expected: '93964028', found: '1721042624'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 570ms
memory: 67392kb
input:
249494 137621 137622 1 198127 198128 1 117358 117359 1 155777 155778 1 112742 112743 1 235668 235669 1 20632 20622 1 41333 41334 1 170699 170692 1 130269 130268 1 137208 137207 1 37791 37767 1 130187 130178 1 89795 89817 1 91408 91359 1 243301 243574 1 22017 22018 1 116466 116467 1 160094 160095 1 4...
output:
244734745
result:
wrong answer 1st lines differ - expected: '714152529', found: '244734745'
Subtask #4:
score: 0
Wrong Answer
Test #48:
score: 0
Wrong Answer
time: 416ms
memory: 101824kb
input:
249876 133760 107716 545485826 57898 35556 542825636 159559 78814 588304799 9037 105623 735470824 242676 240955 258616989 58653 143501 194781066 36591 97835 699376285 95049 123298 35300031 91751 203920 865003045 53908 18382 376452723 211462 200538 638209824 48693 89388 132147210 238496 151742 322726...
output:
-932666363
result:
wrong answer 1st lines differ - expected: '497361697', found: '-932666363'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%