Cod sursa(job #2733934)

Utilizator Sho10Andrei Alexandru Sho10 Data 31 martie 2021 09:40:56
Problema Numarare triunghiuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,a[805],tree[30005];
void update(ll pos,ll val){
for(;pos<=30000;pos+=pos&(-pos)){
    tree[pos]+=val;
}
}
ll query(ll pos){
ll res=0;
for(;pos;pos-=pos&(-pos)){
    res+=tree[pos];
}
return res;
}
int32_t main(){
CODE_START;
ifstream cin("nrtri.in");
ofstream cout("nrtri.out");
cin>>n;
for(ll i=1;i<=n;i++)
{
    cin>>a[i];
    update(a[i],1ll);
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
    for(ll j=i+1;j<=n;j++)
    {
        update(a[i],-1ll);
        update(a[j],-1ll);
        ans+=query(min(30000ll,a[i]+a[j]));
        if(a[j]-a[i]>=2)
        ans-=query((a[j]-a[i]-1));
        update(a[i],1ll);
        update(a[j],1ll);
    }
}
cout<<ans/3<<endl;
}