cf1703F
原题链接:https://codeforces.com/contest/1703/problem/F
题目背景:
找出满足 1 <= i,j <= n 和 ai < i < aj < j 的所有对。
思路:
只需开一个大小为 n 的哈希数组,将所有值小于下标的值都加到哈希数组中并标记,并对哈希数组进行后缀和处理,将所有有标记的值累加到答案里即可。
数据范围:
n 总和不超过 2e5。
时间复杂度:
O(n)。
ac代码:
#include <bits/stdc++.h>#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<bool> vb;const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cout << a[i] << ' ';return os;
}template <class T>
istream &operator>>(istream &in, vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cin >> a[i];return in;
}/* 有乘就强转,前缀和开ll */void solve()
{int n;cin >> n;vi m(n + 10, 0);vb st(n + 10, 0);for (int i = 1; i <= n; ++i){int x;cin >> x;if (x < i)m[x]++, st[i] = 1;}for (int i = n; i; --i)m[i] += m[i + 1];ll ans = 0;for (int i = 1; i <= n; ++i)if (st[i])ans += m[i + 1];cout << ans << endl;
}int main()
{ioscc;int T;cin >> T;while (T--)solve();return 0;
}