[CF] D. Mysterious Crime - Codeforces Round 519 by Botan Investments
Solutions Codeforces 杂题 1700 Med-
Lastmod: 2025-01-14 周二 21:44:12

https://codeforces.com/contest/1043/problem/D

题目大意

给出 $m \ (m \le 10)$ 个 $n \ (n \le 10^5)$ 长的排列,问在所有 $m$ 个数组中都出现的连续子数组有多少个。

简要题解

所有 $1$ 长的显然都符合条件。

对于 $2$ 及以上长度的,其必然在第一个数组中出现。对于 $2$ 长,显然我们只需关注所有第一个数组中出现的连续对的个数就行。而 $3$ 以上长度,必然是由多个这样的 $2$ 的段拼起来的。所以我们只要注意所有 $2$ 长的出现了 $m$ 次的段即可完成统计。

复杂度

$T$:$O(nm \log n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n, m; cin >> n >> m;
  vector<int> a(n), b(n);
  for (int& i : a) {
    cin >> i;
  }
  map<pair<int, int>, int> cnt;
  for (int i = 1; i < n; i++) {
    cnt[{a[i - 1], a[i]}]++;
  }
  for (int j = 1; j < m; j++) {
    for (int& i : b) cin >> i;
    for (int i = 1; i < n; i++) {
      PII p = {b[i - 1], b[i]};
      if (auto it = cnt.find(p); it != cnt.end()) {
        it->second++;
      }
    }
  }

  LL ans = 0;
  int cur = 0;
  for (int i = 1; i < n; i++) {
    PII p = {a[i - 1], a[i]};

    if (cnt[p] == m) {
      cur++;
      ans += cur;
    } else {
      cur = 0;
    }
  }
  cout << (ans + n) << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] D. Scarecrow - Codeforces Round 996 (Div. 2)
Next: [CF] D. Array Division - Educational Codeforces Round 21