結果

問題 No.1030 だんしんぐぱーりない
ユーザー LayCurseLayCurse
提出日時 2020-04-19 10:30:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 11,722 bytes
コンパイル時間 2,705 ms
コンパイル使用メモリ 218,048 KB
実行使用メモリ 16,284 KB
最終ジャッジ日時 2024-04-15 05:32:31
合計ジャッジ時間 7,869 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
7,628 KB
testcase_02 AC 2 ms
7,744 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 47 ms
12,912 KB
testcase_06 AC 37 ms
12,768 KB
testcase_07 AC 27 ms
9,952 KB
testcase_08 AC 26 ms
10,308 KB
testcase_09 AC 39 ms
15,940 KB
testcase_10 AC 19 ms
9,036 KB
testcase_11 AC 42 ms
11,476 KB
testcase_12 AC 41 ms
15,384 KB
testcase_13 AC 35 ms
12,616 KB
testcase_14 AC 47 ms
13,708 KB
testcase_15 AC 23 ms
7,876 KB
testcase_16 AC 39 ms
11,084 KB
testcase_17 AC 42 ms
14,960 KB
testcase_18 AC 49 ms
13,624 KB
testcase_19 AC 29 ms
10,584 KB
testcase_20 AC 31 ms
13,076 KB
testcase_21 AC 33 ms
12,272 KB
testcase_22 AC 32 ms
12,636 KB
testcase_23 AC 38 ms
9,928 KB
testcase_24 AC 30 ms
8,004 KB
testcase_25 AC 35 ms
13,512 KB
testcase_26 AC 24 ms
6,980 KB
testcase_27 AC 27 ms
7,492 KB
testcase_28 AC 42 ms
13,384 KB
testcase_29 AC 30 ms
13,248 KB
testcase_30 AC 30 ms
9,844 KB
testcase_31 AC 32 ms
9,740 KB
testcase_32 AC 38 ms
13,680 KB
testcase_33 AC 40 ms
13,592 KB
testcase_34 AC 19 ms
7,496 KB
testcase_35 AC 62 ms
16,284 KB
testcase_36 AC 62 ms
15,392 KB
testcase_37 AC 62 ms
15,392 KB
testcase_38 AC 61 ms
16,156 KB
testcase_39 AC 61 ms
14,100 KB
testcase_40 AC 1 ms
6,940 KB
testcase_41 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:468:11: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
  468 |       wt_L(cmx[t.get(T[1], T[2]+1)]);
      |       ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:526:5: note: 'res' was declared here
  526 |   T res;
      |     ^~~

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class T> struct segtree_pg{
  int N;
  int logN;
  T *val;
  void malloc(int maxN, int once = 0);
  void walloc(int maxN, int once = 0, void **mem = &wmem);
  void free(void);
  T& operator[](int i);
  void setN(int n, int zerofill = 1, int dobuild = 1);
  void build(void);
  inline void build(int a);
  inline void change(int a, T v);
  inline T get(int a, int b);
}
;
struct graph{
  int N;
  int *es;
  int **edge;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
  template<class S1, class S2> void getDistTree_WeightedNode_max(int root, S1 w[], S2 res[], void *mem = wmem){
    int i;
    int j;
    int k;
    int m;
    int *q;
    int qs = 0;
    int qe = 1;
    char *vis;
    walloc1d(&q,N,&mem);
    walloc1d(&vis,N,&mem);
    for(i=(0);i<(N);i++){
      vis[i] = 0;
    }
    vis[root] = 1;
    res[root] = w[root];
    q[0] = root;
    while(qs < qe){
      i = q[qs++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(vis[k]){
          continue;
        }
        res[k] =max_L(w[k], res[i]);
        q[qe++] = k;
        vis[k] = 1;
      }
    }
  }
}
;
struct HLD{
  int N;
  int *es;
  int **edge;
  int *group;
  int *groupind;
  int groupNum;
  int *groupSize;
  int **groupNode;
  int *groupUpNode;
  int *groupDepth;
  void init(graph g, void **mem = &wmem){
    init(g.N, g.es, g.edge, mem);
  }
  void init(int N__, int *es__, int **edge__, void **mem = &wmem){
    int i;
    int j;
    int k;
    int x;
    int y;
    int mx;
    int *q;
    int q_st;
    int q_ed;
    int *sz;
    char *vis;
    void *tmpmem;
    N = N__;
    es = es__;
    edge = edge__;
    walloc1d(&group, N, mem);
    walloc1d(&groupind, N, mem);
    tmpmem = *mem;
    walloc1d(&q, N, &tmpmem);
    walloc1d(&sz, N, &tmpmem);
    walloc1d(&vis, N, &tmpmem);
    for(i=(0);i<(N);i++){
      vis[i] = 0;
    }
    q_st = 0;
    q_ed = 1;
    q[0] = 0;
    vis[0] = 1;
    while(q_st < q_ed){
      i = q[q_st++];
      for(j=(0);j<(es[i]);j++){
        k = edge[i][j];
        if(!vis[k]){
          vis[k] = 1;
          q[q_ed++] = k;
        }
      }
    }
    for(i=(0);i<(N);i++){
      sz[i] = 0;
    }
    for(j=N-1;j>=0;j--){
      i = q[j];
      sz[i] = 1;
      for(k=(0);k<(es[i]);k++){
        sz[i] += sz[edge[i][k]];
      }
    }
    for(i=(0);i<(N);i++){
      group[i] = -1;
    }
    groupNum = 0;
    for(j=(0);j<(N);j++){
      i = q[j];
      if(group[i]>=0){
        continue;
      }
      group[i] = groupNum++;
      groupind[i] = 0;
      for(;;){
        mx = -1;
        for(k=(0);k<(es[i]);k++){
          if(group[edge[i][k]] != -1){
            continue;
          }
          if(mx==-1){
            mx = k;
          }
          else if(sz[edge[i][k]] > sz[edge[i][mx]]){
            mx = k;
          }
        }
        if(mx==-1){
          break;
        }
        group[edge[i][mx]] = group[i];
        groupind[edge[i][mx]] = groupind[i]+1;
        i = edge[i][mx];
      }
    }
    walloc1d(&groupSize, groupNum, mem);
    walloc1d(&groupUpNode, groupNum, mem);
    walloc1d(&groupDepth, groupNum, mem);
    for(i=(0);i<(groupNum);i++){
      groupSize[i] = 0;
    }
    for(i=(0);i<(N);i++){
      groupSize[group[i]]++;
    }
    walloc1d(&groupNode, groupNum, mem);
    for(i=(0);i<(groupNum);i++){
      walloc1d(&groupNode[i], groupSize[i], mem);
    }
    for(i=(0);i<(N);i++){
      groupNode[group[i]][groupind[i]] = i;
    }
    for(i=(0);i<(groupNum);i++){
      groupDepth[i] = -1;
    }
    groupUpNode[0] = -1;
    groupDepth[0] = 0;
    for(x=(0);x<(groupNum);x++){
      for(y=(0);y<(groupSize[x]);y++){
        i = groupNode[x][y];
        for(j=(0);j<(es[i]);j++){
          k = edge[i][j];
          if(x != group[k] && groupDepth[group[k]]==-1){
            groupUpNode[group[k]] = i;
            groupDepth[group[k]] = groupDepth[x] + 1;
          }
        }
      }
    }
  }
  int lca(int x, int y){
    int x1;
    int y1;
    int x2;
    int y2;
    x1 = group[x];
    x2 = groupind[x];
    y1 = group[y];
    y2 = groupind[y];
    while(groupDepth[x1] > groupDepth[y1]){
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    while(groupDepth[x1] < groupDepth[y1]){
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    while(x1 != y1){
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    if(x2 <= y2){
      return x;
    }
    return y;
  }
  int depth(int x){
    int x1;
    int x2;
    int res = 0;
    x1 = group[x];
    x2 = groupind[x];
    while(groupUpNode[x1] != -1){
      res += x2 + 1;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    return res + x2;
  }
  int dist(int x, int y){
    int x1;
    int y1;
    int x2;
    int y2;
    int res = 0;
    x1 = group[x];
    x2 = groupind[x];
    y1 = group[y];
    y2 = groupind[y];
    while(groupDepth[x1] > groupDepth[y1]){
      res += x2 + 1;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    while(groupDepth[x1] < groupDepth[y1]){
      res += y2 + 1;
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    while(x1 != y1){
      res += x2 + y2 + 2;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
      y = groupUpNode[y1];
      y1 = group[y];
      y2 = groupind[y];
    }
    if(x2 <= y2){
      return res + y2 - x2;
    }
    return res + x2 - y2;
  }
  int up(int x){
    int x1 = group[x];
    int x2 = groupind[x];
    if(x2==0){
      return groupUpNode[x1];
    }
    return groupNode[x1][x2-1];
  }
  int up(int x, int d){
    int x1 = group[x];
    int x2 = groupind[x];
    while(d > x2){
      if(groupUpNode[x1]==-1){
        return -1;
      }
      d -= x2 + 1;
      x = groupUpNode[x1];
      x1 = group[x];
      x2 = groupind[x];
    }
    return groupNode[x1][x2-d];
  }
}
;
int N;
int K;
int Q;
int C[100000];
int A[100000];
int E[100000];
int F[100000];
int T[3];
int cmx[100000];
graph g;
HLD hld;
segtree_pg<int> t;
void segtree_pg_func(int &res, int &a, int &b){
  res = hld.lca(a,b);
}
int main(){
  int ao_dF3pO, i;
  wmem = memarr;
  rd(N);
  rd(K);
  rd(Q);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(C[Lj4PdHRW]);
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(K);e98WHCEY++){
      rd(A[e98WHCEY]);A[e98WHCEY] += (-1);
    }
  }
  {
    int FmcKpFmN;
    for(FmcKpFmN=(0);FmcKpFmN<(N-1);FmcKpFmN++){
      rd(E[FmcKpFmN]);E[FmcKpFmN] += (-1);
      rd(F[FmcKpFmN]);F[FmcKpFmN] += (-1);
    }
  }
  g.setEdge(N,N-1,E,F);
  g.getDistTree_WeightedNode_max(0, C, cmx);
  hld.init(g);
  t.malloc(K);
  t.setN(K);
  for(i=(0);i<(K);i++){
    t[i] = A[i];
  }
  t.build();
  for(ao_dF3pO=(0);ao_dF3pO<(Q);ao_dF3pO++){
    rd(T[0]);
    rd(T[1]);T[1] += (-1);
    rd(T[2]);T[2] += (-1);
    if(T[0]==1){
      t.change(T[1], T[2]);
    }
    else{
      wt_L(cmx[t.get(T[1], T[2]+1)]);
      wt_L('\n');
    }
  }
  return 0;
}
template<class T> void segtree_pg<T>::malloc(int maxN, int once /*= 0*/){
  int i;
  for(i=1;i<maxN;i*=2){
    ;
  }
  val = new T[2*i];
  if(once){
    setN(maxN);
  }
}
template<class T> void segtree_pg<T>::walloc(int maxN, int once /*= 0*/, void **mem /*= &wmem*/){
  int i;
  for(i=1;i<maxN;i*=2){
    ;
  }
  walloc1d(&val, 2*i, mem);
  if(once){
    setN(maxN);
  }
}
template<class T> void segtree_pg<T>::free(void){
  delete [] val;
}
template<class T> T& segtree_pg<T>::operator[](int i){
  return val[N+i];
}
template<class T> void segtree_pg<T>::setN(int n, int zerofill /*= 1*/, int dobuild /*= 1*/){
  int i;
  for(i=1,logN=0;i<n;i*=2,logN++){
    ;
  }
  N = i;
  if(dobuild){
    build();
  }
}
template<class T> void segtree_pg<T>::build(void){
  for(int i=N-1;i;i--){
    segtree_pg_func(val[i], val[2*i], val[2*i+1]);
  }
}
template<class T> inline void segtree_pg<T>::build(int a){
  while(a > 1){
    a /= 2;
    segtree_pg_func(val[a], val[2*a], val[2*a+1]);
  }
}
template<class T> inline void segtree_pg<T>::change(int a, T v){
  val[a+N] = v;
  build(a+N);
}
template<class T> inline T segtree_pg<T>::get(int a, int b){
  T res;
  T tmp;
  int fga = 0;
  int fgb = 0;
  a += N;
  b += N;
  while(a < b){
    if(a%2){
      if(fga){
        segtree_pg_func(res, res, val[a]);
      }
      else{
        res = val[a];
        fga = 1;
      }
      a++;
    }
    if(b%2){
      b--;
      if(fgb){
        segtree_pg_func(tmp, val[b], tmp);
      }
      else{
        tmp = val[b];
        fgb = 1;
      }
    }
    a /= 2;
    b /= 2;
  }
  if(fga==1 && fgb==0){
    return res;
  }
  if(fga==0 && fgb==1){
    return tmp;
  }
  if(fga==1 && fgb==1){
    segtree_pg_func(res, res, tmp);
    return res;
  }
  return res;
}
// cLay varsion 20200419-1 [beta]

// --- original code ---
// int N, K, Q, C[1d5], A[1d5], E[1d5], F[1d5], T[3];
// 
// int cmx[1d5];
// graph g;
// HLD hld;
// segtree_pg<int> t;
// 
// void segtree_pg_func(int &res, int &a, int &b){
//   res = hld.lca(a,b);
// }
// 
// {
//   rd(N,K,Q,C(N),(A--)(K),(E--,F--)(N-1));
// 
//   g.setEdge(N,N-1,E,F);
//   g.getDistTree_WeightedNode_max(0, C, cmx);
//   hld.init(g);
// 
//   t.malloc(K);
//   t.setN(K);
//   rep(i,K) t[i] = A[i];
//   t.build();
// 
//   rep(Q){
//     rd(T[0],T[1]--,T[2]--);
//     if(T[0]==1){
//       t.change(T[1], T[2]);
//     } else {
//       wt(cmx[t.get(T[1], T[2]+1)]);
//     }
//   }
// }
0