Submission #2042849
Source Code Expand
#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)
{
auto p = Information(a);
return (index[p.first] + p.second);
}
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);
}
inline vector< tuple< int, int, bool > > get_path(int a, int b)
{
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
vector< tuple< int, int, bool > > vs, ws;
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1, false);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1, true);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
bool rev = true;
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB), rev = false;
vs.emplace_back(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1, !rev);
reverse(begin(ws), end(ws));
copy(begin(ws), end(ws), back_inserter(vs));
return (vs);
}
};
struct SegNode
{
int v;
SegNode(int v) : v(v) {}
SegNode operator*(const SegNode &r) const
{
return (v < r.v ? *this : r);
}
} e(1 << 30);
struct SegmentTree
{
int sz;
vector< SegNode > seg;
SegmentTree(int n)
{
sz = 1;
while(sz < n) sz <<= 1;
seg.assign(2 * sz - 1, e);
}
void update(int k, const SegNode &x)
{
k += sz - 1;
seg[k] = x;
while(k > 0) {
k = (k - 1) >> 1;
seg[k] = seg[2 * k + 1] * seg[2 * k + 2];
}
}
SegNode query(int a, int b, int k, int l, int r)
{
if(a >= r || b <= l) return (e);
if(a <= l && r <= b) return (seg[k]);
return (query(a, b, 2 * k + 1, l, (l + r) >> 1) * query(a, b, 2 * k + 2, (l + r) >> 1, r));
}
SegNode query(int a, int b)
{
return (query(a, b, 0, 0, sz));
}
int lower_bound_left(int a, int b, int x, int k, int l, int r)
{
if(a >= r || b <= l) return (-1);
if(seg[k].v > x) return (-1);
if(l + 1 >= r) return (l);
auto p = lower_bound_left(a, b, x, 2 * k + 1, l, (l + r) >> 1);
if(~p) return (p);
return (lower_bound_left(a, b, x, 2 * k + 2, (l + r) >> 1, r));
}
int lower_bound_left(int a, int b, int x)
{
return (lower_bound_left(a, b, x, 0, 0, sz));
}
int lower_bound_right(int a, int b, int x, int k, int l, int r)
{
if(a >= r || b <= l) return (-1);
if(seg[k].v > x) return (-1);
if(l + 1 >= r) return (l);
auto p = lower_bound_right(a, b, x, 2 * k + 2, (l + r) >> 1, r);
if(~p) return (p);
return (lower_bound_right(a, b, x, 2 * k + 1, l, (l + r) >> 1));
}
int lower_bound_right(int a, int b, int x)
{
return (lower_bound_right(a, b, x, 0, 0, sz));
}
};
int main()
{
int N, Q, A[100000];
scanf("%d %d", &N, &Q);
for(int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
TreeArray tree(N);
for(int i = 1; i < N; i++) {
int a, b;
scanf("%d %d", &a, &b);
tree.AddEdge(--a, --b);
}
tree.Build();
SegmentTree seg(N);
int rev[100000];
for(int i = 0; i < N; i++) {
seg.update(tree.get(i), A[i]);
rev[tree.get(i)] = A[i];
}
for(int i = 0; i < Q; i++) {
int x, v, w;
scanf("%d %d %d", &x, &v, &w);
--v, --w;
auto path = tree.get_path(v, w);
for(auto &p : path) {
int a, b, c, buff;
tie(a, b, c) = p;
while((buff = (c ? seg.lower_bound_right(a, b, x) : seg.lower_bound_left(a, b, x))) >= 0) {
x %= rev[buff];
if(c) b = buff;
else a = buff + 1;
}
}
printf("%d\n", x);
}
}
Submission Info
Submission Time |
|
Task |
J - MODクエリ |
User |
ei13333 |
Language |
C++14 (GCC 5.4.1) |
Score |
30 |
Code Size |
8901 Byte |
Status |
WA |
Exec Time |
279 ms |
Memory |
17776 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:301:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &Q);
^
./Main.cpp:303:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
^
./Main.cpp:308:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
^
./Main.cpp:320:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &v, &w);
^
Judge Result
Set Name |
Small |
All |
Score / Max Score |
30 / 30 |
0 / 370 |
Status |
|
|
Set Name |
Test Cases |
Small |
00_sample_path_01.txt, 15_path_00.txt, 15_path_01.txt, 15_path_02.txt, 15_path_03.txt, 15_path_04.txt, 16_path_05.txt, 16_path_06.txt, 16_path_07.txt, 16_path_08.txt, 16_path_09.txt, 17_path_10.txt, 17_path_11.txt, 17_path_12.txt, 17_path_13.txt, 17_path_14.txt |
All |
00_sample_path_01.txt, 15_path_00.txt, 15_path_01.txt, 15_path_02.txt, 15_path_03.txt, 15_path_04.txt, 16_path_05.txt, 16_path_06.txt, 16_path_07.txt, 16_path_08.txt, 16_path_09.txt, 17_path_10.txt, 17_path_11.txt, 17_path_12.txt, 17_path_13.txt, 17_path_14.txt, 20_random_15.txt, 20_random_16.txt, 20_random_17.txt, 21_random_18.txt, 21_random_19.txt, 21_random_20.txt, 30_random_21.txt, 30_random_22.txt, 30_random_23.txt, 40_random_24.txt, 40_random_25.txt, 40_random_26.txt, 49_sample_02.txt, 61_test_27.txt, 61_test_28.txt, 61_test_29.txt, 63_star_30.txt, 63_star_31.txt, 63_star_32.txt, 64_three_33.txt, 64_three_34.txt, 64_three_35.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_path_01.txt |
AC |
1 ms |
256 KB |
15_path_00.txt |
AC |
1 ms |
256 KB |
15_path_01.txt |
AC |
1 ms |
256 KB |
15_path_02.txt |
AC |
1 ms |
256 KB |
15_path_03.txt |
AC |
1 ms |
256 KB |
15_path_04.txt |
AC |
1 ms |
256 KB |
16_path_05.txt |
AC |
1 ms |
256 KB |
16_path_06.txt |
AC |
1 ms |
256 KB |
16_path_07.txt |
AC |
1 ms |
256 KB |
16_path_08.txt |
AC |
1 ms |
256 KB |
16_path_09.txt |
AC |
1 ms |
256 KB |
17_path_10.txt |
AC |
128 ms |
9796 KB |
17_path_11.txt |
AC |
128 ms |
9796 KB |
17_path_12.txt |
AC |
129 ms |
9796 KB |
17_path_13.txt |
AC |
130 ms |
9924 KB |
17_path_14.txt |
AC |
128 ms |
9796 KB |
20_random_15.txt |
AC |
1 ms |
256 KB |
20_random_16.txt |
AC |
1 ms |
256 KB |
20_random_17.txt |
AC |
1 ms |
256 KB |
21_random_18.txt |
AC |
1 ms |
256 KB |
21_random_19.txt |
AC |
1 ms |
256 KB |
21_random_20.txt |
WA |
1 ms |
256 KB |
30_random_21.txt |
WA |
1 ms |
256 KB |
30_random_22.txt |
WA |
1 ms |
256 KB |
30_random_23.txt |
WA |
1 ms |
256 KB |
40_random_24.txt |
WA |
274 ms |
14224 KB |
40_random_25.txt |
WA |
279 ms |
14096 KB |
40_random_26.txt |
WA |
276 ms |
14096 KB |
49_sample_02.txt |
WA |
1 ms |
256 KB |
61_test_27.txt |
AC |
138 ms |
12932 KB |
61_test_28.txt |
AC |
134 ms |
12924 KB |
61_test_29.txt |
WA |
139 ms |
12948 KB |
63_star_30.txt |
AC |
177 ms |
17776 KB |
63_star_31.txt |
AC |
176 ms |
17776 KB |
63_star_32.txt |
AC |
177 ms |
17776 KB |
64_three_33.txt |
AC |
143 ms |
10004 KB |
64_three_34.txt |
AC |
141 ms |
9940 KB |
64_three_35.txt |
AC |
144 ms |
9912 KB |