QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#818364 | #9663. Reverse Pairs Coloring | ccsurzw | WA | 0ms | 3488kb | C++23 | 1.8kb | 2024-12-17 19:29:03 | 2024-12-17 19:29:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct BIT
{
const int n;
vector<int> tree;
BIT(int n) : n(n), tree(n + 1) {};
// 询问前x个数的和
int Query(int x)
{
int res = 0;
for (int i = x; i > 0; i -= (i & -i)) res += tree[i];
return res;
}
// 第l个位置+z
void Modify(int l, int z)
{
if (l <= 0) return;
for (int i = l; i <= n; i += (i & -i)) tree[i] += z;
}
// 区间求和
int rangeQuery(int l, int r)
{
return Query(min(n, r)) - Query(max(0ll, l - 1));
}
};
void solve()
{
int n; cin >> n;
vector<int> a(n + 1), pos(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
pos[a[i]] = i;
}
vector<vector<int>> mem(n + 1);
vector<bool> st(n + 1);
for (int i = 1; i <= n; i++)
{
int t = pos[a[i] - 1] + 1;
if (i >= t)
{
mem[t].push_back(i);
st[a[i]] = true;
}
}
/* for (int i = 1; i <= n; i++)if (st[a[i]])cout << a[i] << endl;*/
BIT bit(n);
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (auto t : mem[i]) {
bit.Modify(a[t], 1);//
cout << a[t] << ' ';
}
if (st[a[i]]) {
bit.Modify(a[i], -1);//
cout <<"a[i] = " << a[i] << ' ';
}
if (a[i - 1] + 1 <= a[i] - 1) {
ans += bit.rangeQuery(a[i - 1] + 1, a[i] - 1);
cout <<"a[i-1]+1=" << a[i - 1] + 1 << " a[i]-1=" << a[i] - 1;
}
cout << endl;
}
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3488kb
input:
9 5 9 1 8 2 6 4 7 3
output:
1 a[i-1]+1=1 a[i]-1=4 6 a[i-1]+1=6 a[i]-1=8 a[i] = 1 2 a[i-1]+1=2 a[i]-1=7 a[i] = 2 3 a[i] = 6 a[i-1]+1=3 a[i]-1=5 7 a[i] = 7 a[i-1]+1=5 a[i]-1=6 a[i] = 3 5
result:
wrong answer 1st lines differ - expected: '5', found: '1 a[i-1]+1=1 a[i]-1=4'