One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
5
1 2 3 4 5
0
3
8 12 7
3
3
3 2 1
2
Possible pairs in the second example:
- x = 1, y = 2 (season 1 episode 2 season 2 episode 1);
- x = 2, y = 3 (season 2 episode 3 season 3 episode 2);
- x = 1, y = 3 (season 1 episode 3 season 3 episode 1).
In the third example:
- x = 1, y = 2 (season 1 episode 2 season 2 episode 1);
- x = 1, y = 3 (season 1 episode 3 season 3 episode 1).
题意 : 询问存在多少个点对,使得 a[j] >= i && a[i] >= j
两种方法 :
一 : 主席树,这个比较好想,首先我们比较容易保证的条件是 a[j] >= i , 然后对于这个区间我们去寻找有多少个点是 a[i] >= j, 那么问题不就转变成求一个指定区间比一个指定的数大的个数。
代码示例 :
#define ll long long const ll maxn = 2e5+5; const ll mod = 1e9+7; const double eps = 1e-9; const double pi = acos(-1.0); const ll inf = 0x3f3f3f3f; ll n; ll a[maxn]; ll root[maxn]; ll cnt = 1; struct node { ll l, r; ll sum; }t[maxn*20]; void init(){ root[0] = 0; t[0].l = t[0].r = t[0].sum = 0; } void update(ll num, ll &rt, ll l, ll r){ t[cnt++] = t[rt]; rt = cnt-1; t[rt].sum++; if (l == r) return; ll m = (l+r)>>1; if (num <= m) update(num, t[rt].l, l, m); else update(num, t[rt].r, m+1, r); } ll sum = 0; void query(ll i, ll j, ll k, ll l, ll r){ if (l >= k) { sum += t[j].sum-t[i].sum; return; } ll m = (l+r)>>1; if (k <= m) query(t[i].l, t[j].l, k, l, m); query(t[i].r, t[j].r, k, m+1, r); } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n; for(ll i = 1; i <= n; i++){ scanf("%lld", &a[i]); a[i] = min(a[i], n); } for(ll i = 1; i <= n; i++){ root[i] = root[i-1]; update(a[i], root[i], 1, n); } for(ll i = 1; i <= n; i++){ ll l = i+1, r = a[i]; if (l > r) continue; query(root[l-1], root[r], i, 1, n); } printf("%lld ", sum); return 0; }
方法二 : 树状数组
对于这个问题我是只需要满足两个条件就可以的,第一个条件 a[j] >= i 是很好满足的,并且也可以找到 a[j] 的范围,假想一下,如果找到的这个范围内的数都是符合题意的,那么对于我们计算起来就很方便了,因此我们就朝着这个方向去实现,对于当前位置的 i ,我们查询 1 - a[j] 范围,查询完后我们删除所有下标为 i 位置的数就好了,因为 i 是递增的,在以后永远都是不符合要求的。利用树状数组去维护就可以。
代码示例 :
#define ll long long const ll maxn = 2e5+5; const ll mod = 1e9+7; const double eps = 1e-9; const double pi = acos(-1.0); const ll inf = 0x3f3f3f3f; ll a[maxn], c[maxn]; ll n; vector<ll>ve[maxn]; ll lower(ll x) {return x&(-x);} void add(ll x, ll pt){ for(ll i = x; i <= n; i += lower(i)) c[i] += pt; } ll query(ll x){ ll sum = 0; for(ll i = x; i >= 1; i -= lower(i)) sum += c[i]; return sum; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n; for(ll i = 1; i <= n; i++){ scanf("%lld", &a[i]); a[i] = min(a[i], n); add(i, 1ll); ve[a[i]].push_back(i); } ll sum = 0; for(ll i = 1; i <= n; i++){ sum += query(a[i]); for(ll j = 0; j < ve[i].size(); j++){ add(ve[i][j], -1ll); } if (a[i] >= i) sum--; //printf("i = %lld sum %lld ", i, sum); } printf("%lld ", sum>>1); return 0; }
形式二 :
#define ll long long const ll maxn = 2e5+5; const ll mod = 1e9+7; const double eps = 1e-9; const double pi = acos(-1.0); const ll inf = 0x3f3f3f3f; ll a[maxn], c[maxn]; ll n; vector<ll>ve[maxn]; ll lower(ll x) {return x&(-x);} void add(ll x, ll pt){ for(ll i = x; i <= n; i += lower(i)) c[i] += pt; } ll query(ll x){ ll sum = 0; for(ll i = x; i >= 1; i -= lower(i)) sum += c[i]; return sum; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n; for(ll i = 1; i <= n; i++){ scanf("%lld", &a[i]); a[i] = min(a[i], n); add(i, 1); ve[a[i]].push_back(i); } ll sum = 0; for(ll i = 1; i <= n; i++){ if (i +1 <= a[i]) sum += query(a[i])-query(i); for(ll j = 0; j < ve[i].size(); j++){ add(ve[i][j], -1); } } printf("%lld ", sum); return 0; }