提出 #55042059
ソースコード 拡げる
#line 1 "../atcoder/past202104-open/m/main.cpp"
#include <iostream>
#line 2 "datastructure/range_map.hpp"
#include <map>
#include <vector>
template <typename T, typename S>
struct range_map {
public:
range_map(bool merge_adjacent = true) : _merge_adjacent(merge_adjacent) {
}
typename std::map<T, std::pair<T, S>>::const_iterator get(T p) const {
auto it = values.upper_bound(p);
if (it == values.begin()) return values.end();
if (std::prev(it)->second.first < p) return values.end();
return std::prev(it);
}
typename std::map<T, std::pair<T, S>>::const_iterator get(
std::pair<T, T> range) const {
auto [l, r] = range;
auto it = get(l);
if (it == values.end()) return values.end();
if (it->second.first < r) return values.end();
return it;
}
void set(std::pair<T, T> range, S x) {
set(range, x, [](T, T, S) {}, [](T, T, S) {});
}
template <class op_insert, class op_erase>
void set(std::pair<T, T> range, S x, const op_insert &f,
const op_erase &g) {
auto [l, r] = range;
auto it_l = values.upper_bound(l);
if (it_l != values.begin() &&
l - T(_merge_adjacent) <= std::prev(it_l)->second.first) {
it_l--;
}
auto it_r = values.upper_bound(r + T(_merge_adjacent));
std::vector<std::tuple<T, T, S>> restore;
restore.reserve(3);
if (it_l != values.end() && it_l->first < l) {
if (it_l->second.second != x) {
restore.emplace_back(it_l->first, l - 1, it_l->second.second);
} else {
l = it_l->first;
}
}
if (it_l != it_r && r < std::prev(it_r)->second.first) {
if (std::prev(it_r)->second.second != x) {
restore.emplace_back(r + 1, std::prev(it_r)->second.first,
std::prev(it_r)->second.second);
} else {
r = std::prev(it_r)->second.first;
}
}
restore.emplace_back(l, r, x);
for (auto it = it_l; it != it_r; it = values.erase(it)) {
g(it->first, it->second.first, it->second.second);
}
for (auto [l, r, x] : restore) {
values[l] = {r, x};
f(l, r, x);
}
}
typename std::map<std::pair<T, T>, S>::const_iterator begin() const {
return values.begin();
}
typename std::map<std::pair<T, T>, S>::const_iterator end() const {
return values.end();
}
protected:
std::map<T, std::pair<T, S>> values;
bool _merge_adjacent;
};
#line 4 "../atcoder/past202104-open/m/main.cpp"
int main() {
int n;
std::cin >> n;
range_map<int, long long> mp;
long long ans = 0;
std::map<int, long long> cnt;
auto f = [&](long long l, long long r, long long x) {
ans -= cnt[x] * (cnt[x] - 1) / 2;
cnt[x] += r - l + 1;
ans += cnt[x] * (cnt[x] - 1) / 2;
};
auto g = [&](long long l, long long r, long long x) {
ans -= cnt[x] * (cnt[x] - 1) / 2;
cnt[x] -= r - l + 1;
ans += cnt[x] * (cnt[x] - 1) / 2;
};
for (int i = 1; i <= n; i++) {
int a;
std::cin >> a;
mp.set({i, i}, a, f, g);
}
int q;
std::cin >> q;
while (q--) {
int l, r, x;
std::cin >> l >> r >> x;
mp.set({l, r}, x, f, g);
std::cout << ans << '\n';
}
}
提出情報
提出日時 |
|
問題 |
M - 等しい数 |
ユーザ |
gyouzasushi |
言語 |
C++ 23 (gcc 12.2) |
得点 |
6 |
コード長 |
3588 Byte |
結果 |
AC |
実行時間 |
2414 ms |
メモリ |
31608 KiB |
ジャッジ結果
セット名 |
Sample |
All |
得点 / 配点 |
0 / 0 |
6 / 6 |
結果 |
|
|
セット名 |
テストケース |
Sample |
sample_01.txt |
All |
sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt |
ケース名 |
結果 |
実行時間 |
メモリ |
sample_01.txt |
AC |
1 ms |
3608 KiB |
test_01.txt |
AC |
1 ms |
3492 KiB |
test_02.txt |
AC |
1 ms |
3472 KiB |
test_03.txt |
AC |
2 ms |
3472 KiB |
test_04.txt |
AC |
2 ms |
3592 KiB |
test_05.txt |
AC |
4 ms |
3548 KiB |
test_06.txt |
AC |
2 ms |
3540 KiB |
test_07.txt |
AC |
812 ms |
8148 KiB |
test_08.txt |
AC |
321 ms |
7696 KiB |
test_09.txt |
AC |
431 ms |
15992 KiB |
test_10.txt |
AC |
304 ms |
3416 KiB |
test_11.txt |
AC |
276 ms |
5904 KiB |
test_12.txt |
AC |
1208 ms |
24540 KiB |
test_13.txt |
AC |
470 ms |
11628 KiB |
test_14.txt |
AC |
343 ms |
11576 KiB |
test_15.txt |
AC |
622 ms |
11700 KiB |
test_16.txt |
AC |
554 ms |
14040 KiB |
test_17.txt |
AC |
835 ms |
14296 KiB |
test_18.txt |
AC |
785 ms |
11256 KiB |
test_19.txt |
AC |
234 ms |
5468 KiB |
test_20.txt |
AC |
82 ms |
3416 KiB |
test_21.txt |
AC |
1487 ms |
18648 KiB |
test_22.txt |
AC |
1185 ms |
16240 KiB |
test_23.txt |
AC |
900 ms |
15968 KiB |
test_24.txt |
AC |
1604 ms |
25856 KiB |
test_25.txt |
AC |
1309 ms |
18392 KiB |
test_26.txt |
AC |
1228 ms |
16276 KiB |
test_27.txt |
AC |
1140 ms |
15984 KiB |
test_28.txt |
AC |
1583 ms |
25952 KiB |
test_29.txt |
AC |
1290 ms |
18648 KiB |
test_30.txt |
AC |
400 ms |
3536 KiB |
test_31.txt |
AC |
1016 ms |
16024 KiB |
test_32.txt |
AC |
2191 ms |
25848 KiB |
test_33.txt |
AC |
1278 ms |
18392 KiB |
test_34.txt |
AC |
1081 ms |
16280 KiB |
test_35.txt |
AC |
918 ms |
16028 KiB |
test_36.txt |
AC |
1868 ms |
25876 KiB |
test_37.txt |
AC |
1741 ms |
18584 KiB |
test_38.txt |
AC |
1067 ms |
16156 KiB |
test_39.txt |
AC |
910 ms |
16156 KiB |
test_40.txt |
AC |
391 ms |
3520 KiB |
test_41.txt |
AC |
1943 ms |
28500 KiB |
test_42.txt |
AC |
2414 ms |
31608 KiB |
test_43.txt |
AC |
1732 ms |
28588 KiB |
test_44.txt |
AC |
1723 ms |
28532 KiB |
test_45.txt |
AC |
1735 ms |
28412 KiB |
test_46.txt |
AC |
1238 ms |
16276 KiB |
test_47.txt |
AC |
707 ms |
12872 KiB |
test_48.txt |
AC |
545 ms |
11844 KiB |
test_49.txt |
AC |
486 ms |
9808 KiB |
test_50.txt |
AC |
382 ms |
3536 KiB |
test_51.txt |
AC |
1018 ms |
16144 KiB |
test_52.txt |
AC |
2045 ms |
25752 KiB |
test_53.txt |
AC |
1248 ms |
18384 KiB |
test_54.txt |
AC |
1065 ms |
16116 KiB |
test_55.txt |
AC |
902 ms |
15716 KiB |