結果
問題 | No.1975 Zigzag Sequence |
ユーザー | Cyanmond |
提出日時 | 2022-06-10 22:33:05 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 197 ms / 2,000 ms |
コード長 | 15,642 bytes |
コンパイル時間 | 2,795 ms |
コンパイル使用メモリ | 216,104 KB |
実行使用メモリ | 12,040 KB |
最終ジャッジ日時 | 2024-09-21 06:35:07 |
合計ジャッジ時間 | 7,193 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 19 ms
5,376 KB |
testcase_04 | AC | 8 ms
5,376 KB |
testcase_05 | AC | 167 ms
7,560 KB |
testcase_06 | AC | 107 ms
6,660 KB |
testcase_07 | AC | 20 ms
5,376 KB |
testcase_08 | AC | 107 ms
5,932 KB |
testcase_09 | AC | 159 ms
8,868 KB |
testcase_10 | AC | 111 ms
6,288 KB |
testcase_11 | AC | 127 ms
7,464 KB |
testcase_12 | AC | 29 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 1 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 128 ms
7,504 KB |
testcase_19 | AC | 129 ms
7,376 KB |
testcase_20 | AC | 137 ms
7,692 KB |
testcase_21 | AC | 135 ms
7,700 KB |
testcase_22 | AC | 154 ms
11,912 KB |
testcase_23 | AC | 153 ms
11,916 KB |
testcase_24 | AC | 160 ms
11,912 KB |
testcase_25 | AC | 158 ms
12,040 KB |
testcase_26 | AC | 173 ms
7,520 KB |
testcase_27 | AC | 197 ms
9,600 KB |
testcase_28 | AC | 154 ms
12,032 KB |
testcase_29 | AC | 157 ms
11,904 KB |
testcase_30 | AC | 162 ms
11,904 KB |
testcase_31 | AC | 161 ms
12,036 KB |
testcase_32 | AC | 131 ms
7,120 KB |
testcase_33 | AC | 132 ms
7,124 KB |
testcase_34 | AC | 172 ms
7,820 KB |
testcase_35 | AC | 176 ms
7,808 KB |
ソースコード
#line 2 "ProCon/Library/template/util.hpp" #include <algorithm> #include <array> #include <cassert> #include <cstdint> #include <iostream> #include <iterator> #include <limits> #include <numeric> #include <queue> #include <random> #include <tuple> #include <type_traits> #include <vector> using i8 = std::int8_t; using u8 = std::uint8_t; using i16 = std::int16_t; using i32 = std::int32_t; using i64 = std::int64_t; using u16 = std::uint16_t; using u32 = std::uint32_t; using u64 = std::uint64_t; constexpr i8 operator""_i8(unsigned long long n) noexcept { return static_cast<i8>(n); } constexpr i16 operator""_i16(unsigned long long n) noexcept { return static_cast<i16>(n); } constexpr i32 operator""_i32(unsigned long long n) noexcept { return static_cast<i32>(n); } constexpr i64 operator""_i64(unsigned long long n) noexcept { return static_cast<i64>(n); } constexpr u8 operator""_u8(unsigned long long n) noexcept { return static_cast<u8>(n); } constexpr u16 operator""_u16(unsigned long long n) noexcept { return static_cast<u16>(n); } constexpr u32 operator""_u32(unsigned long long n) noexcept { return static_cast<u32>(n); } constexpr u64 operator""_u64(unsigned long long n) noexcept { return static_cast<u64>(n); } constexpr char eoln = '\n'; template <typename T, T Div = 2> constexpr T infty = std::numeric_limits<T>::max() / Div - 2; template <class T> using RevPriorityQueue = std::priority_queue<T, std::vector<T>, std::greater<T>>; constexpr std::array<std::pair<int, int>, 4> dxy4 = {{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}}; constexpr std::array<i64, 6> infty_list = {infty<short>, infty<int>, infty<i64>, infty<i64, 3>, infty<u32>, infty<u64>}; template <typename T> bool is_infty(const T v) { if (v > 0 and static_cast<u64>(v) > infty<u64>) return false; for (const auto e : infty_list) { if (e == static_cast<i64>(v)) return true; } return false; } class Range { struct Iterator { int itr; constexpr Iterator(const int pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { ++itr; } constexpr bool operator!=(const Iterator &other) const noexcept { return itr != other.itr; } constexpr int operator*() const noexcept { return itr; } }; const Iterator first, last; public: explicit constexpr Range(const int f, const int l) noexcept : first(f), last(std::max(f, l)) {} constexpr Iterator begin() const noexcept { return first; } constexpr Iterator end() const noexcept { return last; } }; class ReversedRange { struct Iterator { int itr; constexpr Iterator(const int pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { --itr; } constexpr bool operator!=(const Iterator &other) const noexcept { return itr != other.itr; } constexpr int operator*() const noexcept { return itr; } }; const Iterator first, last; public: explicit constexpr ReversedRange(const int f, const int l) noexcept : first(l - 1), last(std::min(f, l) - 1) {} constexpr Iterator begin() const noexcept { return first; } constexpr Iterator end() const noexcept { return last; } }; #define SIKICM_REP1(i, r) for (const int i : Range(0, r)) #define SIKICM_REP2(i, l, r) for (int i : Range(l, r)) #define SIKICM_RVP1(i, r) for (const int i : ReversedRange(0, r)) #define SIKICM_RVP2(i, l, r) for (int i : ReversedRange(l, r)) #define SIKICM_SELECT2(a, b, c, name, ...) name #define REP(...) SIKICM_SELECT2(__VA_ARGS__, SIKICM_REP2, SIKICM_REP1)(__VA_ARGS__) #define RVP(...) SIKICM_SELECT2(__VA_ARGS__, SIKICM_RVP2, SIKICM_RVP1)(__VA_ARGS__) #define HRL(n) for ([[maybe_unused]] const int loop_counter : Range(0, n)) template <class Container> constexpr int len(const Container &c) { return static_cast<int>(std::size(c)); } template <typename T> constexpr bool chmin(T &v, const T a) { if (v > a) { v = a; return true; } return false; } template <typename T> constexpr bool chmax(T &v, const T a) { if (v < a) { v = a; return true; } return false; } template <typename T> constexpr T ceil_div(const T x, const T y) { assert(y != 0); assert(x > 0 and y > 0); return (x + y - 1) / y; } template <class Container, class T> constexpr int lwb(const Container &c, const T &val) { return static_cast<int>(std::distance(c.cbegin(), std::lower_bound(c.cbegin(), c.cend(), val))); } template <class Container, class T> constexpr int upb(const Container &c, const T &val) { return static_cast<int>(std::distance(c.cbegin(), std::upper_bound(c.cbegin(), c.cend(), val))); } template <class Container, class F> constexpr int lmp(const Container &c, const F &f) { return static_cast<int>( std::distance(c.cbegin(), std::partition_point(c.cbegin(), c.cend(), f))); } template <class T> auto make_vec(const int n, const T &value) { return std::vector<T>(n, value); } template <class... Args> auto make_vec(const int n, Args... args) { return std::vector<decltype(make_vec(args...))>(n, make_vec(args...)); } template <class T, class... Tail> void renumber(const std::vector<int> &order, std::vector<T> &head, Tail &... tail) { const int n = len(order); std::vector<T> sorted_head(n); REP(i, n) sorted_head[i] = head[order[i]]; head = std::move(sorted_head); if constexpr (sizeof...(Tail) != 0) { renumber(order, tail...); } } template <class Head, class... Tail> std::vector<int> priority_sort(std::vector<Head> &head, std::vector<Tail> &... tail) { const int n = len(head); std::vector<std::tuple<Head, Tail..., int>> res(n); REP(i, n) res[i] = std::make_tuple(head[i], tail[i]..., i); std::sort(res.begin(), res.end()); std::vector<int> order(n); REP(i, 0, n) order[i] = std::get<std::tuple_size_v<std::tuple<Head, Tail...>>>(res[i]); renumber(order, head, tail...); return order; } std::vector<int> iotav(const int n) { std::vector<int> ret(n); std::iota(ret.begin(), ret.end(), 0); return ret; } template <class Container> auto calc_sum(const Container &c) { return std::accumulate(c.cbegin(), c.cend(), static_cast<typename Container::value_type>(0)); } template <typename T> constexpr T ceil_log2(const T x) { int e = 0; while ((static_cast<T>(1) << e) < x) ++e; return e; } template <typename T> constexpr int ceil_pow2(const T x) { return 1 << ceil_log2(x); } #line 5 "ProCon/Library/template/debug.hpp" #include <utility> namespace DebugImpl { template <typename U, typename = void> struct is_specialize : std::false_type {}; template <typename U> struct is_specialize<U, typename std::conditional<false, typename U::iterator, void>::type> : std::true_type {}; template <typename U> struct is_specialize<U, typename std::conditional<false, decltype(U::first), void>::type> : std::true_type {}; template <typename U> struct is_specialize<U, std::enable_if_t<std::is_integral<U>::value, void>> : std::true_type {}; void dump(const char &t) { std::cerr << t; } void dump(const std::string &t) { std::cerr << t; } void dump(const bool &t) { std::cerr << (t ? "true" : "false"); } template <typename U, std::enable_if_t<!is_specialize<U>::value, std::nullptr_t> = nullptr> void dump(const U &t) { std::cerr << t; } template <typename T> void dump(const T &t, std::enable_if_t<std::is_integral<T>::value> * = nullptr) { std::string res; if (is_infty(t)) res = "inf"; if constexpr (std::is_signed<T>::value) { if (is_infty(-t)) res = "-inf"; } if (res.empty()) res = std::to_string(t); std::cerr << res; } template <typename T, typename U> void dump(const std::pair<T, U> &); template <typename T> void dump(const std::pair<T *, int> &); template <typename T> void dump(const T &t, std::enable_if_t<!std::is_void<typename T::iterator>::value> * = nullptr) { std::cerr << "[ "; for (auto it = t.begin(); it != t.end();) { dump(*it); std::cerr << (++it == t.end() ? "" : ", "); } std::cerr << " ]"; } template <typename T, typename U> void dump(const std::pair<T, U> &t) { std::cerr << "( "; dump(t.first); std::cerr << ", "; dump(t.second); std::cerr << " )"; } template <typename T> void dump(const std::pair<T *, int> &t) { std::cerr << "[ "; for (int i = 0; i < t.second; i++) { dump(t.first[i]); std::cerr << (i == t.second - 1 ? "" : ", "); } std::cerr << " ]"; } void trace() { std::cerr << std::endl; } template <typename Head, typename... Tail> void trace(Head &&head, Tail &&... tail) { std::cerr << " "; dump(head); if (sizeof...(tail) != 0) std::cerr << ','; trace(std::forward<Tail>(tail)...); } } // namespace DebugImpl #ifdef SIKI_DEBUG #define vpr(...) \ do { \ std::cerr << "## " << #__VA_ARGS__ << " ="; \ DebugImpl::trace(__VA_ARGS__); \ } while (0) #else #define vpr(...) (void(0)) #endif // https://github.com/NyaanNyaan/library/blob/master/template/debug.hpp #line 2 "ProCon/Library/template/macro.hpp" #define ALL(x) (x).begin(), (x).end() #define pf push_front #define pb push_back #define ef emplace_front #define eb emplace_back #define ppf pop_front #define ppb pop_back #line 4 "main.cpp" #include <bits/stdc++.h> using namespace std; #line 2 "ProCon/Library/nyaan_lib/modint/modint.hpp" template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } int get() const { return x; } static constexpr int get_mod() { return mod; } }; #line 9 "main.cpp" using Fp = ModInt<1000000007>; #line 2 "ProCon/Library/nyaan_lib/misc/compress.hpp" template <class T> struct compress { vector<T> xs; compress(const vector<T>& v) { xs.reserve(v.size()); for (T x : v) xs.push_back(x); sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); } int get(const T& x) const { return lower_bound(xs.begin(), xs.end(), x) - xs.begin(); } inline int operator()(const T& x) const { return get(x); } T operator[](int i) { return xs[i]; } int size() const { return xs.size(); } }; /** * 座標圧縮 */ #line 2 "ProCon/Library/nyaan_lib/segment-tree/segment-tree.hpp" template <typename T, typename F> struct SegmentTree { int N; int size; vector<T> seg; const F f; const T I; SegmentTree(F _f, const T &I_) : N(0), size(0), f(_f), I(I_) {} SegmentTree(int _N, F _f, const T &I_) : f(_f), I(I_) { init(_N); } SegmentTree(const vector<T> &v, F _f, T I_) : f(_f), I(I_) { init(v.size()); for (int i = 0; i < (int)v.size(); i++) { seg[i + size] = v[i]; } build(); } void init(int _N) { N = _N; size = 1; while (size < N) size <<= 1; seg.assign(2 * size, I); } void set(int k, T x) { seg[k + size] = x; } void build() { for (int k = size - 1; k > 0; k--) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } void update(int k, T x) { k += size; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } void add(int k, T x) { k += size; seg[k] += x; while (k >>= 1) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } // query to [a, b) T query(int a, int b) { T L = I, R = I; for (a += size, b += size; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } T &operator[](const int &k) { return seg[k + size]; } // check(a[l] * ... * a[r-1]) が true となる最大の r // (右端まですべて true なら N を返す) template <class C> int max_right(int l, C check) { assert(0 <= l && l <= N); assert(check(I) == true); if (l == N) return N; l += size; T sm = I; do { while (l % 2 == 0) l >>= 1; if (!check(f(sm, seg[l]))) { while (l < size) { l = (2 * l); if (check(f(sm, seg[l]))) { sm = f(sm, seg[l]); l++; } } return l - size; } sm = f(sm, seg[l]); l++; } while ((l & -l) != l); return N; } // check(a[l] * ... * a[r-1]) が true となる最小の l // (左端まで true なら 0 を返す) template <typename C> int min_left(int r, C check) { assert(0 <= r && r <= N); assert(check(I) == true); if (r == 0) return 0; r += size; T sm = I; do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!check(f(seg[r], sm))) { while (r < size) { r = (2 * r + 1); if (check(f(seg[r], sm))) { sm = f(seg[r], sm); r--; } } return r + 1 - size; } sm = f(seg[r], sm); } while ((r & -r) != r); return 0; } }; #line 13 "main.cpp" int main() { auto plus_s = [](Fp a, Fp b) { return a + b; }; int N; cin >> N; vector<int> A(N); for (auto &e : A) cin >> e; auto calc_zig = [&](vector<int> S) { compress cp(S); for (auto &e : S) e = cp(e); SegmentTree<Fp, decltype(plus_s)> seg_l(N, plus_s, Fp(0)), seg_r(N, plus_s, Fp(0)); REP(i, N) seg_l.set(i, Fp(2).pow(i)); REP(i, N) seg_r.set(i, Fp(2).pow(N - i - 1)); seg_l.build(); seg_r.build(); vpr(S); vector<vector<int>> events(len(cp)); REP(i, N) events[S[i]].pb(i); Fp ret = 0; REP(i, len(cp)) { for (const int d : events[i]) { seg_l.update(d, 0); seg_r.update(d, 0); } for (const int d : events[i]) ret += seg_l.query(0, d) * seg_r.query(d + 1, N); } return ret; }; Fp ans = calc_zig(A); for (auto &e : A) e = infty<int> - e; ans += calc_zig(A); cout << ans << endl; }