結果
問題 | No.1270 Range Arrange Query |
ユーザー | stoq |
提出日時 | 2020-09-18 06:18:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,720 ms / 7,000 ms |
コード長 | 8,150 bytes |
コンパイル時間 | 3,178 ms |
コンパイル使用メモリ | 237,456 KB |
実行使用メモリ | 6,656 KB |
最終ジャッジ日時 | 2024-06-28 05:18:50 |
合計ジャッジ時間 | 17,301 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 193 ms
5,376 KB |
testcase_07 | AC | 1,678 ms
5,504 KB |
testcase_08 | AC | 240 ms
5,376 KB |
testcase_09 | AC | 1,132 ms
5,376 KB |
testcase_10 | AC | 1,066 ms
5,376 KB |
testcase_11 | AC | 2,720 ms
6,656 KB |
testcase_12 | AC | 2,690 ms
6,656 KB |
testcase_13 | AC | 2,692 ms
6,656 KB |
testcase_14 | AC | 36 ms
6,528 KB |
testcase_15 | AC | 45 ms
6,656 KB |
testcase_16 | AC | 59 ms
6,528 KB |
testcase_17 | AC | 63 ms
6,656 KB |
コンパイルメッセージ
main.cpp: In function 'void solve()': main.cpp:382:18: warning: 'len' may be used uninitialized [-Wmaybe-uninitialized] 382 | ans[i] = inv + sg.query(0, n) * len; | ~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:378:8: note: 'len' was declared here 378 | ll len; | ^~~
ソースコード
#define MOD_TYPE 1 #pragma region Macros #include <bits/stdc++.h> using namespace std; #if 0 #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using Int = boost::multiprecision::cpp_int; using lld = boost::multiprecision::cpp_dec_float_100; #endif #if 1 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif using ll = long long int; using ld = long double; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pld = pair<ld, ld>; template <typename Q_type> using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>; constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353); //constexpr ll MOD = 1; constexpr int INF = (int)1e9 + 10; constexpr ll LINF = (ll)4e18; constexpr double PI = acos(-1.0); constexpr double EPS = 1e-9; constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0}; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define REPI(i, m, n) for (int i = m; i < (int)(n); ++i) #define repi(i, n) REPI(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << "\n" #define Yes(n) cout << ((n) ? "Yes" : "No") << "\n" #define possible(n) cout << ((n) ? "possible" : "impossible") << "\n" #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n" #define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n" #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x) cerr << #x << ":" << x << "\n"; struct io_init { io_init() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(30) << setiosflags(ios::fixed); }; } io_init; template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template <typename A, size_t N, typename T> inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template <typename T, typename U> constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept { is >> p.first >> p.second; return is; } template <typename T, typename U> constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept { os << p.first << " " << p.second; return os; } #pragma endregion template <typename T> struct BIT { int n; vector<T> dat; BIT() {} BIT(int n_) : n(n_), dat(n_, 0) {} // 0-indexed void add(int i, T x) { i++; while (i <= n) { dat[i - 1] += x; i += i & -i; } } // [0, i) T sum(int i) { T res = 0; while (i > 0) { res += dat[i - 1]; i -= i & -i; } return res; } // 0-indexed T get(int i) { return sum(i + 1) - sum(i); } // [l, r) T sum(int l, int r) { return sum(r) - sum(l); } void display() { for (int i = 0; i < n; i++) cerr << get(i) << " "; cerr << "\n"; } }; template <typename T, typename E> struct SegmentTree { using F = function<T(T, T)>; using G = function<T(T, E)>; using H = function<E(E, E)>; using P = function<E(E, int)>; int n; F f; G g; H h; P p; T ti; E ei; vector<T> dat; vector<E> laz; SegmentTree() {} SegmentTree( int n_, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), ti(ti), ei(ei), p(p) { init(n_); } SegmentTree( vector<T> &v, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), ti(ti), ei(ei), p(p) { init(v.size()); build(v); } void init(int n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, ti); laz.clear(); laz.resize(2 * n - 1, ei); } void build(const vector<T> v) { for (int i = 0; i < v.size(); i++) dat[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } void eval(int len, int k) { if (laz[k] == ei) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = ei; } T update(int a, int b, E x, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); } T query(int a, int b, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return ti; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(int a, int b) { return query(a, b, 0, 0, n); } void disp() { rep(i, n) cerr << query(i, i + 1) << " "; cerr << "\n"; } }; auto my_min = [](ll a, ll b) { return min(a, b); }; int n; vector<int> a; BIT<ll> bit_l, bit_r; SegmentTree<ll, ll> sg; ll inv = 0; struct Mo { vector<int> left, right, order; vector<vector<bool>> v; int width; int nl, nr, ptr; Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(vector<vector<bool>>(n, vector<bool>(2, 0))) {} void insert(int l, int r) /* [l, r) */ { left.push_back(l); right.push_back(r); } /* ソート */ void build() { order.resize(left.size()); iota(begin(order), end(order), 0); sort(begin(order), end(order), [&](int a, int b) { if (left[a] / width != left[b] / width) return left[a] < left[b]; return right[a] < right[b]; }); } /* クエリを 1 つぶんすすめて, クエリのidを返す */ void process(int &query_id, ll &len) { if (ptr == order.size()) { query_id = -1; return; } const auto id = order[ptr]; while (nl > left[id]) distribute(--nl, 0); while (nr < right[id]) distribute(nr++, 1); while (nl < left[id]) distribute(nl++, 0); while (nr > right[id]) distribute(--nr, 1); query_id = order[ptr++], len = right[id] - left[id]; } inline void distribute(int idx, bool R) { v[idx][R].flip(); if (v[idx][R] ^ !R) add(idx, R); else del(idx, R); } void add(int idx, bool R) { //cerr << "add\n"; //dbg(idx); //dbg(R); if (!R) // left { sg.update(0, a[idx], -1); bit_l.add(a[idx], -1); } else // right { sg.update(a[idx] + 1, n, -1); bit_r.add(a[idx], -1); } inv -= bit_l.sum(a[idx] + 1, n) + bit_r.sum(0, a[idx]); //sg.disp(); } void del(int idx, bool R) { //cerr << "del\n"; //dbg(idx); //dbg(R); if (!R) // left { sg.update(0, a[idx], 1); bit_l.add(a[idx], 1); } else // right { sg.update(a[idx] + 1, n, 1); bit_r.add(a[idx], 1); } inv += bit_l.sum(a[idx] + 1, n) + bit_r.sum(0, a[idx]); //sg.disp(); } }; void solve() { int q; cin >> n >> q; bit_l = BIT<ll>(n), bit_r = BIT<ll>(n); vector<ll> sg_init(n, 0); sg = SegmentTree<ll, ll>(sg_init, my_min, plus<ll>(), plus<ll>(), (ll)1e18, 0LL); a.resize(n); rep(i, n) { cin >> a[i]; a[i]--; } for (int i = n - 1; i >= 0; i--) { inv += bit_r.sum(0, a[i]); bit_r.add(a[i], 1); } //dbg(inv); Mo mo(n); rep(qi, q) { int l, r; cin >> l >> r; l--; mo.insert(l, r); } mo.build(); rep(i, n) sg.update(a[i] + 1, n, 1); //sg.disp(); vector<int> ans(q); rep(qi, q) { int i; ll len; mo.process(i, len); //dbg(i); //dbg(inv); ans[i] = inv + sg.query(0, n) * len; //sg.disp(); } rep(i, q) cout << ans[i] << "\n"; } int main() { solve(); }