QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#330354 | #8050. Random Permutation | ucup-team3099# | TL | 2041ms | 9572kb | C++14 | 11.1kb | 2024-02-17 14:45:07 | 2024-02-17 14:45:10 |
Judging History
answer
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <algorithm>
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
// Range update, range query segment tree
struct LazyContext {
LazyContext(int v = 0) {
sum = v;
}
void reset() {
sum = 0;
}
void operator += (LazyContext o) {
sum += o.sum;
}
// atributes
int sum;
};
struct Node {
Node() {
// neutral element or empty node
mn = 1e9, mx = -1e9;
}
Node(int x) {
// init
mn = mx = x;
}
Node(const Node &l, const Node &r) {
// merge
mn = std::min(l.mn, r.mn);
mx = std::max(l.mx, r.mx);
}
void apply(LazyContext lazy) {
mn += lazy.sum;
mx += lazy.sum;
}
Node intersect(const Node o, int delta) {
Node n;
n.mn = std::max(mn, o.mn) - delta;
n.mx = std::min(mx, o.mx) + delta;
return n;
}
bool valid() {
return mn <= mx;
}
// atributes
int mn, mx;
};
template <class i_t, class e_t, class lazy_cont = int>
class SegmentTree {
public:
void init(std::vector<e_t> base) {
n = (int) base.size();
h = 0;
while((1 << h) < n) h++;
tree.resize(2 * n);
dirty.assign(n, false);
lazy.resize(n);
for(int i = 0; i < n; i++) {
tree[i + n] = i_t(base[i]);
}
for(int i = n - 1; i > 0; i--) {
tree[i] = i_t(tree[i + i], tree[i + i + 1]);
lazy[i].reset();
}
}
i_t qry(int l, int r) {
if(l >= r) return i_t();
l += n, r += n;
push(l), push(r - 1);
i_t lp, rp;
for(; l < r; l /= 2, r /= 2) {
if(l & 1) lp = i_t(lp, tree[l++]);
if(r & 1) rp = i_t(tree[--r], rp);
}
return i_t(lp, rp);
}
void upd(int l, int r, lazy_cont lc) {
if(l >= r) return;
l += n, r += n;
push(l), push(r - 1);
int l0 = l, r0 = r;
for(; l < r; l /= 2, r /= 2) {
if(l & 1) apply(l++, lc);
if(r & 1) apply(--r, lc);
}
build(l0), build(r0 - 1);
}
// don't copy this for most segment tree problems!
// search first x in [l, r) such that [l, x) triggers f([l, x))
template<class F>
std::pair<int, i_t> searchPrefix(int l, int r, F f) {
// assert(l < r);
if(l >= r) return std::pair<int, i_t>(r, i_t()); // careful with this case
int lst = r;
l += n, r += n;
push(l), push(r-1);
static int pref[30], suf[30];
int s[2] = {0, 0};
for(; l < r; l /= 2, r /= 2) {
if(l & 1) pref[s[0]++] = l++;
if(r & 1) suf[s[1]++] = --r;
}
i_t cur;
for(int rep = 0; rep < 2; rep++) {
if(rep == 1) { std::reverse(suf, suf + s[1]); }
for(int id = 0; id < s[rep]; id++) {
int i = rep == 0 ? pref[id] : suf[id];
if(f(i_t(cur, tree[i]))) {
while(i < n) {
pushNode(i);
i += i;
i_t other = i_t(cur, tree[i]);
if(!f(other)) {
cur = other;
i++;
}
}
return std::pair<int, i_t>(i - n, cur);
}
cur = i_t(cur, tree[i]);
}
}
return std::pair<int, i_t>(lst, cur);
}
template<class F>
std::pair<int, i_t> searchSuffix(int l, int r, F f) {
// assert(l < r);
if(l >= r) return std::pair<int, i_t>(r, i_t()); // careful with this case
int lst = l;
l += n, r += n;
push(l), push(r-1);
static int pref[30], suf[30];
int s[2] = {0, 0};
for(; l < r; l /= 2, r /= 2) {
if(l & 1) pref[s[1]++] = l++;
if(r & 1) suf[s[0]++] = --r;
}
i_t cur;
for(int rep = 0; rep < 2; rep++) {
if(rep == 1) { std::reverse(pref, pref + s[1]); }
for(int id = 0; id < s[rep]; id++) {
int i = rep == 1 ? pref[id] : suf[id];
if(f(i_t(cur, tree[i]))) {
while(i < n) {
pushNode(i);
i += i + 1;
i_t other = i_t(cur, tree[i]);
if(!f(other)) {
cur = other;
i--;
}
}
return std::pair<int, i_t>(i - n, cur);
}
cur = i_t(cur, tree[i]);
}
}
return std::pair<int, i_t>(lst, cur);
}
private:
int n, h;
std::vector<bool> dirty;
std::vector<i_t> tree;
std::vector<lazy_cont> lazy;
void apply(int p, lazy_cont &lc) {
tree[p].apply(lc);
if(p < n) {
dirty[p] = true;
lazy[p] += lc;
}
}
void push(int p) {
for(int s = h; s > 0; s--) {
int i = p >> s;
pushNode(i);
}
}
inline void pushNode(int i) {
if(dirty[i]) {
apply(i + i, lazy[i]);
apply(i + i + 1, lazy[i]);
lazy[i].reset();
dirty[i] = false;
}
}
void build(int p) {
for(p /= 2; p > 0; p /= 2) {
tree[p] = i_t(tree[p + p], tree[p + p + 1]);
if(dirty[p]) {
tree[p].apply(lazy[p]);
}
}
}
};
const int ms = 300300;
int cnt[2 * ms];
long long solve(const std::vector<int> &a, int l, int mid, int r) {
int n = (int) a.size();
l = std::max(l, 0);
r = std::min(r, n-1);
cnt[ms]++;
for(int sum = ms, i = mid-1; i >= l; i--) {
sum -= a[i] < a[mid] ? -1 : 1;
cnt[sum]++;
}
long long ans = 0;
ans += cnt[ms];
ans += cnt[ms-1];
for(int sum = ms, i = mid+1; i <= r; i++) {
sum += a[i] < a[mid] ? -1 : 1;
ans += cnt[sum] + cnt[sum-1];
}
cnt[ms]--;
for(int sum = ms, i = mid-1; i >= l; i--) {
sum -= a[i] < a[mid] ? -1 : 1;
cnt[sum]--;
}
return (1LL + a[mid]) * ans;
}
long long brute(const std::vector<int> a) {
int n = (int) a.size();
long long ans = 0;
for(int i = 0; i < n; i++) {
std::vector<int> cur;
for(int j = i; j < n; j++) {
cur.push_back(a[j]);
std::sort(cur.begin(), cur.end());
ans += 1 + cur[(j - i) / 2];
}
}
return ans;
}
int main() {
std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n), b(n);
for(int i = 0; i < n; i++) {
a[i] = i+1;
std::cin >> a[i];
}
//std::shuffle(a.begin(), a.end(), rng);
for(int i = 0; i < n; i++) {
b[--a[i]] = i;
}
SegmentTree<Node, int, LazyContext> tree;
tree.init(std::vector<int>(n+1, 0));
for(int i = 1; i <= n; i++) {
tree.upd(i, n+1, LazyContext(1));
}
//long long sum = 0;
long long ans = 0;
for(auto id : b) {
tree.upd(id+1, n+1, LazyContext(-1));
auto range = tree.qry(0, id+1).intersect(tree.qry(id+1, n+1), 1);
// int l = 0, r = id+1;
// while(l != r) {
// int mid = (l + r) / 2;
// if(tree.qry(0, mid).intersect(range, 0).valid()) {
// r = mid;
// } else {
// l = mid + 1;
// }
// }
// int left = l;
//std::cout << "left is " << left << " and searching got " << tree.searchPrefix(0, id+1, [&](Node node) { return node.intersect(range, 0).valid(); }).first << std::endl;
// assert(left == 1 + tree.searchPrefix(0, id+1, [&](Node node) { return node.intersect(range, 0).valid(); }).first);
int left = 1 + tree.searchPrefix(0, id+1, [&](Node node) { return node.intersect(range, 0).valid(); }).first;
// l = id+1, r = n+1;
// while(l != r) {
// int mid = (l + r + 1) / 2;
// if(tree.qry(mid, n+1).intersect(range, 0).valid()) {
// l = mid;
// } else {
// r = mid - 1;
// }
// }
// int right = r;
// assert(right == tree.searchSuffix(id+1, n+1, [&](Node node) { return node.intersect(range, 0).valid(); }).first);
int right = tree.searchSuffix(id+1, n+1, [&](Node node) { return node.intersect(range, 0).valid(); }).first;
//sum += (right - left + 1);
//left = 0, right = n-1;
tree.upd(id+1, n+1, LazyContext(-1));
ans += solve(a, left - 2, id, right + 2);
}
std::cout << ans << '\n';
//std::cout << "should be " << brute(a) << '\n';
}
/*
NEVER FORGET TO:
Look at the problem's constraints before coding.
How to cheese cf:
Find a lower bound or upper bound for the problem. Have faith that it is the answer of the problem.
If it isn't the answer, have more faith or change to another bound god by looking for a better bound.
Trust guesses. Who has time to think? If people in div2 AC the problem it requires no proof since people don't prove things.
You must draw cases. Thinking gets you nowhere, so draw cases and reach illogical conclusions from them.
Sometimes drawing cases is bad because it takes too much time. Faster is to not think at all and just code a bruteforce solution.
This is called "law of small numbers". If something works for small numbers, surely it works for big numbers.
https://en.wikipedia.org/wiki/Faulty_generalization#Hasty_generalization don't mind the "faulty" part of it, in competitive programming mistakes are lightly punished
Don't think about them being right or not, cf is a battle of intuition only.
Be as stupid as possible in implementation. Trying to be smart is an easy way to get WA.
Think about 2x2 cases for matrix problems and hope that everything works for the general case.
Find a necessary condition and trust it to be sufficient. They're basically the same thing.
Heuristics might speed up your code. Forget about complexity, it's only about ACing and not proving that your solution is good.
For paths in a grid starting at (1, i) or something like that, assume that they never cross and do D&C
Consider doing problems in reverse order of queries/updates
For combinatorics problems, consider symmetry
General strategy (MUST DO):
Try to solve the problem with more restricted constraints.
About testing:
Test n=1, a[i]=1, a[i]=n, etc. Basically, test low values. No need to test if pretests are strong, but if you get WA it's good.
This isn't a joke. Do it if you get stuck. It's shit practice in my opinion, but do it if you want AC.
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3816kb
input:
4 1 4 2 3
output:
22
result:
ok 1 number(s): "22"
Test #2:
score: 0
Accepted
time: 693ms
memory: 6308kb
input:
100000 56449 21738 74917 44834 36187 96576 37204 28451 3444 13029 66039 8955 51445 30706 27229 37159 66052 16691 70389 29935 44984 3648 75082 73600 76621 28345 5298 37940 49412 85260 92029 18185 84398 10233 79227 98312 96649 30680 65206 38879 75397 26951 11294 58085 37297 97167 59252 44104 4058 3796...
output:
250202478701074
result:
ok 1 number(s): "250202478701074"
Test #3:
score: 0
Accepted
time: 2041ms
memory: 9572kb
input:
200000 70900 189146 39056 135530 191967 133789 108558 81993 132081 113826 54456 127761 27836 64897 87159 105191 109446 81230 75525 90396 75756 50200 43091 151358 100791 193998 157161 119352 176873 120724 134471 155040 138534 182263 161855 4577 124893 199710 60764 146460 75314 43696 129155 64816 1390...
output:
1998651699409551
result:
ok 1 number(s): "1998651699409551"
Test #4:
score: -100
Time Limit Exceeded
input:
300000 291335 150122 292004 129126 26666 242892 124454 146198 257953 17400 245611 108131 68345 266915 44851 146376 110324 211968 251982 127849 152791 204625 213884 144643 137727 113838 115018 225220 169217 151 284989 29747 37230 110534 124886 224954 253706 175150 103605 289983 165895 113248 32809 28...