結果
| 問題 |
No.877 Range ReLU Query
|
| コンテスト | |
| ユーザー |
yosupot
|
| 提出日時 | 2019-09-06 22:51:54 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,452 ms / 2,000 ms |
| コード長 | 5,158 bytes |
| コンパイル時間 | 2,357 ms |
| コンパイル使用メモリ | 211,520 KB |
| 最終ジャッジ日時 | 2025-01-07 16:55:59 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 20 |
ソースコード
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
#ifdef LOCAL
struct PrettyOS {
ostream& os;
bool first;
template <class T> auto operator<<(T&& x) {
if (!first) os << ", ";
first = false;
os << x;
return *this;
}
};
template <class... T> void dbg0(T&&... t) {
(PrettyOS{cerr, true} << ... << t);
}
#define dbg(...) \
do { \
cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
dbg0(__VA_ARGS__); \
cerr << endl; \
} while (false);
#else
#define dbg(...)
#endif
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
return os << "P(" << p.first << ", " << p.second << ")";
}
template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
os << "[";
for (auto d : v) os << d << ", ";
return os << "]";
}
template <class T> struct Fenwick {
int n;
V<T> seg;
Fenwick(int _n = 0) : n(_n), seg(n + 1) {}
/// i番目の要素にxを追加する
void add(int i, T x) {
i++;
while (i <= n) {
seg[i] += x;
i += i & -i;
}
}
/// [0, i)のsum
T sum(int i) {
T s = 0;
while (i > 0) {
s += seg[i];
i -= i & -i;
}
return s;
}
/// [a, b)のsum
T sum(int a, int b) { return sum(b) - sum(a); }
/// sum[0, idx) >= xなる最小のidx(sum[0, n) < x なら n+1)
int sum_lower_bound(T x) {
if (x <= 0) return 0;
int res = 0, len = 1;
while (2 * len <= n) len *= 2;
for (; len >= 1; len /= 2) {
if (res + len <= n && seg[res + len] < x) {
res += len;
x -= seg[res];
}
}
return res + 1;
}
};
template <class D, class I> struct Fenwick2D {
using P = pair<I, I>;
V<P> points;
VV<I> ys;
V<Fenwick<D>> fws;
int lg, sz;
Fenwick2D(V<P> _points) : points(_points) {
sort(points.begin(), points.end());
points.erase(unique(points.begin(), points.end()), points.end());
int n = int(points.size());
lg = 1;
while ((1 << lg) < n) lg++;
sz = 1 << lg;
ys = VV<I>(2 * sz);
for (int i = 0; i < n; i++) ys[sz + i].push_back(points[i].second);
for (int i = sz - 1; i >= 1; i--) {
ys[i] = V<I>(ys[2 * i].size() + ys[2 * i + 1].size());
merge(ys[2 * i].begin(), ys[2 * i].end(), ys[2 * i + 1].begin(),
ys[2 * i + 1].end(), ys[i].begin());
}
fws = V<Fenwick<D>>(2 * sz);
for (int i = 1; i < 2 * sz; i++) {
fws[i] = Fenwick<D>(int(ys[i].size()));
}
}
void add(P p, D x) {
int k =
int(lower_bound(points.begin(), points.end(), p) - points.begin());
k += sz;
while (k) {
int yid = lower_bound(ys[k].begin(), ys[k].end(), p.second) -
ys[k].begin();
fws[k].add(yid, x);
k >>= 1;
}
}
D sum(int a, int b, I lw, I up, int l, int r, int k) {
if (b <= l || r <= a) return D(0);
if (a <= l && r <= b) {
int lid =
lower_bound(ys[k].begin(), ys[k].end(), lw) - ys[k].begin();
int uid =
lower_bound(ys[k].begin(), ys[k].end(), up) - ys[k].begin();
return fws[k].sum(lid, uid);
}
int mid = (l + r) / 2;
return sum(a, b, lw, up, l, mid, 2 * k) +
sum(a, b, lw, up, mid, r, 2 * k + 1);
}
D sum(P lw, P up) {
int a = lower_bound(points.begin(), points.end(), lw.first,
[&](P p, I x) { return p.first < x; }) -
points.begin();
int b = lower_bound(points.begin(), points.end(), up.first,
[&](P p, I x) { return p.first < x; }) -
points.begin();
return sum(a, b, lw.second, up.second, 0, sz, 1);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
using P = pair<ll, ll>;
V<P> a(n);
for (int i = 0; i < n; i++) {
a[i].first = i;
cin >> a[i].second;
}
auto fen2d0 = Fenwick2D<ll, ll>(a);
auto fen2d1 = Fenwick2D<ll, ll>(a);
for (auto p: a) {
fen2d0.add(p, 1);
fen2d1.add(p, p.second);
}
for (int ph = 0; ph < q; ph++) {
int ty, l, r;
ll x;
cin >> ty >> l >> r >> x; l--;
ll y = fen2d0.sum(P(l, x), P(r, TEN(9) + 1));
ll z = fen2d1.sum(P(l, x), P(r, TEN(9) + 1));
cout << z - y * x << "\n";
}
return 0;
}
yosupot