codeforces 459D D. Pashmak and Parmida's problem(이산화+세그먼트 트리 또는 트리 배열 역순)

13233 단어
제목 링크:
D. Pashmak and Parmida's problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r andak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
input
7
1 2 1 1 2 2 1

output
8

input
3
1 1 1

output
1

input
5
1 2 3 4 5

output
0
:f[l,r,x]= a[l],a[l+1]...a[r] a[i] x, f[1,i,a[i]]>f[j,n,a[j]]&&1<=i<j<=n i j , pre[i] a[i] [1,i] , nex[j] a[j] [j,n] a[j] , sigma{pre[i] nex[i+1] }i 1 n-1; ( ), , , ;
AC :

/*~~~~~~~~ ~~~~~~~~~~~~~*/
#include <bits/stdc++.h> using namespace std; const int N=1e6+4; int n,a[N],pre[N],nex[N]; struct nod { int l,r,sum; }; nod tree[4*N]; void build(int node,int le,int ri) { tree[node].l=le; tree[node].r=ri; tree[node].sum=0; if(le==ri)return ; int mid=(le+ri)>>1; build(2*node,le,mid); build(2*node+1,mid+1,ri); tree[node].sum=tree[2*node].sum+tree[2*node+1].sum; } int query(int node,int L,int R) { if(L<=tree[node].l&&R>=tree[node].r) { return tree[node].sum; } int mid=(tree[node].l+tree[node].r)>>1; if(R<=mid)return query(2*node,L,R); else if(L>mid)return query(2*node+1,L,R); else return query(2*node,L,R)+query(2*node+1,L,R); } int update(int node,int num) { if(tree[node].l==tree[node].r&&tree[node].l==num) { tree[node].sum+=1; return 0; } int mid=(tree[node].l+tree[node].r)>>1; if(num<=mid)update(2*node,num); else update(2*node+1,num); tree[node].sum=tree[2*node].sum+tree[2*node+1].sum; } map<int,int>mp1,mp2; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); mp1[a[i]]++; pre[i]=mp1[a[i]];// map } for(int i=n;i>0;i--) { mp2[a[i]]++; nex[i]=mp2[a[i]]; } long long ans=0; build(1,1,n+1);// n+1, nex[i]+1>n; for(int i=1;i<=n;i++) { if(nex[i]!=n) ans+=(long long)query(1,nex[i]+1,n); update(1,pre[i]); } cout<<ans<<"
"; return 0; }

 
/*~~~~~~~~       ~~~~~~~~~~                ?*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+4;
int n,a[N],pre[N],nex[N],sum[N];
int lowbit(int x)
{
    return x&(-x);
}
void update(int x)
{
    while(x<=n)
    {
        sum[x]++;
        x+=lowbit(x);
    }
}
int query(int x)
{
    int s=0;
    while(x>0)
    {
        s+=sum[x];
        x-=lowbit(x);
    }
    return s;
}
map<int,int>mp1,mp2;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        mp1[a[i]]++;
        pre[i]=mp1[a[i]];
    }
    for(int i=n;i>0;i--)
    {
        mp2[a[i]]++;
        nex[i]=mp2[a[i]];
    }
    long long ans=0;
    for(int i=n;i>0;i--)
    {
        ans+=(long long)query(pre[i]-1);
        update(nex[i]);
    }
    cout<<ans<<"
"; return 0; }

 


좋은 웹페이지 즐겨찾기