题解:CF1167F Scalar Queries
难度适中的 2300,还是不错的。
对于
那么答案就是:
这里有个多余的
考虑枚举
我们从小到大排序就可以将
参考代码如下:
/*
* author: LostKeyToReach
* created time: 2025-05-14 13:37:27
*/
#include <bits/stdc++.h>
#define int long long
#define vi vt<int>
using ll = long long; using pii = std::pair<int, int>;
template<typename T> using vt = std::vector<T>;
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define For(i, a, b) for(int i = (a); i <= (b); ++i)
#define Rof(i, a, b) for(int i = (a); i >= (b); --i)
#define S std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0)
template<typename T> T chkmax(T& x, T y) {
return (x < y) ? (x = y, y) : x;
} template<typename T> T chkmin(T& x, T y) {
return (x > y) ? (x = y, y) : x;
}int fio = (S, 0); constexpr int N = 5e5 + 5, mod = 1e9 + 7;
struct Fenw {vi f; int n;
Fenw(int n) : n(n), f(n + 1, 0) {}
void add(int x, int y) {
for (; x <= n; x += x & -x) (f[x] += y) %= mod;
} int qry(int x) { int ans = 0;
for (; x > 0; x -= x & -x) (ans += f[x]) % mod;
return ans;}
}; int32_t main() {int n, ans = 0, tot = 0; std::cin >> n;
vi a(n + 1); For(i, 1, n) std::cin >> a[i],
(ans += i * (n - i + 1) % mod * a[i] % mod) %= mod;
vt<pii> ord; For(i, 1, n) ord.emplace_back(a[i], i);
std::sort(ord.begin(), ord.end()); Fenw fl(n), fr(n);
for (auto& [v, id] : ord) {
(ans += ((n - id + 1) * fl.qry(id - 1) % mod
+ id * (tot - fr.qry(id) + mod) % mod) % mod * v % mod) %= mod;
fl.add(id, id), fr.add(id, n - id + 1);
(tot += n - id + 1) %= mod;
} std::cout << (ans + mod) % mod << "\n";
}