Ejemplo n.º 1
0
 public void run() {
   // Scanner sc = new Scanner(System.in);
   Scanner sc = new Scanner();
   StringBuilder sb = new StringBuilder(1000000);
   // System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
   final int N = sc.nextInt();
   final int M = sc.nextInt();
   final int K = (int) floor(sqrt(N));
   final int[] A = new int[N + 1];
   A[0] = 1;
   for (int i = 1; i <= N; i++) A[i] = sc.nextInt();
   final int[] next = new int[N + 1];
   final int[] step = new int[N + 1];
   final int[] last = new int[N + 1];
   for (int i = N; i > 0; i--) {
     int j = i + A[i];
     if (j > N || j / K > i / K) {
       last[i] = i;
       step[i] = 1;
       next[i] = j;
     } else {
       last[i] = last[j];
       step[i] = step[j] + 1;
       next[i] = next[j];
     }
   }
   for (int t = 0; t < M; t++)
     if (sc.nextInt() == 1) {
       int i = sc.nextInt();
       int j = 0;
       int k = 0;
       while (i <= N) {
         j += step[i];
         k = last[i];
         i = next[i];
       }
       sb.append(k).append(' ').append(j).append('\n');
       // System.out.println(k + " " + j);
     } else {
       int k = sc.nextInt();
       int b = k / K * K;
       A[k] = sc.nextInt();
       for (int i = min(b + K - 1, N); i >= b; i--) {
         int j = i + A[i];
         if (j > N || j / K > i / K) {
           last[i] = i;
           step[i] = 1;
           next[i] = j;
         } else {
           last[i] = last[j];
           step[i] = step[j] + 1;
           next[i] = next[j];
         }
       }
     }
   // System.out.flush();
   System.out.print(sb);
 }
Ejemplo n.º 2
0
 @Override
 public String toString() {
   StringBuilder builder = new StringBuilder(size() * 3);
   builder.append('[').append(array[start]);
   for (int i = start + 1; i < end; i++) {
     builder.append(", ").append(array[i]);
   }
   return builder.append(']').toString();
 }
Ejemplo n.º 3
0
  /**
   * Returns a string containing the supplied {@code char} values separated by {@code separator}.
   * For example, {@code join("-", '1', '2', '3')} returns the string {@code "1-2-3"}.
   *
   * @param separator the text that should appear between consecutive values in the resulting string
   *     (but not at the start or end)
   * @param array an array of {@code char} values, possibly empty
   */
  public static String join(String separator, char... array) {
    notNull(separator);
    int len = array.length;
    if (len == 0) {
      return "";
    }

    StringBuilder builder = new StringBuilder(len + separator.length() * (len - 1));
    builder.append(array[0]);
    for (int i = 1; i < len; i++) {
      builder.append(separator).append(array[i]);
    }
    return builder.toString();
  }