Codeforces #261 D

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.

简单的树状数组求逆序数。。。开始怎么也没想到。。。

答案没用long long wa一次

 #include <cstring>

 #include <iostream>

 #include <algorithm>

 #include <cstdio>

 #include <cmath>

 #include <map>

 #include <cstdlib>

 #define M(a,b) memset(a,b,sizeof(a))

 using namespace std;

 int n;

 int num[];

 int savefro[],saveb[];

 int ans[];

 int flag[];

 map<int,int> front,back;

 int bit[],cnn;

 int sum(int i)

 {

     int res = ;

     while(i>)

     {

         res+=bit[i];

         i-=i&-i;

     }

     return res;

 }

 void add(int i,int x)

 {

     while(i<=cnn)

     {

         bit[i]+=x;

         i+=i&-i;

     }

 }

 int main()

 {

     while(scanf("%d",&n)==)

     {

         front.clear();

         back.clear();

         M(saveb,);

         M(savefro,);

         M(ans,);

         M(flag,);

         M(bit,);

         long long res = ;

         for(int i = ;i<n;i++)

         {

             scanf("%d",&num[i]);

         }

         for(int i = ;i<n;i++)

         {

             savefro[i] = ++front[num[i]];

         }

         cnn = n;

         for(int i = n-;i>=;i--)

         {

             saveb[i] = ++back[num[i]];

             add(saveb[i],);

             res+=sum(savefro[i-]-);

            // cout<<' '<<sum(savefro[i-1]-1)<<' '<<savefro[i-1]<<endl;

         }

         /*cout<<"savefro"<<endl;

         for(int i = 0;i<n;i++)

         {

             cout<<savefro[i]<<' ';

         }

         cout<<endl;

         cout<<"saveb"<<endl;

         for(int i = 0;i<n;i++)

         {

             cout<<saveb[i]<<' ';

         }

         cout<<endl;

         cout<<"flag"<<endl;

         for(int i = 0;i<n;i++)

         {

             cout<<flag[i]<<' ';

         }

         cout<<endl;*/

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

     }

     return ;

 }

Codeforces #261 D的更多相关文章

  1. codeforces #261 C题 Pashmak and Buses(瞎搞)

    题目地址:http://codeforces.com/contest/459/problem/C C. Pashmak and Buses time limit per test 1 second m ...

  2. New Training Table

          2014_8_15 CodeForces 261 DIV2 A. Pashmak and Garden 简单题   B. Pashmak and Flowers    简单题   C. P ...

  3. Codeforces Round #261 (Div. 2)[ABCDE]

    Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...

  4. Codeforces Round #261 (Div. 2) B

    链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second m ...

  5. Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP

    http://codeforces.com/contest/459/problem/E 不明确的是我的代码为啥AC不了,我的是记录we[i]以i为结尾的点的最大权值得边,然后wa在第35  36组数据 ...

  6. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  7. Codeforces Round #261 (Div. 2) - E (459E)

    题目连接:http://codeforces.com/contest/459/problem/E 题目大意:给定一张有向图,无自环无重边,每条边有一个边权,求最长严格上升路径长度.(1≤n,m≤3 * ...

  8. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  9. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

随机推荐

  1. spring3.0+mybatis+spring快速入门

    一.首先奉上项目目录结构: 说明: dao,mapping,model包下的所有内容可以使用Generator工具自助生成. 具体用法,可以网上学习一下,比较简单,主要做以下工作: 1.提供相关的数据 ...

  2. Java并发编程:深入剖析ThreadLocal

    原文出处: 海 子 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本文先谈一下对ThreadLocal的理解,然后根据Threa ...

  3. Java实现数组排序

    package com.souvc.hibernate.exp; public class MySort { /** * 方法名:main</br> * 详述:Java实现数组排序 < ...

  4. java并发编程学习: 阻塞队列 使用 及 实现原理

    队列(Queue)与栈(Stack)是数据结构中的二种常用结构,队列的特点是先进先出(First In First Out),而Stack是先进后出(First In Last Out),说得通俗点: ...

  5. 学习web前端学习路程

    学习路程: 1.HTML和CSS基础 2.JavaScript语言 3.jQuery 4.综合网站实践 5.优化及调试

  6. MyBatis参数传入集合之foreach用法

    传入集合list // 账户类型包括门店和分公司 List<Object> scopeList = new ArrayList<Object>(); scopeList.add ...

  7. 递推 hdu 2048

    t 关于错排 共n个数 第n个 和一个交换  其他n-2错排  (n-1)*z[n-2]; n-1个错排 第n个和n-1个交换    (n-1)*z[n-1]; 求和 然后概率的话要除n! 一个一个除 ...

  8. cmd命令快速修改dns

    新建cmd文件,修改红色ip部分,以 ANSI 编码保存,双击运行即可快速修改dns配置 netsh interface ip set dns "本地连接" source=stat ...

  9. OpenLayers的定制

    最近因为工作的需要,把主流的的一些GIS的javascript库看了一遍,主要是ArcGIS Server API for Javascript,Openlayers和Leaflet. 先说说ArcG ...

  10. 在ubuntu/deepin/mint等系统中使用命令删除文件或文件夹

    此命令操作需谨慎: sudo rm -rf 文件夹路径(或文件路径)