QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#798722 | #9792. Ogre Sort | poetryfactory | WA | 0ms | 3680kb | C++23 | 4.1kb | 2024-12-04 16:30:53 | 2024-12-04 16:30:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define endl '\n'
typedef vector<int> vi;
typedef vector<vi> vvi;
#define lowbit(x) x&-x
template <typename T>
struct SegTree
{
const int START = 1;
int n;
struct node
{
int l, r;
T val, lazy;
int length() const { return (this->r - this->l + 1); }
};
std::vector<node> tree;
SegTree() = default;
SegTree(const std::vector<T>& ini) : n(ini.size() - 1), tree((n + 1) << 2)
{
build(1, START, n, ini);
}
SegTree(int mx) : n(mx), tree((mx + 1) << 2)
{
build(1, START, n);
}
void _pushup(int u)
{
tree[u].val = tree[u << 1].val + tree[u << 1 | 1].val;
}
void _pushdown(int u)
{
const int la = tree[u].lazy;
if (la)
{
tree[u << 1].lazy += la;
tree[u << 1 | 1].lazy += la;
tree[u << 1].val += tree[u << 1].length() * la;
tree[u << 1 | 1].val += tree[u << 1 | 1].length() * la;
tree[u].lazy = 0;
}
}
void build(int u, int l, int r)
{
tree[u].l = l;
tree[u].r = r;
tree[u].lazy = 0;
if (l == r)
{
tree[u].val = 0;
return;
}
const int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
_pushup(u);
}
void build(int u, int l, int r, const std::vector<T>& ini)
{
tree[u].l = l;
tree[u].r = r;
tree[u].lazy = 0;
if (l == r)
{
tree[u].val = ini[l];
return;
}
const int mid = (l + r) >> 1;
build(u << 1, l, mid, ini);
build(u << 1 | 1, mid + 1, r, ini);
_pushup(u);
}
void _modify(int u, int l, int r, const int s, const int t, T x)
{
if (l >= s && r <= t)
{
tree[u].val += x * tree[u].length();
tree[u].lazy += x;
return;
}
const int mid = (l + r) >> 1;
_pushdown(u);
if (mid >= s) _modify(u << 1, l, mid, s, t, x);
if (mid < t) _modify(u << 1 | 1, mid + 1, r, s, t, x);
_pushup(u);
}
T _query(int u, int l, int r, const int s, const int t)
{
if (l >= s && r <= t)
{
return tree[u].val;
}
const int mid = (l + r) >> 1;
_pushdown(u);
T ans = 0;
if (mid >= s) ans += _query(u << 1, l, mid, s, t);
if (mid < t) ans += _query(u << 1 | 1, mid + 1, r, s, t);
_pushup(u);
return ans;
}
void modify(int s, int t, T x) { _modify(1, START, n, s, t, x); }
T query(int s, int t) { return _query(1, START, n, s, t); }
};
void solve()
{
int n;
cin>>n;
vector<int> a(n+1);
vi pos(n+1);
SegTree<int> seg(n+1);
vi premax(n+1);
for(int i = 1;i<=n;++i)
{
cin>>a[i];
pos[a[i]] = i;
premax[i] = max(premax[i-1],a[i]);
}
int res = 0;
vector<pair<int,int>> op(1);
priority_queue<int> pq;
for(int i = 1;i<=n;++i)
{
if(premax[i]!=a[i]) pq.push(a[i]);
}
while(!pq.empty())
{
auto p = pq.top();
pq.pop();
int coverp = seg.query(pos[p],pos[p]);
int coverp1=seg.query(pos[p+1],pos[p+1]);
op.emplace_back(pos[p]+coverp,pos[p+1]+coverp1);
seg.modify(pos[p+1]+1,pos[p],1);
res+=pos[p+1]+coverp1;
pos[p] = pos[p+1];
}
cout<<res<<" "<<op.size()-1<<endl;
for(int i = 1;i<=op.size()-1;++i)
{
cout<<op[i].first<<" "<<op[i].second<<endl;
}
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
while(tc--)
{
solve();
}
return 0;
}
// 5
// 5 4 2 1 3
// 4 5 2 1 3
// 3 4 5 2 1
// 2 3 4 5 1
// 1 2 3 4 5
// 9
// 2 8 7 3 4 1 9 6 5
// 2 7 8 3 4 1 9 6 5
// 2 6 7 8 3 4 1 9 5
// 2 5 6 7 8 3 4 1 9
// 2 4 5 6 7 8 3 1 9
// 2 3 4 5 6 7 8 1 9
// 1 2 3 4 5 6 7 8 9
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
4 1 2 4 3
output:
3 1 4 3
result:
ok Participant's output is correct
Test #2:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
5 2 4 1 3 5
output:
3 2 4 2 4 1
result:
ok Participant's output is correct
Test #3:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
3 1 2 3
output:
0 0
result:
ok Participant's output is correct
Test #4:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
4 1 2 4 3
output:
3 1 4 3
result:
ok Participant's output is correct
Test #5:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
5 2 4 1 3 5
output:
3 2 4 2 4 1
result:
ok Participant's output is correct
Test #6:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
3 1 2 3
output:
0 0
result:
ok Participant's output is correct
Test #7:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
1 1
output:
0 0
result:
ok Participant's output is correct
Test #8:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
5 5 3 4 1 2
output:
4 4 3 1 3 1 5 1 5 1
result:
ok Participant's output is correct
Test #9:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
5 4 1 2 3 5
output:
3 3 4 1 4 1 4 1
result:
ok Participant's output is correct
Test #10:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
5 1 3 4 2 5
output:
2 1 4 2
result:
ok Participant's output is correct
Test #11:
score: -100
Wrong Answer
time: 0ms
memory: 3584kb
input:
5 1 4 5 2 3
output:
4 2 5 2 5 2
result:
wrong answer Integer parameter [name=participant answer] equals to 4, violates the range [0, 3]