結果

問題 No.1266 7 Colors
ユーザー LayCurseLayCurse
提出日時 2020-10-23 22:23:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 3,000 ms
コード長 8,929 bytes
コンパイル時間 2,917 ms
コンパイル使用メモリ 218,332 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-07-21 11:05:31
合計ジャッジ時間 5,465 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 37 ms
7,296 KB
testcase_04 AC 61 ms
10,240 KB
testcase_05 AC 40 ms
7,424 KB
testcase_06 AC 61 ms
10,752 KB
testcase_07 AC 62 ms
11,264 KB
testcase_08 AC 58 ms
10,240 KB
testcase_09 AC 50 ms
9,216 KB
testcase_10 AC 49 ms
8,960 KB
testcase_11 AC 43 ms
7,680 KB
testcase_12 AC 46 ms
8,192 KB
testcase_13 AC 47 ms
8,576 KB
testcase_14 AC 39 ms
7,424 KB
testcase_15 AC 70 ms
11,392 KB
testcase_16 AC 45 ms
8,320 KB
testcase_17 AC 63 ms
11,136 KB
testcase_18 AC 24 ms
12,672 KB
testcase_19 AC 29 ms
11,776 KB
testcase_20 AC 29 ms
11,648 KB
testcase_21 AC 13 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
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;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
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');
  }
}
inline void wt_L(unsigned x){
  int s=0;
  char f[10];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  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');
  }
}
inline void wt_L(unsigned long long x){
  int s=0;
  char f[21];
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int WRITER_DOUBLE_DIGIT = 15;
inline int writerDigit_double(){
  return WRITER_DOUBLE_DIGIT;
}
inline void writerDigit_double(int d){
  WRITER_DOUBLE_DIGIT = d;
}
inline void wt_L(double x){
  const int d = WRITER_DOUBLE_DIGIT;
  int k;
  int r;
  double v;
  if(x!=x || (x==x+1 && x==2*x)){
    my_putchar_unlocked('E');
    my_putchar_unlocked('r');
    my_putchar_unlocked('r');
    return;
  }
  if(x < 0){
    my_putchar_unlocked('-');
    x = -x;
  }
  x += 0.5 * pow(0.1, d);
  r = 0;
  v = 1;
  while(x >= 10*v){
    v *= 10;
    r++;
  }
  while(r >= 0){
    r--;
    k = floor(x / v);
    if(k >= 10){
      k = 9;
    }
    if(k <= -1){
      k = 0;
    }
    x -= k * v;
    v *= 0.1;
    my_putchar_unlocked(k + '0');
  }
  if(d > 0){
    my_putchar_unlocked('.');
    v = 1;
    for(r=(0);r<(d);r++){
      v *= 0.1;
      k = floor(x / v);
      if(k >= 10){
        k = 9;
      }
      if(k <= -1){
        k = 0;
      }
      x -= k * v;
      my_putchar_unlocked(k + '0');
    }
  }
}
inline void wt_L(const char c[]){
  int i=0;
  for(i=0;c[i]!='\0';i++){
    my_putchar_unlocked(c[i]);
  }
}
inline void wt_L(string &x){
  int i=0;
  for(i=0;x[i]!='\0';i++){
    my_putchar_unlocked(x[i]);
  }
}
struct unionFind{
  int*d;
  int N;
  int M;
  inline void malloc(const int n){
    d = (int*)std::malloc(n*sizeof(int));
    M = n;
  }
  inline void malloc(const int n, const int fg){
    d = (int*)std::malloc(n*sizeof(int));
    M = n;
    if(fg){
      init(n);
    }
  }
  inline void free(void){
    std::free(d);
  }
  inline void walloc(const int n, void **mem=&wmem){
    walloc1d(&d, n, mem);
    M = n;
  }
  inline void walloc(const int n, const int fg, void **mem=&wmem){
    walloc1d(&d, n, mem);
    M = n;
    if(fg){
      init(n);
    }
  }
  inline void init(const int n){
    int i;
    N = n;
    for(i=(0);i<(n);i++){
      d[i] = -1;
    }
  }
  inline void init(void){
    init(M);
  }
  inline int get(int a){
    int t = a;
    int k;
    while(d[t]>=0){
      t=d[t];
    }
    while(d[a]>=0){
      k=d[a];
      d[a]=t;
      a=k;
    }
    return a;
  }
  inline int connect(int a, int b){
    if(d[a]>=0){
      a=get(a);
    }
    if(d[b]>=0){
      b=get(b);
    }
    if(a==b){
      return 0;
    }
    if(d[a] < d[b]){
      d[a] += d[b];
      d[b] = a;
    }
    else{
      d[b] += d[a];
      d[a] = b;
    }
    return 1;
  }
  inline int operator()(int a){
    return get(a);
  }
  inline int operator()(int a, int b){
    return connect(a,b);
  }
  inline int& operator[](const int a){
    return d[a];
  }
  inline int size(int a){
    a = get(a);
    return -d[a];
  }
  inline int sizeList(int res[]){
    int i;
    int sz=0;
    for(i=(0);i<(N);i++){
      if(d[i]<0){
        res[sz++] = -d[i];
      }
    }
    return sz;
  }
}
;
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];
    }
  }
}
;
int N;
int M;
int Q;
int A[100000];
int B[100000];
int T;
int X;
int Y;
graph g;
char S[100000][9];
int main(){
  int APIVbQlN, i;
  wmem = memarr;
  int x;
  int y;
  unionFind uf;
  rd(N);
  rd(M);
  rd(Q);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(S[Lj4PdHRW]);
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(M);e98WHCEY++){
      rd(A[e98WHCEY]);A[e98WHCEY] += (-1);
      rd(B[e98WHCEY]);B[e98WHCEY] += (-1);
    }
  }
  g.setEdge(N,M,A,B);
  uf.walloc(N*7, 1);
  for(i=(0);i<(N);i++){
    int j;
    for(j=(0);j<(7);j++){
      if(S[i][j]==S[i][(j+1)%7] && S[i][(j+1)%7]=='1'){
        uf(i*7+j, i*7+(j+1)%7);
      }
    }
  }
  for(i=(0);i<(M);i++){
    int j;
    for(j=(0);j<(7);j++){
      {
        auto hCmBdyQB = (A[i]);
        auto V9aVTaxx = ( B[i]);
        x = hCmBdyQB;
        y = V9aVTaxx;
      }
      if(S[x][j]==S[y][j] && S[y][j]=='1'){
        uf(x*7+j, y*7+j);
      }
    }
  }
  for(APIVbQlN=(0);APIVbQlN<(Q);APIVbQlN++){
    rd(T);
    rd(X);X += (-1);
    rd(Y);Y += (-1);
    if(T==1){
      int j;
      S[X][Y] = '1';
      for(j=(0);j<(7);j++){
        if(S[X][j]==S[X][(j+1)%7] && S[X][(j+1)%7]=='1'){
          uf(X*7+j, X*7+(j+1)%7);
        }
      }
      for(i=(0);i<(g.es[X]);i++){
        {
          auto XJIcIBrW = (X);
          auto jPV_0s1p = ( g.edge[X][i]);
          x = XJIcIBrW;
          y = jPV_0s1p;
        }
        if(S[x][Y]==S[y][Y] && S[y][Y]=='1'){
          uf(x*7+Y, y*7+Y);
        }
        {
          auto BUotOFBp = (X);
          auto Q5rsz4fz = ( g.edge[X][i]);
          y = BUotOFBp;
          x = Q5rsz4fz;
        }
        if(S[x][Y]==S[y][Y] && S[y][Y]=='1'){
          uf(x*7+Y, y*7+Y);
        }
      }
    }
    else{
      wt_L(uf.size(X*7));
      wt_L('\n');
    }
  }
  return 0;
}
// cLay varsion 20201018-2

// --- original code ---
// int N, M, Q, A[1d5], B[1d5], T, X, Y;
// graph g;
// char S[1d5][9];
// {
//   int x, y;
//   unionFind uf;
//   rd(N,M,Q,S(N),(A--,B--)(M));
//   g.setEdge(N,M,A,B);
//   uf.walloc(N*7, 1);
//   rep(i,N) rep(j,7) if(S[i][j]==S[i][(j+1)%7]=='1') uf(i*7+j, i*7+(j+1)%7);
//   rep(i,M) rep(j,7){
//     (x, y) = (A[i], B[i]);
//     if(S[x][j]==S[y][j]=='1') uf(x*7+j, y*7+j);
//   }
// 
//   rep(Q){
//     rd(T, X--, Y--);
//     if(T==1){
//       S[X][Y] = '1';
//       rep(j,7) if(S[X][j]==S[X][(j+1)%7]=='1') uf(X*7+j, X*7+(j+1)%7);
//       rep(i,g.es[X]){
//         (x, y) = (X, g.edge[X][i]);
//         if(S[x][Y]==S[y][Y]=='1') uf(x*7+Y, y*7+Y);
//         (y, x) = (X, g.edge[X][i]);
//         if(S[x][Y]==S[y][Y]=='1') uf(x*7+Y, y*7+Y);
//       }
//     } else {
//       wt(uf.size(X*7));
//     }
//   }
// }
0