結果
| 問題 |
No.2338 Range AtCoder Query
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-06-02 23:37:50 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 878 ms / 4,000 ms |
| コード長 | 3,690 bytes |
| コンパイル時間 | 3,282 ms |
| コンパイル使用メモリ | 259,948 KB |
| 実行使用メモリ | 14,436 KB |
| 最終ジャッジ日時 | 2024-12-28 23:44:44 |
| 合計ジャッジ時間 | 22,167 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 34 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
struct Mo {
explicit Mo(const std::vector<int>& ls, const std::vector<int>& rs)
: n(ls.size()), ptr(0), nl(0), nr(0), ls(ls), rs(rs) {
const int width = std::round(std::sqrt(n));
order.resize(n);
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(),
[&ls, &rs, width](const int a, const int b) -> bool {
if (ls[a] / width != ls[b] / width) return ls[a] < ls[b];
return (ls[a] / width) & 1 ? rs[a] < rs[b] : rs[a] > rs[b];
});
}
int process() {
if (ptr == n) [[unlikely]] return -1;
const int id = order[ptr++];
while (ls[id] < nl) add(--nl);
while (nr < rs[id]) add(nr++);
while (nl < ls[id]) del(nl++);
while (rs[id] < nr) del(--nr);
return id;
}
void add(const int idx) const;
void del(const int idx) const;
int nl, nr;
private:
const int n;
int ptr;
std::vector<int> ls, rs, order;
};
constexpr int N = 200000, M = 200000;
int p[M]{}, num[N]{}, nxt[N]{};
bool s[M]{};
int al[M]{}, ac[M]{};
int atc = 0;
ll penalty = 0;
void Mo::add(const int idx) const {
const int prob = p[idx];
if (idx == nl) {
if (s[idx]) {
if (ac[prob] == 0) {
++atc;
} else {
penalty -= num[nxt[idx]] - num[idx] - 1;
}
++ac[prob];
} else {
if (ac[prob] > 0) ++penalty;
}
} else {
if (s[idx]) {
if (ac[prob] == 0) {
++atc;
penalty += al[prob];
}
++ac[prob];
}
}
++al[prob];
// cout << "[add] " << idx << ' ' << atc << ' ' << penalty << '\n';
}
void Mo::del(const int idx) const {
const int prob = p[idx];
--al[prob];
if (idx + 1 == nl) {
if (s[idx]) {
--ac[prob];
if (ac[prob] == 0) {
--atc;
} else {
penalty += num[nxt[idx]] - num[idx] - 1;
}
} else {
if (ac[prob] > 0) --penalty;
}
} else {
if (s[idx] && --ac[prob] == 0) {
--atc;
penalty -= al[prob];
}
}
// cout << "[del] " << idx << ' ' << atc << ' ' << penalty << '\n';
}
int main() {
int n, m, q; cin >> n >> m >> q;
REP(i, n) {
string ac_or_wa; cin >> p[i] >> ac_or_wa; --p[i];
s[i] = (ac_or_wa == "AC");
}
vector<int> last(m, -1);
REP(i, n) {
if (last[p[i]] != -1) num[i] = num[last[p[i]]];
++num[i];
last[p[i]] = i;
}
ranges::fill(last, -1);
fill(nxt, nxt + n, -1);
REP(i, n) {
if (s[i]) {
if (last[p[i]] != -1) nxt[last[p[i]]] = i;
last[p[i]] = i;
}
}
vector<int> l(q), r(q); REP(i, q) cin >> l[i] >> r[i], --l[i];
Mo mo(l, r);
vector<int> ans1(q);
vector<ll> ans2(q);
REP(_, q) {
const int index = mo.process();
ans1[index] = atc;
ans2[index] = penalty;
}
REP(i, q) cout << ans1[i] << ' ' << ans2[i] << '\n';
return 0;
}
emthrm