Cod sursa(job #1654758)

Utilizator bogdanboboc97Bogdan Boboc bogdanboboc97 Data 17 martie 2016 14:19:58
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <numeric>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <map>
#define pb push_back
#define mp make_pair
#define INF numeric_limits<int>::max()
#define bit(x) (-x)&x
#define int64 long long
using namespace std;
ifstream in("nrtri.in");
ofstream out("nrtri.out");
#define N 30001
vector<int> aib(N+1);
void update(int pos)
{
    for(int i=pos;i<=N;i+=bit(i))
        aib[i]++;
}
int sum(int pos)
{
    int s=0;
    for(int i=pos;i>0;i-=bit(i))
        s+=aib[i];
    return s;
}
int countTri(int x,int y)
{
    return sum(x+y)-sum(max(x,y)-1)-1;
}
int v[808],n;
int main()
{
    in>>n;
    for(int i=1;i<=n;i++)
    {
        in>>v[i];
        update(v[i]+1);
    }
    int sol=0;
    for(int i=1;i<=n;i++)
    for(int j=i+1;j<=n;j++)
        sol+=countTri(v[i]+1,v[j]+1);
    out<<sol<<'\n';
    return 0;
}