QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#673617#9482. Count Pseudo-Palindromeshos_lyricTL 0ms3856kbC++145.9kb2024-10-25 02:19:142024-10-25 02:19:14

Judging History

你现在查看的是最新测评结果

  • [2024-10-25 02:19:14]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3856kb
  • [2024-10-25 02:19:14]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
// 2^61 - 1 = 2'305'843'009'213'693'951
struct ModLong61 {
  static constexpr unsigned long long M = (1ULL << 61) - 1;
  unsigned long long x;
  constexpr ModLong61() : x(0ULL) {}
  constexpr ModLong61(unsigned x_) : x(x_) {}
  constexpr ModLong61(unsigned long long x_) : x(x_ % M) {}
  constexpr ModLong61(int x_) : x((x_ < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  constexpr ModLong61(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong61 &operator+=(const ModLong61 &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModLong61 &operator-=(const ModLong61 &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModLong61 &operator*=(const ModLong61 &a) {
    const unsigned __int128 y = static_cast<unsigned __int128>(x) * a.x;
    x = (y >> 61) + (y & M);
    x = (x >= M) ? (x - M) : x;
    return *this;
  }
  ModLong61 &operator/=(const ModLong61 &a) { return (*this *= a.inv()); }
  ModLong61 pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModLong61 a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModLong61 inv() const {
    unsigned long long a = M, b = x; long long y = 0, z = 1;
    for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
    assert(a == 1ULL); return ModLong61(y);
  }
  ModLong61 operator+() const { return *this; }
  ModLong61 operator-() const { ModLong61 a; a.x = x ? (M - x) : 0ULL; return a; }
  ModLong61 operator+(const ModLong61 &a) const { return (ModLong61(*this) += a); }
  ModLong61 operator-(const ModLong61 &a) const { return (ModLong61(*this) -= a); }
  ModLong61 operator*(const ModLong61 &a) const { return (ModLong61(*this) *= a); }
  ModLong61 operator/(const ModLong61 &a) const { return (ModLong61(*this) /= a); }
  template <class T> friend ModLong61 operator+(T a, const ModLong61 &b) { return (ModLong61(a) += b); }
  template <class T> friend ModLong61 operator-(T a, const ModLong61 &b) { return (ModLong61(a) -= b); }
  template <class T> friend ModLong61 operator*(T a, const ModLong61 &b) { return (ModLong61(a) *= b); }
  template <class T> friend ModLong61 operator/(T a, const ModLong61 &b) { return (ModLong61(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModLong61 &a) const { return (x == a.x); }
  bool operator!=(const ModLong61 &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModLong61 &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

#include <chrono>
#ifdef LOCAL
mt19937_64 rng(58);
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif

////////////////////////////////////////////////////////////////////////////////


/*
  for each a, count [l, r) s.t. I[0][a] < l <= I[1][a] < r
  fix l
    [l, r) must contain a with min I[1][a] s.t. I[0][a] < l <= I[1][a]
    ==> a appears once in [l, r)
    ==> others should appear even times
*/

int N;
vector<int> A;

int main() {
  for (; ~scanf("%d", &N); ) {
    A.resize(2*N);
    for (int i = 0; i < 2*N; ++i) {
      scanf("%d", &A[i]);
      --A[i];
    }
    
    vector<unsigned long long> rnds(N);
    for (int a = 0; a < N; ++a) rnds[a] = rng();
    
    vector<Int> ans(2*N, 0);
    for (int dir = 0; dir < 2; ++dir) {
      vector<vector<int>> I(N);
      for (int i = 0; i < 2*N; ++i) I[A[i]].push_back(i);
      vector<vector<int>> lss(2*N);
      {
        set<int> is;
        for (int i = 0; i < 2*N; ++i) {
          if (I[A[i]][0] == i) {
            is.insert(I[A[i]][1]);
          } else {
            is.erase(i);
          }
          if (is.size()) lss[*is.begin()].push_back(i + 1);
        }
      }
      vector<unsigned long long> pre(2*N + 1, 0);
      for (int i = 0; i < 2*N; ++i) pre[i + 1] = pre[i] ^ rnds[A[i]];
      map<unsigned long long, int> freq;
      for (int i = 2*N; --i >= 0; ) {
        ++freq[pre[i + 1]];
        for (const int l : lss[i]) {
          auto it = freq.find(pre[l] ^ rnds[A[i]]);
          if (it != freq.end()) {
// cerr<<"A = "<<A<<", i = "<<i<<", l = "<<l<<", *it = "<<*it<<endl;
            ans[i] += it->second;
          }
        }
      }
      
      reverse(A.begin(), A.end());
      reverse(ans.begin(), ans.end());
    }
    
    for (int i = 0; i < 2*N; ++i) {
      if (i) printf(" ");
      printf("%lld", ans[i]);
    }
    puts("");
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3856kb

input:

2
1 1 2 2

output:

1 2 2 1

result:

ok 4 tokens

Test #2:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

3
2 1 2 3 1 3

output:

1 2 2 2 2 1

result:

ok 6 tokens

Test #3:

score: 0
Accepted
time: 0ms
memory: 3800kb

input:

4
1 2 4 3 4 1 3 2

output:

1 2 1 2 1 3 1 1

result:

ok 8 tokens

Test #4:

score: 0
Accepted
time: 0ms
memory: 3756kb

input:

1
1 1

output:

1 1

result:

ok 2 tokens

Test #5:

score: -100
Time Limit Exceeded

input:

500000
233733 151347 468353 495903 234861 297169 312993 2734 287872 359375 79017 285205 219439 37290 409190 194953 306816 472906 123794 121028 66509 62235 385879 300188 485875 72413 167304 333428 33910 220100 151575 22575 206653 82054 111518 34032 48702 198940 6262 222529 170641 1735 38943 235003 11...

output:


result: