首页 > 其他分享 >D. Pashmak and Parmida's problem 树状数组

D. Pashmak and Parmida's problem 树状数组

时间:2023-04-24 22:38:45浏览次数:27  
标签:Parmida typedef int long maxn Pashmak problem include outputCopy


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 and ak = 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
inputCopy
7
1 2 1 1 2 2 1
outputCopy
8
inputCopy
3
1 1 1
outputCopy
1
inputCopy
5
1 2 3 4 5
outputCopy
0

先预处理一下,然后逆序对搞一搞就好

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long int ull;

typedef pair<int,int> pii;
int n;
int a[maxn];
map<int,int>mp;
int b[maxn],c[maxn];
void add(int x)
{
    for(;x<=n;x+=x&(-x))a[x]++;
}
ll query(int x)
{
    ll ans=0;
    for(;x;x-=x&(-x))ans+=a[x];
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
  //  int n;
    cin>>n;
    int i,j;
    for(i=1;i<=n;i++)cin>>a[i];
    for(i=1;i<=n;i++){
        mp[a[i]]++;
        b[i]=mp[a[i]];
    }
    mp.clear();
    for(i=n;i>=1;i--){
        mp[a[i]]++;
        c[i]=mp[a[i]];
    }
    memset(a,0,sizeof(a));
    ll res=0;
    for(i=n-1;i>=2;i--){
        add(c[i+1]);
        res+=query(b[i]-1);
    }
    cout<<res<<endl;
}


标签:Parmida,typedef,int,long,maxn,Pashmak,problem,include,outputCopy
From: https://blog.51cto.com/u_15657999/6221928

相关文章

  • Problem G: 距离向量算法(D-V)
    ProblemDescriptionRIP路由协议是一种分布式的基于距离向量的路由选择协议。距离向量(D-V)算法:收到相邻路由器(设其地址为X)的一个RIP报文: ①先修改此RIP报文中的所有项目:把“下一跳”字段中的地址都改为X,并把所有的“距离”字段的值加1。 ②对修改后的RIP报文中的每一......
  • Problem I: base64编码
    ProblemDescription对于任意二进制文件(如图像、声音、视频、程序等),都可以用base64编码。base64编码方法:先把二进制代码划分为一系列24位长的单元,然后把每一个24位单元划分为4个6位的组。每一个6位组按下列方法转换为ASCII码。6位二进制有64个不同的值,0-63。用A表示0,B表示1,等。2......
  • Problem D: IP地址合法性和网络类别检查
    1defIslegal(a):2ip=list(map(int,a))3flag=0#标志是否合法4if0<=ip[0]<=255:5if0<=ip[1]<=255:6if0<=ip[2]<=255:7if0<=ip[3]<=255:8......
  • Problem E: 计算网络地址
    1defcalculate(m,n):2ans=[]3a=m.split('.',3)4b=n.split('.',3)5ip=list(map(int,a))6mask=list(map(int,b))7foriinrange(4):8x=ip[i]&mask[i]9ans.append......
  • AtCoder Problem Difficulty
    ABC299之前.......
  • AtCoder Regular Contest 111 F Do you like query problems?
    洛谷传送门AtCoder传送门挺有意思的计数。计数感觉很难做,不妨转成期望,期望又可以转成概率之和。考虑枚举\(w\in[0,m-1]\),把\(>w\)的数设为\(1\),\(\lew\)的数设为\(0\)。那么期望就是所有\(w\),\(a_i\)为\(1\)的概率之和。对于一个\(i\),只有以下的操作能改变\(......
  • (UVA) The ? 1 ? 2 ? ... ? n = k problem
    The?1?2?...?n=kproblemTheproblemGiventhefollowingformula,onecansetoperators'+'or'-'insteadofeach'?',inordertoobtainagivenk?1?2?...?n=kForexample:toobtaink=12,theexp......
  • org.pentaho.di.core.exception.KettleXMLException: Unexpected problem reading sh
    【kettle】【报错】UnexpectedproblemreadingsharedobjectsfromXMLfile当读共享文件时发生错误UnexpectedproblemreadingsharedobjectsfromXMLfile:null当读共享文件时发生错误kettle新建转换时,“读取共享对象时发生一个严重错误”“Unexpectedproblemre......
  • A*B Problem 485 (数学题+九余数定理)
    A*BProblem1000ms |          内存限制:655352设计一个程序求出A*B,然后将其结果每一位相加得到C,如果C的位数大于等于2,继续将C的各位数相加,直到结果是个一位数k。例如:6*8=48;4+8=12;1+2=3;输出3即可。第一行输入一个数N(0<N<=1000000),表示N组测试数据。......
  • hdoj The sum problem 2058 (数学等差公式&技巧转换)
    ThesumproblemTimeLimit:5000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):21416    AcceptedSubmission(s):6287ProblemDescriptionGivenasequence1,2,3,......N,yourjob......