結果
| 問題 | No.399 動的な領主 |
| コンテスト | |
| ユーザー |
ei1333333
|
| 提出日時 | 2017-10-31 10:54:21 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 153 ms / 2,000 ms |
| コード長 | 5,996 bytes |
| コンパイル時間 | 2,861 ms |
| コンパイル使用メモリ | 214,084 KB |
| 最終ジャッジ日時 | 2025-01-05 03:41:01 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:228:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
228 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:232:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
232 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:237:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
237 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
main.cpp:240:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
240 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct CentroidPathDecomposition
{
struct Centroid
{
int ParIndex, ParDepth, Deep;
vector< int > node;
Centroid(int idx, int dep, int deep) : ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size()
{
return (node.size());
}
inline int &operator[](int k)
{
return (node[k]);
}
inline pair< int, int > Up()
{
return (make_pair(ParIndex, ParDepth));
}
};
vector< vector< int > > graph;
vector< int > SubTreeSize, NextPath;
vector< int > TreeIndex, TreeDepth;
vector< Centroid > Centroids;
void BuildSubTreeSize()
{
stack< pair< int, int > > s;
s.emplace(0, -1);
while(!s.empty()) {
auto p = s.top();
s.pop();
if(~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
SubTreeSize[p.first] += SubTreeSize[to];
if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for(auto &to : graph[p.first]) {
if(p.second != to) s.emplace(to, p.first);
}
}
}
}
void BuildPath()
{
stack< pair< int, int > > s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while(!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int) Centroids[TreeIndex[p.first]].size();
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
if(to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int) Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
void AddEdge(int x, int y)
{
graph[x].push_back(y);
graph[y].push_back(x);
}
virtual void Build()
{
BuildSubTreeSize();
BuildPath();
}
inline size_t size()
{
return (Centroids.size());
}
inline pair< int, int > Information(int idx)
{
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k)
{
return (Centroids[k]);
}
inline int LCA(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b, const function< void(int, int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(int SZ)
{
graph.resize(SZ);
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
struct TreeArray : CentroidPathDecomposition
{
TreeArray(int sz) : CentroidPathDecomposition(sz) {}
vector< int > index;
void Build()
{
CentroidPathDecomposition::Build();
int ptr = 0;
for(auto ¢roid : Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a)
{
return (index[a]);
}
inline void query(int a, int b, const function< void(int, int) > &f)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
}
};
template< class T >
struct CumulativeSum
{
vector< T > data;
CumulativeSum(int sz) : data(sz, 0) {};
void add(int k, int x)
{
data[k] += x;
}
void build()
{
for(int i = 1; i < data.size(); i++) {
data[i] += data[i - 1];
}
}
T query(int k)
{
if(k < 0) return (0);
return (data[min(k, (int) data.size() - 1)]);
}
};
using int64 = long long;
int main()
{
int N, Q;
scanf("%d", &N);
TreeArray tree(N);
for(int i = 1; i < N; i++) {
int a, b;
scanf("%d %d", &a, &b);
tree.AddEdge(--a, --b);
}
tree.Build();
CumulativeSum< int > sum(N + 1);
scanf("%d", &Q);
for(int i = 0; i < Q; i++) {
int a, b;
scanf("%d %d", &a, &b);
tree.query(--a, --b, [&](int l, int r)
{
sum.add(l, 1);
sum.add(r, -1);
});
}
sum.build();
int64 ret = 0;
for(int i = 0; i < N; i++) {
ret += 1LL * sum.query(i) * (sum.query(i) + 1) / 2;
}
printf("%lld\n", ret);
}
ei1333333