Submission #501881

# Submission time Handle Problem Language Result Execution time Memory
501881 2022-01-04T18:08:54 Z 600Mihnea Fish (IOI08_fish) C++17
70 / 100
3000 ms 51384 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Fish {
  int sz;
  int tp;
};

bool operator < (Fish a, Fish b) {
  return a.sz < b.sz;
}

const int N = (int) 5e5 + 7;
int n, k, mod;
Fish a[N];
vector<int> aux[N], oftp[N];

int caneat(int f, int s) {
  int low = 0, high = (int) oftp[s].size() - 1, sol = 0;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (a[oftp[f].back()].sz >= 2 * a[oftp[s][mid]].sz) {
      sol = mid + 1;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return sol;
}

int aux_order[N], aux_inv_order[N];

bool cmp(int i, int j) {
  return aux[i].back() < aux[j].back();
}

int tree[4 * N];

void upd(int v, int tl, int tr, int i, int x) {
  if (tr < i || i < tl) {
    return;
  }
  if (tl == tr) {
    tree[v] = x;
    return;
  }
  int tm = (tl + tr) / 2;
  upd(2 * v, tl, tm, i, x);
  upd(2 * v + 1, tm + 1, tr, i, x);
  tree[v] = tree[2 * v] * (ll) tree[2 * v + 1] % mod;
}

int get(int v, int tl, int tr, int l, int r) {
  if (tr < l || r < tl) {
    return 1;
  }
  if (l <= tl && tr <= r) {
    return tree[v];
  }
  int tm = (tl + tr) / 2;
  return get(2 * v, tl, tm, l, r) * (ll) get(2 * v + 1, tm + 1, tr, l, r) % mod;
}


void upd(int i, int x) {
  upd(1, 1, n, i, x);
}

int get(int l, int r) {
  return get(1, 1, n, l, r);
}

int thesol[N], frq[N];

int main() {
  ios::sync_with_stdio(0); cin.tie(0);

  for (int i = 1; i < 4 * N; i++) {
    tree[i] = 1;
  }

 /// freopen ("input", "r", stdin);

  cin >> n >> k >> mod;
  int init_n = n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].sz >> a[i].tp;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    aux[a[i].tp].push_back(i);
  }
  n = k;
  for (int i = 1; i <= n; i++) {
    aux_order[i] = i;
  }

  sort(aux_order + 1, aux_order + n + 1, cmp);
  for (int i = 1; i <= n; i++) aux_inv_order[aux_order[i]] = i;
  for (int i = 1; i <= n; i++) oftp[i] = aux[aux_order[i]];
  for (int i = 1; i <= n; i++) aux[i] = oftp[i];

  for (int i = 1; i <= init_n; i++) a[i].tp = aux_inv_order[a[i].tp];

  int go = 1;

  for (int i = 1; i <= n; i++) {
    while (2 * a[go].sz <= a[oftp[i].back()].sz) {
      assert(go <= init_n);
      frq[a[go].tp]++;
      upd(a[go].tp, frq[a[go].tp] + 1);
      go++;
    }
    thesol[i] = get(1, i - 1);
  }


  int print = 0;
  for (int i = n; i >= 1; i--) {
    int full = 1, semi = caneat(i, i);
    int last = i, low = i + 1, high = n;
    while (low <= high) {
      int j = (low + high) / 2;
      if (caneat(j, i) == caneat(i, i)) {
        last = j;
        low = j + 1;
      } else {
        high = j - 1;
      }
    }
    for (int j = i + 1; j <= last; j++) {
      full = full * (ll) (caneat(i, j) + 1) % mod;
    }
    int sol = 1;
    for (int j = 1; j < i; j++) {
      sol = sol * (ll) (caneat(i, j) + 1) % mod;
    }
    assert(sol == thesol[i]);
    full = full * (ll) sol % mod;
    semi = semi * (ll) sol % mod;

    print = (print + full) % mod;
    print = (print + semi) % mod;
  }
  cout << print << "\n";

  return 0;
}

/**
**/
# Verdict Execution time Memory Grader output
1 Correct 19 ms 31648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31564 KB Output is correct
2 Correct 17 ms 31692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31688 KB Output is correct
2 Correct 144 ms 39772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 31604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 31656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 35272 KB Output is correct
2 Correct 96 ms 36296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 31820 KB Output is correct
2 Correct 33 ms 31816 KB Output is correct
3 Correct 39 ms 31864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 37448 KB Output is correct
2 Correct 437 ms 40524 KB Output is correct
3 Correct 401 ms 41224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 40608 KB Output is correct
2 Correct 517 ms 40736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 336 ms 37352 KB Output is correct
2 Correct 2216 ms 41028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3065 ms 40428 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 3065 ms 41432 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Execution timed out 3079 ms 40636 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3071 ms 47460 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3090 ms 50212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3095 ms 51384 KB Time limit exceeded
2 Halted 0 ms 0 KB -