Submission #2042861


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 &centroid : 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, true);
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1, false);
        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));
  }

  void lower_bound_left(int a, int b, int &x, int k, int l, int r)
  {
    if(a >= r || b <= l) return;
    if(seg[k].v > x) return;
    if(l + 1 >= r) {
      x %= seg[k].v;
      return;
    }
    lower_bound_left(a, b, x, 2 * k + 1, l, (l + r) >> 1);
    lower_bound_left(a, b, x, 2 * k + 2, (l + r) >> 1, r);
  }

  void lower_bound_left(int a, int b, int &x)
  {
    return (lower_bound_left(a, b, x, 0, 0, sz));
  }

  void lower_bound_right(int a, int b, int &x, int k, int l, int r)
  {
    if(a >= r || b <= l) return;
    if(seg[k].v > x) return;
    if(l + 1 >= r) {
      x %= seg[k].v;
      return;
    }
    lower_bound_right(a, b, x, 2 * k + 2, (l + r) >> 1, r);
    lower_bound_right(a, b, x, 2 * k + 1, l, (l + r) >> 1);
  }

  void 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;
      if(c) seg.lower_bound_right(a, b, x);
      else seg.lower_bound_left(a, b, x);
    }
    printf("%d\n", x);
  }
}

Submission Info

Submission Time
Task J - MODクエリ
User ei13333
Language C++14 (GCC 5.4.1)
Score 400
Code Size 8775 Byte
Status AC
Exec Time 264 ms
Memory 18544 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:305: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:307:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &A[i]);
                       ^
./Main.cpp:312: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:324: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 370 / 370
Status
AC × 16
AC × 38
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 118 ms 9412 KB
17_path_11.txt AC 118 ms 9412 KB
17_path_12.txt AC 118 ms 9412 KB
17_path_13.txt AC 117 ms 9412 KB
17_path_14.txt AC 118 ms 9412 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 AC 1 ms 256 KB
30_random_21.txt AC 1 ms 256 KB
30_random_22.txt AC 1 ms 256 KB
30_random_23.txt AC 1 ms 256 KB
40_random_24.txt AC 261 ms 14224 KB
40_random_25.txt AC 256 ms 14096 KB
40_random_26.txt AC 264 ms 13712 KB
49_sample_02.txt AC 1 ms 256 KB
61_test_27.txt AC 131 ms 12548 KB
61_test_28.txt AC 130 ms 13308 KB
61_test_29.txt AC 137 ms 12436 KB
63_star_30.txt AC 166 ms 18544 KB
63_star_31.txt AC 176 ms 17520 KB
63_star_32.txt AC 169 ms 17392 KB
64_three_33.txt AC 135 ms 9620 KB
64_three_34.txt AC 138 ms 9556 KB
64_three_35.txt AC 137 ms 9528 KB