Algorithm


E. Tufurama
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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!

Input

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.

Output

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.

Examples
input
Copy
5
1 2 3 4 5
output
Copy
0
input
Copy
3
8 12 7
output
Copy
3
input
Copy
3
3 2 1
output
Copy
2
Note

Possible pairs in the second example:

  1. x = 1y = 2 (season 1 episode 2  season 2 episode 1);
  2. x = 2y = 3 (season 2 episode 3  season 3 episode 2);
  3. x = 1y = 3 (season 1 episode 3  season 3 episode 1).

In the third example:

  1. x = 1y = 2 (season 1 episode 2  season 2 episode 1);
  2. x = 1y = 3 (season 1 episode 3  season 3 episode 1).

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 3e5;
int n, a[N];
vector<vector<int> > seg;

void build(int idx, int l, int r) {
  if(l == r) {
    seg[idx].push_back(a[l]);
    return;
  }

  int mid = (l + r) / 2;
  build(idx * 2, l, mid);
  build(idx * 2 + 1, mid + 1, r);

  int i, j;
  for(i = 0, j = 0; i < seg[idx * 2].size() && j < seg[idx * 2 + 1].size();)
    if(seg[idx * 2][i] <= seg[idx * 2 + 1][j])
      seg[idx].push_back(seg[idx * 2][i++]);
    else
      seg[idx].push_back(seg[idx * 2 + 1][j++]);

  if(i != seg[idx * 2].size()) {
    for(; i < seg[idx * 2].size(); ++i)
      seg[idx].push_back(seg[idx * 2][i]);
  }

  if(j != seg[idx * 2 + 1].size()) {
    for(; j  <  seg[idx * 2 + 1].size(); ++j)
      seg[idx].push_back(seg[idx * 2 + 1][j]);
  }
}

int s, e, val;
long long get(int idx, int l, int r> {
  if(l > e || r < s)
    return 0;

  if(l >= s && r <= e) {
    int id = lower_bound(seg[idx].begin(), seg[idx].end(), val) - seg[idx].begin();
    return max(int(seg[idx].size()) - id, 0);
  }

  int mid = (l + r) / 2;
  return get(idx * 2, l, mid) + get(idx * 2 + 1, mid + 1, r);
}

int main() {
  scanf("%d", &n);
  for(int i = 1; i <= n; ++i)
  	scanf("%d", a + i);

  seg.resize(4 * N);
  build(1, 1, n);

  long long res = 0;
  for(int i = 1; i < n; ++i) {
  	s = i + 1, e = s + (a[i] - i - 1), val = i;
  	if(e > n)
  		e = n;
  	if(e < s)
  		continue;
  	res += max(get(1, 1, n), 0ll);
  }

  printf("%lld\n", res);

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
1 2 3 4 5
Advertisements

Demonstration


Codeforces Solution-E. Tufurama-Solution in C, C++, Java, Python,Tufurama,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+