読者です 読者をやめる 読者になる 読者になる

CodeChef: Chef and Array

問題

https://www.codechef.com/problems/CHEFLKJ

素数 N の数列が与えられ、Q 個のクエリが来る。クエリは以下の2種類である。

  1. x 番目の数字を y に変更する。
  2. l 番目から r 番目までの部分列の中で、過半数は同じ数であるか判定する。

解法

2次元BITを構築しておく。2次元BITは値の取得にO((logN)^2)かかってしまうので、出来るだけクエリは投げたくない。

天才という感じがする。

コード

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            pass System Test!
*/

class CHEFLKJ {
  private static class Task {
    void solve(FastScanner in, PrintWriter out) throws Exception {
      int N = in.nextInt();
      int Q = in.nextInt();
      int[] array = in.nextIntArray(N);
      TreeMap<Integer, SparseFenwickTree> bits = new TreeMap<>();
      for (int i = 0; i < N; i++) {
        int a = array[i];

        SparseFenwickTree bit = bits.get(a);
        if (bit == null) bits.put(a, bit = new SparseFenwickTree(N));
        bit.add(i, 1);
      }

      Random random = new Random();
      HashMap<Integer, Integer> cnt = new HashMap<>();
      ArrayList<int[]> v = new ArrayList<>();

      query:
      for (; Q > 0; Q--) {
        int t = in.nextInt();
        if (t == 1) {
          int x = in.nextInt() - 1;
          int y = in.nextInt();
          int prev = array[x];
          array[x] = y;
          bits.get(prev).add(x, -1);
          SparseFenwickTree bit = bits.get(y);
          if (bit == null) bits.put(y, bit = new SparseFenwickTree(N));
          bit.add(x, 1);
        } else {
          cnt.clear();
          v.clear();

          int l = in.nextInt() - 1;
          int r = in.nextInt() - 1;
          for (int i = 0; i < 50; i++) {
            int j = random.nextInt(r - l + 1) + l;
            int id = array[j];
            if (cnt.containsKey(id)) cnt.put(id, cnt.get(id) + 1);
            else cnt.put(id, 1);
          }

          for (Map.Entry<Integer, Integer> entry : cnt.entrySet())
            v.add(new int[]{entry.getValue(), entry.getKey()});

          Collections.sort(v, (o1, o2) -> -Integer.compare(o1[0], o2[0]));

          for (int i = 0; i < Math.min(10, v.size()); i++) {
            int id = v.get(i)[1];
            if (bits.get(id).sum(l, r + 1) > (r - l + 1) / 2) {
              out.println("Yes");
              continue query;
            }
          }

          out.println("No");
        }
      }
    }

    class SparseFenwickTree {
      int N;
      TreeMap<Integer, Integer> data;

      SparseFenwickTree(int N) {
        this.N = N + 1;
        data = new TreeMap<>();
      }

      void add(int k, int val) {
        for (int x = k; x < N; x |= x + 1) {
          Integer d = data.get(x);
          if (d == null) d = 0;
          data.put(x, d + val);
        }
      }

      // [0, k)
      int sum(int k) {
        if (k >= N) k = N - 1;
        int ret = 0;
        for (int x = k - 1; x >= 0; x = (x & (x + 1)) - 1) {
          Integer d = data.get(x);
          if (d == null) d = 0;
          ret += d;
        }
        return ret;
      }

      // [l, r)
      int sum(int l, int r) {
        return sum(r) - sum(l);
      }

      int get(int k) {
        assert (0 <= k && k < N);
        return sum(k + 1) - sum(k);
      }
    }
  }

  /**
   * ここから下はテンプレートです。
   */
  public static void main(String[] args) throws Exception {
    OutputStream outputStream = System.out;
    FastScanner in = new FastScanner();
    PrintWriter out = new PrintWriter(outputStream);
    Task solver = new Task();
    solver.solve(in, out);
    out.close();
  }
  private static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int bufferLength = 0;

    private boolean hasNextByte() {
      if (ptr < bufferLength) {
        return true;
      } else {
        ptr = 0;
        try {
          bufferLength = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (bufferLength <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    double[] nextDoubleArray(int n) {
      double[] array = new double[n];
      for (int i = 0; i < n; i++) {
        array[i] = nextDouble();
      }
      return array;
    }

    double[][] nextDoubleMap(int n, int m) {
      double[][] map = new double[n][];
      for (int i = 0; i < n; i++) {
        map[i] = nextDoubleArray(m);
      }
      return map;
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int n) {
      int[] array = new int[n];
      for (int i = 0; i < n; i++) array[i] = nextInt();
      return array;
    }

    public long[] nextLongArray(int n) {
      long[] array = new long[n];
      for (int i = 0; i < n; i++) array[i] = nextLong();
      return array;
    }

    public String[] nextStringArray(int n) {
      String[] array = new String[n];
      for (int i = 0; i < n; i++) array[i] = next();
      return array;
    }

    public char[][] nextCharMap(int n) {
      char[][] array = new char[n][];
      for (int i = 0; i < n; i++) array[i] = next().toCharArray();
      return array;
    }
  }
}