Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of elements, which work in the following way.Each element has one controller that can be set to any non-negative real value. If a controller is set on some value x, then the controller consumes x2 energy units per second. At the same time, any two elements connected by a wire produce y·z energy units per second, where y and z are the values set on their controllers.Petr has only a limited number of wires, so he has already built some scheme of elements and wires, and is now interested if it's possible to set the controllers in such a way that the system produces at least as much power as it consumes, and at least one controller is set on the value different from 0. Help him check this, and if it's possible, find the required integer values that should be set.It is guaranteed that if there exist controllers' settings satisfying the above conditions, then there exist required integer values not greater than 106.

There are several (at least one) test cases in the input. The first line contains single integer — the number of test cases. There is an empty line before each test case. The first line of test case contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of elements in the scheme and the number of wires. After that, m lines follow, each of them contains two integers a and b (1 ≤ a, b ≤ n) — two elements connected by a wire. No element is connected with itself, no two elements are connected by more than one wire. It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 105. For hacks you can only use tests with one test case.

Print answer for each test case. For each test case print "YES" if it's possible to set the controllers in such a way that the consumed power is not greater than the power produced, and the required values on the next line. The settings should be integers from 0 to 106, inclusive, and at least one value should be different from 0. If there are multiple answers, print any of them. If it's not possible to set the controllers in the required way, print one line "NO".

NoteIn the first example it's possible to set the controllers in the required way, for example, in the following way: set 1 on the first element, set 2 on the second and on the third, set 1 on the fourth. The consumed power is then equal to 12 + 22 + 22 + 12 = 10 energy units per second, the produced power is equal to 1·2 + 2·2 + 2·1 + 2·1 = 10 energy units per second. Thus the answer is "YES".In the second test case it's not possible to set the controllers in the required way. For example, if we set all controllers to 0.5, then the consumed powers equals 0.75 energy units per second, while produced power equals 0.5 energy units per second.

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

/*
  x "--- x t"
  x (x t) - (x t)^2 = x^2 (t - t^2) <= (1/4) x^2  (max at t = 1/2)

  x "--- x t --- *"
  x (x t) - (x t)^2 + (1/4) (x t)^2 = x^2 (t - (3/4) t^2) <= (1/3) x^2  (max at t = 2/3)

  x "--- x t --- * --- *"
  x (x t) - (x t)^2 + (1/3) (x t)^2 = x^2 (t - (2/3) t^2) <= (3/8) x^2  (max at t = 3/4)

  x "--- * --- * ... --- *"
  (1/2 - 1/(2(k+1))) x^2 at (k/(k+1)) x, ..., (1/(k+1)) x

  deg = 3
  -x^2 + \sum_{j=1}^3 (1/2 - 1/(2(k_j+1))) x^2
  = (1/2) (1 - \sum_{j=1}^3 1/(k_j+1)) x^2

  1/2 + 1/3 + 1/6
  1/2 + 1/4 + 1/4
  1/3 + 1/3 + 1/3
*/
void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto G = new int[][N];
        foreach (i; 0 .. M) {
          G[A[i]] ~= i;
          G[B[i]] ~= i;
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }
        auto uss = new int[][N];
        foreach (u; 0 .. N) {
          uss[uf.root(u)] ~= u;
        }
        auto iss = new int[][N];
        foreach (i; 0 .. M) {
          iss[uf.root(A[i])] ~= i;
        }

        auto ans = new long[N];
        foreach (r; 0 .. N) {
          if (uf[r] < 0) {
            if (cast(int)(iss[r].length) >= -uf[r]) {
              foreach (u; uss[r]) {
                ans[u] = 1;
              }
            } else {
              const us4 = uss[r].filter!(u => (G[u].length >= 4)).array;
              if (us4.length >= 1) {
                foreach (u; uss[r]) {
                  ans[u] = 1;
                }
                ans[us4[0]] = 2;
              } else {
                const us3 = uss[r].filter!(u => (G[u].length >= 3)).array;
                if (us3.length >= 2) {
                  int dfs(int u, int p) {
                    int ret = 1;
                    foreach (i; G[u]) {
                      const v = A[i] ^ B[i] ^ u;
                      if (v != p) {
                        const res = dfs(v, u);
                        if (res == 2) {
                          ret = 2;
                        }
                      }
                    }
                    if (u == us3[1]) {
                      ret = 2;
                    }
                    return ans[u] = ret;
                  }
                  dfs(us3[0], -1);
                } else if (us3.length >= 1) {
                  auto vss = new int[][3];
                  foreach (j; 0 .. 3) {
                    const i0 = G[us3[0]][j];
                    for (int p = us3[0], u = A[i0] ^ B[i0] ^ us3[0]; ; ) {
                      vss[j] ~= u;
                      if (G[u].length == 1) {
                        break;
                      }
                      foreach (i; G[u]) {
                        const v = A[i] ^ B[i] ^ u;
                        if (v != p) {
                          p = u;
                          u = v;
                          break;
                        }
                      }
                    }
                  }
                  vss.sort!"a.length < b.length";
                  debug {
                    writeln("vss = ", vss);
                  }
                  foreach (ks; [[1, 2, 5], [1, 3, 3], [2, 2, 2]]) {
                    bool ok = true;
                    foreach (j; 0 .. 3) {
                      ok = ok && (vss[j].length >= ks[j]);
                    }
                    if (ok) {
                      ans[us3[0]] = 12;
                      foreach (j; 0 .. 3) {
                        foreach (l; 0 .. ks[j]) {
                          ans[vss[j][l]] = 12 / (ks[j] + 1) * (ks[j] - l);
                        }
                      }
                      break;
                    }
                  }
                }
              }
            }
          }
        }

        long score;
        foreach (u; 0 .. N) {
          score -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          score += ans[A[i]] * ans[B[i]];
        }
        debug {
          writeln("score = ", score);
        }
        if (score >= 0 && ans.sum > 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum L = 10L^^6;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto deg = new int[N];
        foreach (i; 0 .. M) {
          ++deg[A[i]];
          ++deg[B[i]];
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }

        auto numEdges = new int[N];
        foreach (i; 0 .. M) {
          ++numEdges[uf.root(A[i])];
        }
        auto hasHub = new bool[N];
        foreach (u; 0 .. N) {
          if (deg[u] >= 4) {
            hasHub[uf.root(u)] = true;
          }
        }

        auto ans = new long[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (numEdges[r] > -uf[r] - 1) {
            ans[u] = L;
          } else {
            ans[u] = (deg[u] >= 4) ? L : hasHub[r] ? (L / 2) : 1;
          }
        }

        long sum;
        foreach (u; 0 .. N) {
          sum -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          sum += ans[A[i]] * ans[B[i]];
        }
        if (sum >= 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum L = 10L^^6;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto deg = new int[N];
        foreach (i; 0 .. M) {
          ++deg[A[i]];
          ++deg[B[i]];
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }

        auto numEdges = new int[N];
        foreach (i; 0 .. M) {
          const r = uf.root(A[i]);
          ++numEdges[r];
        }
        auto numLeaves = new int[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (deg[u] == 1) {
            ++numLeaves[r];
          }
        }

        auto ans = new long[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (numEdges[r] > (-uf[r]) - 1 || numLeaves[r] >= 4) {
            ans[u] = (deg[u] == 1) ? (L / 2) : L;
          } else {
            ans[u] = 0;
          }
        }

        long sum;
        foreach (u; 0 .. N) {
          sum -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          sum += ans[A[i]] * ans[B[i]];
        }
        debug {
          writeln("sum = ", sum);
        }
        if (sum >= 0 && ans.sum > 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum L = 10L^^6;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto deg = new int[N];
        foreach (i; 0 .. M) {
          ++deg[A[i]];
          ++deg[B[i]];
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }

        auto numEdges = new int[N];
        foreach (i; 0 .. M) {
          ++numEdges[uf.root(A[i])];
        }
        auto cnt4 = new int[N];
        auto cnt3 = new int[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (deg[u] >= 4) {
            ++cnt4[r];
          } else if (deg[u] >= 3) {
            ++cnt3[r];
          }
        }

        auto ans = new long[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (numEdges[r] > (-uf[r]) - 1) {
            ans[u] = L;
          } else if (cnt4[r] >= 1) {
            ans[u] = (deg[u] >= 4) ? L : (L / 2);
          } else if (cnt3[r] >= 2) {
            ans[u] = (deg[u] >= 3) ? L : (L / 2);
          } else {
            ans[u] = 1;
          }
        }

        long sum;
        foreach (u; 0 .. N) {
          sum -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          sum += ans[A[i]] * ans[B[i]];
        }
        debug {
          writeln("sum = ", sum);
        }
        if (sum >= 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum L = 10L^^6;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto deg = new int[N];
        foreach (i; 0 .. M) {
          ++deg[A[i]];
          ++deg[B[i]];
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }

        auto numEdges = new int[N];
        foreach (i; 0 .. M) {
          const r = uf.root(A[i]);
          ++numEdges[r];
        }
        auto numLeaves = new int[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (deg[u] == 1) {
            ++numLeaves[r];
          }
        }

        auto ans = new long[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (numEdges[r] > (-uf[r]) - 1 || numLeaves[r] >= 4) {
            ans[u] = (deg[u] == 1) ? (L / 2) : L;
          } else {
            ans[u] = 1;
          }
        }

        long sum;
        foreach (u; 0 .. N) {
          sum -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          sum += ans[A[i]] * ans[B[i]];
        }
        debug {
          writeln("sum = ", sum);
        }
        if (sum >= 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum L = 10L^^6;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto deg = new int[N];
        foreach (i; 0 .. M) {
          ++deg[A[i]];
          ++deg[B[i]];
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }

        auto numEdges = new int[N];
        foreach (i; 0 .. M) {
          ++numEdges[uf.root(A[i])];
        }
        auto cnt4 = new int[N];
        auto cnt3 = new int[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (deg[u] >= 4) {
            ++cnt4[r];
          } else if (deg[u] >= 3) {
            ++cnt3[r];
          }
        }

        auto ans = new long[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (numEdges[r] > (-uf[r]) - 1) {
            ans[u] = L;
          } else if (cnt4[r] >= 1 || cnt3[r] >= 2) {
            ans[u] = (deg[u] == 1) ? (L / 2) : L;
          } else {
            ans[u] = 1;
          }
        }

        long sum;
        foreach (u; 0 .. N) {
          sum -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          sum += ans[A[i]] * ans[B[i]];
        }
        debug {
          writeln("sum = ", sum);
        }
        if (sum >= 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum L = 10L^^6;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const M = readInt();
        auto A = new int[M];
        auto B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto deg = new int[N];
        foreach (i; 0 .. M) {
          ++deg[A[i]];
          ++deg[B[i]];
        }
        auto uf = new int[N];
        uf[] = -1;
        foreach (i; 0 .. M) {
          uf.connect(A[i], B[i]);
        }

        auto numEdges = new int[N];
        foreach (i; 0 .. M) {
          const r = uf.root(A[i]);
          ++numEdges[r];
        }
        auto numLeaves = new int[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (deg[u] == 1) {
            ++numLeaves[r];
          }
        }

        auto ans = new long[N];
        foreach (u; 0 .. N) {
          const r = uf.root(u);
          if (numEdges[r] > (-uf[r]) - 1 || numLeaves[r] >= 4) {
            ans[u] = (deg[u] == 1) ? (L / 2) : L;
          } else {
            ans[u] = 0;
          }
        }

        long sum;
        foreach (u; 0 .. N) {
          sum -= ans[u]^^2;
        }
        foreach (i; 0 .. M) {
          sum += ans[A[i]] * ans[B[i]];
        }
        debug {
          writeln("sum = ", sum);
        }
        if (sum >= 0) {
          writeln("YES");
          foreach (u; 0 .. N) {
            if (u > 0) write(" ");
            write(ans[u]);
          }
          writeln;
        } else {
          writeln("NO");
        }
      }
    }
  } catch (EOFException e) {
  }
}


Polycarp doesn't like integers that are divisible by $$$3$$$ or end with the digit $$$3$$$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.Polycarp starts to write out the positive (greater than $$$0$$$) integers which he likes: $$$1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \dots$$$. Output the $$$k$$$-th element of this sequence (the elements are numbered from $$$1$$$).

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$k$$$ ($$$1 \le k \le 1000$$$).

For each test case, output in a separate line one integer $$$x$$$ — the $$$k$$$-th element of the sequence that was written out by Polycarp.

import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    long ans;
    foreach (i ; 1 .. 100000) {
      if (i % 10 == 3 || i % 3 == 0) {
      } else {
        if (--n == 0) {
          writeln(i);
          break;
        }
      }
    }
  }
}

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    int k = scan!int;
    int j = 0;
    for(int i = 1; i <= 3*k; ++i){
        if(i % 3 == 0 || i % 10 == 3) continue;
        ++j;
        if(j == k){
            writeln(i);
        }
    }
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) theCode();
}

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");


Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.

The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.

Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.

5
threehorses
goodsubstrings
secret
primematrix
beautifulyear

NoteIn the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.

import std.algorithm;
import std.stdio;
import std.string;

immutable int LET = 26;

struct node
{
	int p [LET];
}

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		node [] t;
		t.length++;
		foreach (i; 0..n)
		{
			auto s = strip (readln ());
			while (s.length > 0)
			{
				debug {writeln (s);}
				int r = 0;
				foreach (c; s)
				{
					int d = c - 'a';
					if (!t[r].p[d])
					{
						t[r].p[d] = t.length;
						t.length++;
					}
					r = t[r].p[d];
				}
				s = s[1..$];
			}
		}
		string res = "zzz";
		foreach (d; 0..LET)
		{
			debug {writefln ("%s %s", d, t[0].p[d]);}
			if (!t[0].p[d])
			{
				res = min (res, "" ~ cast (char) (d + 'a'));
			}
		}
		if (res == "zzz")
		{
			foreach (d; 0..LET)
			{
				foreach (e; 0..LET)
				{
					if (!t[t[0].p[d]].p[e])
					{
						res = min (res, "" ~ cast (char) (d + 'a') ~
						                     cast (char) (e + 'a'));
					}
				}
			}
		}
		writeln (res);
	}
}


These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts $$$1$$$ minute.He was asked to insert one takeoff in the schedule. The takeoff takes $$$1$$$ minute itself, but for safety reasons there should be a time space between the takeoff and any landing of at least $$$s$$$ minutes from both sides.Find the earliest time when Arkady can insert the takeoff.

The first line of input contains two integers $$$n$$$ and $$$s$$$ ($$$1 \le n \le 100$$$, $$$1 \le s \le 60$$$) — the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff. Each of next $$$n$$$ lines contains two integers $$$h$$$ and $$$m$$$ ($$$0 \le h \le 23$$$, $$$0 \le m \le 59$$$) — the time, in hours and minutes, when a plane will land, starting from current moment (i. e. the current time is $$$0$$$ $$$0$$$). These times are given in increasing order.

Print two integers $$$h$$$ and $$$m$$$ — the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff.

NoteIn the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute.In the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than $$$24$$$ hours to insert the takeoff.In the third example Arkady can insert the takeoff even between the first landing.

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main() {
	auto ns = readln.chomp.split.map!(to!int);
	int n = ns[0];
	int s = ns[1];
	int[] times;
	times ~= -s - 1;
	foreach (i; 0..n) {
		auto hs = readln.chomp.split.map!(to!int);
		int h = hs[0];
		int m = hs[1];
		int t = h * 60 + m;
		times ~= t;
	}
	times ~= times[$-1] + (1 << 28);
	int len = cast(int) times.length;
	for (int i = 0; i < len - 1; ++i) {
		int t1 = times[i];
		int t2 = times[i + 1];
		debug stderr.writefln("[%d] %d %d ... %d", i, t1, t2, t2 - t1);
		if (t2 - t1 >= s * 2 + 2) {
			int h = t1 / 60;
			int m = t1 % 60;
			debug stderr.writefln("\t%d, %d", h, m);
			m += s + 1;
			h += m / 60;
			m %= 60;
			if (h < 0 || m < 0) {
				continue;
			}
			debug stderr.writefln("\t\t[%d] %d %d ... %d", i, t1, t2, t2 - t1);
			writefln("%d %d", h, m);
			return;
		}
	}
}

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, s; readV(n, s);

  auto t = new int[](n);
  foreach (i; 0..n) {
    int h, m; readV(h, m);
    t[i] = h*60+m;
  }

  auto put(int ans)
  {
    writeln(ans/60, " ", ans%60);
  }

  if (t[0] >= s+1) {
    put(0);
    return;
  }

  foreach (i; 0..n-1) {
    if (t[i+1]-t[i] >= s*2+2) {
      put(t[i]+s+1);
      return;
    }
  }

  put(t[$-1]+s+1);
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main() {
	auto ns = readln.chomp.split.map!(to!int);
	int n = ns[0];
	int s = ns[1];
	int[] times;
	times ~= -s - 1;
	foreach (i; 0..n) {
		auto hs = readln.chomp.split.map!(to!int);
		int h = hs[0];
		int m = hs[1];
		int t = h * 60 + m;
		times ~= t;
	}
	times ~= times[$-1] + s;
	times ~= times[$-1] + 1 << 28;
	int len = cast(int) times.length;
	for (int i = 0; i < len - 1; ++i) {
		int t1 = times[i];
		int t2 = times[i + 1];
		if (t2 - t1 >= s * 2 + 2) {
			int h = t1 / 60;
			int m = t1 % 60;
			m += s + 1;
			h += m / 60;
			m %= 60;
			debug stderr.writefln("[%d] %d %d ... %d", i, t1, t2, t2 - t1);
			writefln("%d %d", h, m);
			return;
		}
	}
}


In this problem, we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a string. We denote the substring of string $$$s$$$ starting from the $$$l$$$-th character and ending with the $$$r$$$-th character as $$$s[l \dots r]$$$. The characters of each string are numbered from $$$1$$$.We can perform several operations on the strings we consider. Each operation is to choose a substring of our string and replace it with another string. There are two possible types of operations: replace 011 with 110, or replace 110 with 011. For example, if we apply exactly one operation to the string 110011110, it can be transformed into 011011110, 110110110, or 110011011.Binary string $$$a$$$ is considered reachable from binary string $$$b$$$ if there exists a sequence $$$s_1$$$, $$$s_2$$$, ..., $$$s_k$$$ such that $$$s_1 = a$$$, $$$s_k = b$$$, and for every $$$i \in [1, k - 1]$$$, $$$s_i$$$ can be transformed into $$$s_{i + 1}$$$ using exactly one operation. Note that $$$k$$$ can be equal to $$$1$$$, i. e., every string is reachable from itself.You are given a string $$$t$$$ and $$$q$$$ queries to it. Each query consists of three integers $$$l_1$$$, $$$l_2$$$ and $$$len$$$. To answer each query, you have to determine whether $$$t[l_1 \dots l_1 + len - 1]$$$ is reachable from $$$t[l_2 \dots l_2 + len - 1]$$$.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of string $$$t$$$. The second line contains one string $$$t$$$ ($$$|t| = n$$$). Each character of $$$t$$$ is either 0 or 1. The third line contains one integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each line represents a query. The $$$i$$$-th line contains three integers $$$l_1$$$, $$$l_2$$$ and $$$len$$$ ($$$1 \le l_1, l_2 \le |t|$$$, $$$1 \le len \le |t| - \max(l_1, l_2) + 1$$$) for the $$$i$$$-th query.

For each query, print either YES if $$$t[l_1 \dots l_1 + len - 1]$$$ is reachable from $$$t[l_2 \dots l_2 + len - 1]$$$, or NO otherwise. You may print each letter in any register.

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct ModInt(int M_) {
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op)() const if (op == "-") { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op)(long a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 403;
alias Mint = ModInt!MO;
Mint R = 2020;

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

Mint[] RR;

struct Info {
  int num1, len, head;
  Mint val;
  Info opBinary(string op)(const(Info) o) const if (op == "*") {
    Info ret;
    ret.num1 = num1 + o.num1;
    if (len == 0) {
      ret.len = o.len;
      ret.head = (num1 & 1) ^ o.head;
      ret.val = o.val;
    } else if (o.len == 0) {
      ret.len = len;
      ret.head = head;
      ret.val = val;
    } else if ((head ^ (len & 1)) == ((num1 & 1) ^ o.head)) {
      // concat
      ret.len = len + o.len;
      ret.head = head;
      ret.val = val + RR[len] * o.val;
    } else {
      // overlap
      ret.len = len + o.len - 1;
      ret.head = head;
      ret.val = val + RR[len - 1] * o.val;
    }
    return ret;
  }
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const T = readToken();
      const Q = readInt();
      auto A = new int[Q];
      auto B = new int[Q];
      auto L = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
        L[q] = readInt();
      }

      foreach (q; 0 .. Q) {
        R.x ^= A[q] ^ B[q] ^ L[q];
      }
      RR = new Mint[N + 1];
      RR[0] = 1;
      foreach (i; 1 .. N + 1) {
        RR[i] = RR[i - 1] * R;
      }

      auto seg = new SegmentTree!(Info, "a * b")(N, Info(0, 0, 0, Mint(0)));
      foreach (i; 0 .. N) {
        seg.at(i) = (T[i] == '1') ? Info(1, 0, 0, Mint(0)) : Info(0, 1, 0, Mint(1));
      }
      seg.build;
      debug {
        foreach (i; 0 .. N) foreach (j; i + 1 .. N + 1) {
          // writeln(i, " ", j, ": ", seg.query(i, j));
        }
      }

      foreach (q; 0 .. Q) {
        const resA = seg.query(A[q], A[q] + L[q]);
        const resB = seg.query(B[q], B[q] + L[q]);
        debug {
          writeln(true, " ", resA, " ", resB);
        }
        const ans = (resA == resB);
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct ModInt(int M_) {
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op)() const if (op == "-") { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op)(long a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 403;
alias Mint = ModInt!MO;

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

Mint R;
Mint[] RR;

struct Info {
  int num1, len, head;
  Mint val;
  Info opBinary(string op)(const(Info) o) const if (op == "*") {
    Info ret;
    ret.num1 = num1 + o.num1;
    if (len == 0) {
      ret.len = o.len;
      ret.head = (num1 & 1) ^ o.head;
      ret.val = o.val;
    } else if (o.len == 0) {
      ret.len = len;
      ret.head = head;
      ret.val = val;
    } else if ((head ^ (len & 1)) == ((num1 & 1) ^ o.head)) {
      // concat
      ret.len = len + o.len;
      ret.head = head;
      ret.val = val + RR[len] * o.val;
    } else {
      // overlap
      ret.len = len + o.len - 1;
      ret.head = head;
      ret.val = val + RR[len - 1] * o.val;
    }
    return ret;
  }
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const T = readToken();
      const Q = readInt();
      auto A = new int[Q];
      auto B = new int[Q];
      auto L = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
        L[q] = readInt();
      }

      import std.datetime;
      R = 2020 ^ Clock.currStdTime;
      debug {
        writeln("R = ", R);
      }
      RR = new Mint[N + 1];
      RR[0] = 1;
      foreach (i; 1 .. N + 1) {
        RR[i] = RR[i - 1] * R;
      }

      auto seg = new SegmentTree!(Info, "a * b")(N, Info(0, 0, 0, Mint(0)));
      foreach (i; 0 .. N) {
        seg.at(i) = (T[i] == '1') ? Info(1, 0, 0, Mint(0)) : Info(0, 1, 0, Mint(1));
      }
      seg.build;
      debug {
        foreach (i; 0 .. N) foreach (j; i + 1 .. N + 1) {
          // writeln(i, " ", j, ": ", seg.query(i, j));
        }
      }

      foreach (q; 0 .. Q) {
        const resA = seg.query(A[q], A[q] + L[q]);
        const resB = seg.query(B[q], B[q] + L[q]);
        debug {
          writeln(true, " ", resA, " ", resB);
        }
        const ans = (resA == resB);
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct SuffixArray(T) {
  import std.algorithm : sort;
  int n;
  T[] ts;
  int[] us, su, lcp;
  this(T)(T[] ts) {
    n = cast(int)(ts.length);
    this.ts = ts;
    us = new int[n + 1];
    su = new int[n + 1];
    foreach (i; 0 .. n + 1) us[i] = i;
    us.sort!((u, v) => (cmp(u, v) < 0));
    auto vals = new int[n + 1], cnt = new int[n + 1], tmp = new int[n + 1];
    foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((cmp(us[i], us[i + 1]) < 0) ? 1 : 0);
    for (int h = 1; ; h <<= 1) {
      int ahead(int i) {
        return (us[i] + h <= n) ? su[us[i] + h] : 0;
      }
      foreach (i; 0 .. n + 1) su[us[i]] = vals[i];
      if (vals[n] == n) break;
      cnt[] = 0;
      foreach (i; 0 .. n + 1) ++cnt[ahead(i)];
      foreach (j; 0 .. n) cnt[j + 1] += cnt[j];
      foreach_reverse (i; 0 .. n + 1) tmp[--cnt[ahead(i)]] = us[i];
      cnt[] = 0;
      foreach (i; 0 .. n + 1) ++cnt[su[tmp[i]]];
      foreach (j; 0 .. n) cnt[j + 1] += cnt[j];
      foreach_reverse (i; 0 .. n + 1) us[--cnt[su[tmp[i]]]] = tmp[i];
      foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((su[us[i]] < su[us[i + 1]] || ahead(i) < ahead(i + 1)) ? 1 : 0);
    }
    lcp = new int[n];
    int h;
    foreach (u; 0 .. n) {
      for (int v = us[su[u] - 1]; cmp(u + h, v + h) == 0; ++h) {}
      lcp[su[u] - 1] = h;
      if (h > 0) --h;
    }
  }
  int cmp(int u, int v) const {
    return (u == n) ? ((v == n) ? 0 : -1) : (v == n) ? +1 : (ts[u] < ts[v]) ? -1 : (ts[u] > ts[v]) ? +1 : 0;
  }
  void print() const {
    import std.math : log10;
    import std.stdio : writefln;
    const numDigits = cast(int)(log10(n)) + 1;
    foreach (i; 0 .. n + 1) {
      writefln("%*d %s", numDigits, us[i], ts[us[i] .. $]);
    }
  }
}

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const T = readToken();
      const Q = readInt();
      auto A = new int[Q];
      auto B = new int[Q];
      auto L = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
        L[q] = readInt();
      }

      auto num1 = new int[N + 1];
      foreach (i; 0 .. N) {
        num1[i + 1] = num1[i] + ((T[i] == '1') ? 1 : 0);
      }
      auto num11 = new int[N];
      foreach (i; 0 .. N - 1) {
        num11[i + 1] = num11[i] + ((T[i] == '1' && T[i + 1] == '1') ? 1 : 0);
      }

      auto sum = new long[N + 1];
      foreach (i; 0 .. N) {
        sum[i + 1] = sum[i] + ((T[i] == '1') ? i : 0);
      }

      auto sa = new SuffixArray!(immutable(char))(T);
      auto seg = new SegmentTree!(int, min)(sa.lcp, N + 1);

      foreach (q; 0 .. Q) {
        bool ans;
        if (num11[A[q] + L[q] - 1] - num11[A[q]] > 0 && num11[B[q] + L[q] - 1] - num11[B[q]] > 0) {
          const n1A = num1[A[q] + L[q]] - num1[A[q]];
          const n1B = num1[B[q] + L[q]] - num1[B[q]];
          const sA = (sum[A[q] + L[q]] - sum[A[q]]) - 1L * n1A * A[q];
          const sB = (sum[B[q] + L[q]] - sum[B[q]]) - 1L * n1B * B[q];
          debug {
            writeln(true, " ", [n1A, n1B], " ", [sA, sB]);
          }
          ans = (n1A == n1B && (sA - sB) % 2 == 0);
        } else {
          int a = sa.su[A[q]], b = sa.su[B[q]];
          if (a > b) {
            swap(a, b);
          }
          const res = seg.query(a, b);
          debug {
            writeln(false, " ", A[q], " ", B[q], ": ", res);
          }
          ans = (res >= L[q]);
        }
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct SuffixArray(T) {
  import std.algorithm : sort;
  int n;
  T[] ts;
  int[] us, su, lcp;
  this(T)(T[] ts) {
    n = cast(int)(ts.length);
    this.ts = ts;
    us = new int[n + 1];
    su = new int[n + 1];
    foreach (i; 0 .. n + 1) us[i] = i;
    us.sort!((u, v) => (cmp(u, v) < 0));
    auto vals = new int[n + 1], cnt = new int[n + 1], tmp = new int[n + 1];
    foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((cmp(us[i], us[i + 1]) < 0) ? 1 : 0);
    for (int h = 1; ; h <<= 1) {
      int ahead(int i) {
        return (us[i] + h <= n) ? su[us[i] + h] : 0;
      }
      foreach (i; 0 .. n + 1) su[us[i]] = vals[i];
      if (vals[n] == n) break;
      cnt[] = 0;
      foreach (i; 0 .. n + 1) ++cnt[ahead(i)];
      foreach (j; 0 .. n) cnt[j + 1] += cnt[j];
      foreach_reverse (i; 0 .. n + 1) tmp[--cnt[ahead(i)]] = us[i];
      cnt[] = 0;
      foreach (i; 0 .. n + 1) ++cnt[su[tmp[i]]];
      foreach (j; 0 .. n) cnt[j + 1] += cnt[j];
      foreach_reverse (i; 0 .. n + 1) us[--cnt[su[tmp[i]]]] = tmp[i];
      foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((su[us[i]] < su[us[i + 1]] || ahead(i) < ahead(i + 1)) ? 1 : 0);
    }
    lcp = new int[n];
    int h;
    foreach (u; 0 .. n) {
      for (int v = us[su[u] - 1]; cmp(u + h, v + h) == 0; ++h) {}
      lcp[su[u] - 1] = h;
      if (h > 0) --h;
    }
  }
  int cmp(int u, int v) const {
    return (u == n) ? ((v == n) ? 0 : -1) : (v == n) ? +1 : (ts[u] < ts[v]) ? -1 : (ts[u] > ts[v]) ? +1 : 0;
  }
  void print() const {
    import std.math : log10;
    import std.stdio : writefln;
    const numDigits = cast(int)(log10(n)) + 1;
    foreach (i; 0 .. n + 1) {
      writefln("%*d %s", numDigits, us[i], ts[us[i] .. $]);
    }
  }
}

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const T = readToken();
      const Q = readInt();
      auto A = new int[Q];
      auto B = new int[Q];
      auto L = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
        L[q] = readInt();
      }

      auto num1 = new int[N + 1];
      foreach (i; 0 .. N) {
        num1[i + 1] = num1[i] + ((T[i] == '1') ? 1 : 0);
      }
      auto num11 = new int[N];
      foreach (i; 0 .. N - 1) {
        num11[i + 1] = num11[i] + ((T[i] == '1' && T[i + 1] == '1') ? 1 : 0);
      }

      auto sa = new SuffixArray!(immutable(char))(T);
      auto seg = new SegmentTree!(int, min)(sa.lcp, N + 1);

      foreach (q; 0 .. Q) {
        bool ans;
        if (num11[A[q] + L[q] - 1] - num11[A[q]] > 0 && num11[B[q] + L[q] - 1] - num11[B[q]] > 0) {
          ans = (num1[A[q] + L[q]] - num1[A[q]] == num1[B[q] + L[q]] - num1[B[q]]);
        } else {
          int a = sa.su[A[q]], b = sa.su[B[q]];
          if (a > b) {
            swap(a, b);
          }
          const res = seg.query(a, b);
          debug {
            writeln(A[q], " ", B[q], ": ", res);
          }
          ans = (res >= L[q]);
        }
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct SuffixArray(T) {
  import std.algorithm : sort;
  int n;
  T[] ts;
  int[] us, su, lcp;
  this(T)(T[] ts) {
    n = cast(int)(ts.length);
    this.ts = ts;
    us = new int[n + 1];
    su = new int[n + 1];
    foreach (i; 0 .. n + 1) us[i] = i;
    us.sort!((u, v) => (cmp(u, v) < 0));
    auto vals = new int[n + 1], cnt = new int[n + 1], tmp = new int[n + 1];
    foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((cmp(us[i], us[i + 1]) < 0) ? 1 : 0);
    for (int h = 1; ; h <<= 1) {
      int ahead(int i) {
        return (us[i] + h <= n) ? su[us[i] + h] : 0;
      }
      foreach (i; 0 .. n + 1) su[us[i]] = vals[i];
      if (vals[n] == n) break;
      cnt[] = 0;
      foreach (i; 0 .. n + 1) ++cnt[ahead(i)];
      foreach (j; 0 .. n) cnt[j + 1] += cnt[j];
      foreach_reverse (i; 0 .. n + 1) tmp[--cnt[ahead(i)]] = us[i];
      cnt[] = 0;
      foreach (i; 0 .. n + 1) ++cnt[su[tmp[i]]];
      foreach (j; 0 .. n) cnt[j + 1] += cnt[j];
      foreach_reverse (i; 0 .. n + 1) us[--cnt[su[tmp[i]]]] = tmp[i];
      foreach (i; 0 .. n) vals[i + 1] = vals[i] + ((su[us[i]] < su[us[i + 1]] || ahead(i) < ahead(i + 1)) ? 1 : 0);
    }
    lcp = new int[n];
    int h;
    foreach (u; 0 .. n) {
      for (int v = us[su[u] - 1]; cmp(u + h, v + h) == 0; ++h) {}
      lcp[su[u] - 1] = h;
      if (h > 0) --h;
    }
  }
  int cmp(int u, int v) const {
    return (u == n) ? ((v == n) ? 0 : -1) : (v == n) ? +1 : (ts[u] < ts[v]) ? -1 : (ts[u] > ts[v]) ? +1 : 0;
  }
  void print() const {
    import std.math : log10;
    import std.stdio : writefln;
    const numDigits = cast(int)(log10(n)) + 1;
    foreach (i; 0 .. n + 1) {
      writefln("%*d %s", numDigits, us[i], ts[us[i] .. $]);
    }
  }
}

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

struct Info {
  int a, b0, b1;
  Info opBinary(string op)(const(Info) o) const if (op == "*") {
    return Info(a + o.a, b0 + ((a & 1) ? o.b1 : o.b0), b1 + ((a & 1) ? o.b0 : o.b1));
  }
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const T = readToken();
      const Q = readInt();
      auto A = new int[Q];
      auto B = new int[Q];
      auto L = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt() - 1;
        B[q] = readInt() - 1;
        L[q] = readInt();
      }

      auto num1 = new int[N + 1];
      foreach (i; 0 .. N) {
        num1[i + 1] = num1[i] + ((T[i] == '1') ? 1 : 0);
      }
      auto num11 = new int[N];
      foreach (i; 0 .. N - 1) {
        num11[i + 1] = num11[i] + ((T[i] == '1' && T[i + 1] == '1') ? 1 : 0);
      }

      auto segInfo = new SegmentTree!(Info, "a * b")(N, Info(0, 0, 0));
      foreach (i; 0 .. N) {
        segInfo.at(i) = (T[i] == '1') ? Info(1, 0, 0) : Info(0, 1, 0);
      }
      segInfo.build;
      debug {
        foreach (i; 0 .. N) foreach (j; i + 1 .. N + 1) {
          // writeln(i, " ", j, ": ", segInfo.query(i, j));
        }
      }

      auto sa = new SuffixArray!(immutable(char))(T);
      auto segLCP = new SegmentTree!(int, min)(sa.lcp, N + 1);

      foreach (q; 0 .. Q) {
        bool ans;
        if (num11[A[q] + L[q] - 1] - num11[A[q]] > 0 && num11[B[q] + L[q] - 1] - num11[B[q]] > 0) {
          const resA = segInfo.query(A[q], A[q] + L[q]);
          const resB = segInfo.query(B[q], B[q] + L[q]);
          debug {
            writeln(true, " ", resA, " ", resB);
          }
          ans = (resA == resB);
        } else {
          int a = sa.su[A[q]], b = sa.su[B[q]];
          if (a > b) {
            swap(a, b);
          }
          const res = segLCP.query(a, b);
          debug {
            writeln(false, " ", A[q], " ", B[q], ": ", res);
          }
          ans = (res >= L[q]);
        }
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}


Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from $$$1$$$ to $$$n$$$ and then $$$3n$$$ times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements $$$7n+1$$$ times instead of $$$3n$$$ times. Because it is more random, OK?!You somehow get a test from one of these problems and now you want to know from which one.

In the first line of input there is one integer $$$n$$$ ($$$10^{3} \le n \le 10^{6}$$$). In the second line there are $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$ — the permutation of size $$$n$$$ from the test. It is guaranteed that all tests except for sample are generated this way: First we choose $$$n$$$ — the size of the permutation. Then we randomly choose a method to generate a permutation — the one of Petr or the one of Alex. Then we generate a permutation using chosen method.

If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).

NotePlease note that the sample is not a valid test (because of limitations for $$$n$$$) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.Due to randomness of input hacks in this problem are forbidden.

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;
immutable int N = 10 ^^ 6 + 23;

int[N] fw;

void fwadd(int p, int x) {
    while (p < N) {
        fw[p] += x;
        p += (1 << bsf(p));
    }
}

int fwsum(int p) {
    int ans = 0;
    while (p > 0) {
        ans += fw[p];
        p -= (1 << bsf(p));
    }

    debug { ans.writeln; }

    return ans;
}

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    bool odd = false;
    foreach_reverse (e; arr) {
        odd ^= fwsum(e) & 1;
        fwadd(e, 1);
    }

    debug { odd.writeln; }

    bool PetrOdd = (3 * n) & 1;

    writeln(odd == PetrOdd ? "Petr" : "Um_nik");
}

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

void main() {
  int n;
  debug dbg ();
  readf (" %d", &n);
  auto a = new int[n];
  foreach (i; 0 .. n) {
    readf (" %d", &a[i]);
  }
  auto b = new int[n];
  int merge (int l, int r) {
    if (r - l > 1) {
      immutable m = (l + r) >> 1;
      int res = merge (l, m) ^ merge (m, r);
      int i = l;
      int j = m;
      int k = 0;
      while (i < m && j < r) {
        if (a[i] < a[j]) {
          b[k++] = a[i++];
        } else {
          b[k++] = a[j++];
          res ^= (m - i) & 1;
        }
      }
      while (i < m) b[k++] = a[i++];
      while (j < r) b[k++] = a[j++];
      assert (r - l == k);
      a[l .. r] = b[0 .. k];
      return res;
    }
    return 0;
  }
  int r = merge (0, n);
  assert (r == 0 || r == 1);
  debug stderr.writeln (r);
  debug stderr.writeln (a);
  writeln (r == ((3 * n) & 1) ? "Petr" : "Um_nik");
}

void dbg () {
  foreach (i; 1000 .. 1000000) {
    assert ( (((3 * i) ^ (7 * i + 1)) & 1) == 1);
  }
}


You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.

Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.

5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba

immutable multi = true;

void solve(int tc)
{
	auto alphabet = readString;
	auto word = readString;
	int[char] position;
	foreach(i, ch; alphabet) position[ch] = cast(int)i;
	long sum = 0;
	foreach(i; 1 .. word.length)
	{
		sum += abs(position[word[i]] - position[word[i-1]]);
	}
	sum.writeln;
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
	{
		popChar;
	}
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

// Author: Ivan Kazmenko (gassa@mail.ru)
module solution;
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto k = readln.strip;
		auto s = readln.strip;
		auto t = s.map !(x => k.countUntil (x)).array;
		zip (t, t.drop (1)).map !(q{abs (a[0] - a[1])}).sum.writeln;
	}
}


The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.In the Pindows operating system a strings are the lexemes of the command line — the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command " run.exe one, two . ", we give four lexemes to the Pindows command line: "run.exe", "one,", "two", ".". More formally, if we run a command that can be represented as string s (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string s that contain no spaces.To send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited — that is, for each occurrence of character """ we should be able to say clearly that the quotes are opening or closing. For example, as we run the command ""run.exe o" "" " ne, " two . " " ", we give six lexemes to the Pindows command line: "run.exe o", "" (an empty string), " ne, ", "two", ".", " " (a single space).It is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.You have a string that consists of uppercase and lowercase English letters, digits, characters ".,?!"" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character """ to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters.

The single line contains a non-empty string s. String s consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the ".,?!"" signs, or a space. It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme.

In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the "<" (less) character to the left of your lexemes and the ">" (more) character to the right. Print the lexemes in the order in which they occur in the command. Please, follow the given output format strictly. For more clarifications on the output format see the test samples.

"RUn.exe O" "" "   2ne, " two! . " "
firstarg   second   ""

import std.stdio, std.conv, std.string, std.regex;
void main()
{
    string command;
    string[] lexems;
    auto reg = regex("[^\" ]+|(\"[^\"]*\")");

    command = readln();
    command = strip(command);

    while(true)
    {
        auto m = match(command, reg);
        if (m.empty) break;
        lexems ~= chomp(chompPrefix(m.front.hit, `"`),`"`);
        command = m.post;
    }

    foreach(ref l; lexems)
        writeln("<", l, ">");
}

import std.algorithm;
import std.container;
import std.exception;
import std.math;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	string s;
	while ((s = readln ()) != null)
	{
		while (1)
		{
			s = strip (s);
			if (s.length == 0)
			{
				break;
			}
			string t;
			if (s[0] == '"')
			{
				t = find (s[1..$], '"');
				writefln ("<%s>", s[1..$ - t.length]);
			}
			else
			{
				t = find (s[1..$], ' ');
				writefln ("<%s>", s[0..$ - t.length]);
			}
			s = t;
			if (s.length > 0)
			{
				s = s[1..$];
			}
		}
	}
}

module cf_291B;

import std.stdio;

void main() {
    char symbol;
    bool wasQuote = false;
    string lexem;

    while (readf("%c", &symbol)) {
        if (symbol == '\"') {
            wasQuote = !wasQuote;

            if (!wasQuote) {
                writefln("<%s>", lexem);
            }
            lexem = "";

            continue;
        }

        lexem ~= symbol;
        if (!wasQuote && (symbol == ' ' || symbol == '\n')) {
            lexem = lexem[0 .. $ - 1];
            if (lexem.length > 0) {
                writefln("<%s>", lexem);
                lexem = "";
            }
        }

        if (symbol == '\n') {
            break;
        }
    }
}

module sigod.codeforces.p291B;

import std.array;
import std.stdio;
import std.string;

void main()
{
	auto result = solve(stdin.readln());

	foreach (r; result) {
		stdout.writeln(r);
	}
}

string[] solve(string input)
{
	input = input.strip();

	auto result = appender!(string[])();

	bool quote = false;
	bool without = false;
	int start;

	foreach (index, c; input) {
		if (quote) {
			if (c == '"') {
				result.put('<' ~ input[start + 1 .. index] ~ '>');
				quote = false;
			}
		}
		else if (without) {
			if (c == ' ') {
				result.put('<' ~ input[start .. index] ~ '>');
				without = false;
			}
		}
		else {
			if (c == '"') {
				quote = true;
				start = index;
			}
			else if (c != ' ') {
				without = true;
				start = index;
			}
		}
	}

	if (without) {
		result.put('<' ~ input[start .. $] ~ '>');
	}

	return result.data();
}

unittest {
	assert(std.algorithm.equal(solve(`"RUn.exe O" "" "   2ne, " two! . " "`), [
			"<RUn.exe O>",
			"<>",
			"<   2ne, >",
			"<two!>",
			"<.>",
			"< >"
		]));
	assert(std.algorithm.equal(solve(`   firstarg   second   ""    `), [
			"<firstarg>",
			"<second>",
			"<>"
		]));
}

module cf_291B;

import std.stdio;

void main() {
    char symbol;
    bool wasQuote = false;
    string lexem;

    while (readf("%c", &symbol), symbol != '\n') {
        if (symbol == '\"') {
            wasQuote = !wasQuote;

            if (!wasQuote) {
                writefln("<%s>", lexem);
            }
            lexem = "";

            continue;
        }

        lexem ~= symbol;
        if (!wasQuote && symbol == ' ') {
            lexem = lexem[0 .. $ - 1];
            if (lexem.length > 0) {
                writefln("<%s>", lexem);
                lexem = "";
            }
        }
    }
}


Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).You may perform the following operations until both a and s are empty:  Take the first element of a, push it into s and remove it from a (if a is not empty);  Take the top element from s, append it to the end of array b and remove it from s (if s is not empty). You can perform these operations in arbitrary order.If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:  Remove 3 from a and push it into s;  Remove 1 from a and push it into s;  Remove 1 from s and append it to the end of b;  Remove 2 from a and push it into s;  Remove 2 from s and append it to the end of b;  Remove 3 from s and append it to the end of b. After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k qi = pi, and qk > pk). You may not swap or change any of first k elements of the permutation.Print the lexicographically maximal permutation p you can obtain.If there exists no answer then output -1.

The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively. The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the first k elements of p. These integers are pairwise distinct.

If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation. Otherwise print -1.

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

const int INF = 1 << 29;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;

    auto ans = new int[](N);
    int p = 0;
    auto used = new bool[](N+1);
    foreach (a; A) used[a] = true;
    int[] stack;
    int back = 0;
    auto rbt = new RedBlackTree!(int, "a < b")();
    foreach (i; 1..N+1) if (!used[i]) rbt.insert(i);

    foreach (i; 0..K) {
        while (!stack.empty && stack.back == back + 1) {
            stack.popBack;
            back += 1;
        }
        if (A[i] == back + 1) {
            back = A[i];
        } else if (stack.empty || stack.back > A[i]) {
            stack ~= A[i];
        } else {
            writeln(-1);
            return;
        }
        ans[p++] = A[i];
    }

    while (!rbt.empty) {
        while (!stack.empty && stack.back == back + 1) {
            stack.popBack;
            back += 1;
        }

        int tar = stack.empty ? INF : stack.back;
        auto lb = rbt.lowerBound(tar);
        if (lb.empty) {
            writeln(-1);
            return;
        }
        auto x = lb.back;

        rbt.removeKey(x);
        if (x == back + 1) {
            back += 1;
        } else {
            stack ~= x;
        }
        ans[p++] = x;
    }

    ans.map!(to!string).join(" ").writeln;
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

const int INF = 1 << 29;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!int).array;

    auto ans = new int[](N);
    int p = 0;
    auto used = new bool[](N+1);
    foreach (a; A) used[a] = true;
    int[] stack;
    int back = 0;
    auto rbt = new RedBlackTree!(int, "a < b")();
    foreach (i; 1..N+1) if (!used[i]) rbt.insert(i);

    foreach (i; 0..K) {
        if (A[i] == back + 1) {
            back = A[i];
        } else if (stack.empty || stack.back > A[i]) {
            stack ~= A[i];
        } else {
            writeln(-1);
            return;
        }
        ans[p++] = A[i];
    }

    while (!rbt.empty) {
        while (!stack.empty && stack.back == back + 1) {
            stack.popBack;
            back += 1;
        }

        int tar = stack.empty ? INF : stack.back;
        auto lb = rbt.lowerBound(tar);
        if (lb.empty) {
            writeln(-1);
            return;
        }
        auto x = lb.back;

        rbt.removeKey(x);
        if (x == back + 1) {
            back += 1;
        } else {
            stack ~= x;
        }
        ans[p++] = x;
    }

    ans.map!(to!string).join(" ").writeln;
}


It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array $$$a$$$. Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices $$$x, y, z, w$$$ such that $$$a_x + a_y = a_z + a_w$$$.Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery?

The first line contains the single integer $$$n$$$ ($$$4 \leq n \leq 200\,000$$$) — the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2.5 \cdot 10^6$$$).

Print "YES" if there are such four indices, and "NO" otherwise. If such indices exist, print these indices $$$x$$$, $$$y$$$, $$$z$$$ and $$$w$$$ ($$$1 \le x, y, z, w \le n$$$). If there are multiple answers, print any of them.

NoteIn the first example $$$a_2 + a_3 = 1 + 5 = 2 + 4 = a_1 + a_6$$$. Note that there are other answer, for example, 2 3 4 6.In the second example, we can't choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that $$$a_1 + a_2 = 1 + 3 = 3 + 1 = a_2 + a_3$$$

import std;

alias P = Tuple!(int, int);

void main() {
    int N = readln.chomp.to!int;
    auto A = readln.chomp.split(" ").to!(int[]);
    {
        int[][int] M;
        foreach (i, a; A) {
            M[a] ~= i;
            if (M[a].length >= 4) {
                writeln("YES");
                writefln("%(%s %)", M[a][0 .. 4].map!(i => i + 1));
                return;
            }
        }
    }
    Tuple!(int,int)[][int] C;
    for (int i = 0; i < N; i++) {
        for (int j = i+1; j < N; j++) {
            int s = A[i] + A[j];
            C[s] ~= tuple(i, j);
            C[s] ~= tuple(j, i);
            if (C[s].length >= 4) {
                auto xs = C[s];
                foreach (x; xs) {
                    foreach (y; xs) {
                        if (x[0] == y[0] || x[0] == y[1] || x[1] == y[0] || x[1] == y[1]) continue;
                        writeln("YES");
                        writefln("%(%s %)", [x[0] + 1, x[1] + 1, y[0] + 1, y[1] + 1]);
                        return;
                    }
                }
            }
        }
    }
    writeln("NO");
}

import std;

alias P = Tuple!(int, int);

void main() {
    int N = readln.chomp.to!int;
    auto A = readln.chomp.split(" ").to!(int[]);

    int[][int] M;
    foreach (i, a; A) {
        M[a] ~= i;
    }
    auto X = M.byPair.array.sort!((a, b) => a.value.length > b.value.length).array;
    if (X.front.value.length >= 4) {
        int[] xs = X[0].value;
        writeln("YES");
        writefln("%(%s %)", xs[0 .. 4].map!(i => i + 1));
    } else if (X.length >= 2 && X[0].value.length >= 2 && X[1].value.length >= 2) {
        int[] xs = X[0].value;
        int[] ys = X[1].value;
        writeln("YES");
        writefln("%(%s %)", [xs[0], ys[0], xs[1], ys[1]].map!(i => i + 1));
    } else {
        int n = X.length;
        X.sort!"a.key < b.key";
        Tuple!(int,int)[][int] C;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                int s = X[i].key + X[j].key;
                C[s] ~= tuple(X[i].value[0], X[j].value[0]);
                if (C[s].length == 2) {
                    writeln("YES");
                    writefln("%(%s %)", [C[s][0][0], C[s][0][1], C[s][1][0], C[s][1][1]].map!(i => i + 1));
                    return;
                }
            }
        }
        writeln("NO");
    }
}


Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos.In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos).Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word.

The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters.

Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them.

NoteThe second valid answer to the test from the statement is "heloo".

import std.stdio, std.string, std.algorithm;
import std.conv, std.array, std.container;
import std.typecons;

alias Tuple!(char, int) P;

void main() {
    string s = stdin.readln.chomp;
    Array!P oc;

    int count = 1;
    char ch = s[0];
    foreach (i; 1 .. s.length) {
        if (s[i] == ch) {
            count++;
        } else {
            oc.insert(P(ch, count));
            count = 1;
            ch = s[i];
        }
    }
    oc.insert(P(ch, count));

    foreach (i; 0 .. oc.length) {
        auto o = oc[i];
        if (o[1] >= 2) {
            o[1] = 2;
            if (i + 1 < oc.length) {
                auto p = oc[i + 1];
                if (p[1] >= 2) {
                    p[1] = 1;
                    oc[i + 1] = p;
                }
            }
        }
        oc[i] = o;
    }

    foreach (o; oc) {
        foreach (_; 0 .. o[1]) {
            o[0].write;
        }
    }
    "".writeln;
}