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. J2EE基础之JSP

    J2EE基础之JSP 1.JSP简介 JSP是JavaServer的缩写,是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动态网页技术标准.在HTML文件中加入Java程序代码 ...

  2. [WPF系列]-Layout

    DockPanel The nice thing about dock panels is they already fill all the available space. LastChildFi ...

  3. Zabbix监控Tengine

    title: Zabbix监控Tengine tags: zabbix,nginx,tengine author: Chinge Yang date: 2016-12-29 --- Zabbix监控T ...

  4. 洛谷P3390 【模板】矩阵快速幂

    给定n*n的矩阵A,求A^k 行列都是n #include <iostream> #include <cstdio> #include <cstring> #inc ...

  5. kettle启动“Error: could not create the Java Virtual Machine”

    因为我的操作系统是32bit,而Ketttle的Spoon脚本中,默认是PENTAHO_DI_JAVA_OPTIONS="-Xms1024m" "-Xmx2048m&qu ...

  6. 生产环境使用 pt-table-checksum 检查MySQL数据一致性

    公司数据中心从托管机房迁移到阿里云,需要对mysql迁移(Replication)后的数据一致性进行校验,但又不能对生产环境使用造成影响,pt-table-checksum 成为了绝佳也是唯一的检查工 ...

  7. MVC 问答

    1.View含有什么,默认就念有Models吗? 不是,ViewBag是一个空对象.ViewBag 与 Models 不是必须一起使用的 . 2.Models 可用可不用?存在意义?

  8. win7 装了VB虚拟机 开始挺好用 后来突然就打不开了 提示如下错误:(如图)创建 COM 对象失败.

    创建 COM 对象失败. 应用程序将被中断. Start tag expected, '<' not found. Location: 'C:\Users\Mike/.VirtualBox\Vi ...

  9. 你可能不知道的 NaN 以及 underscore 1.8.3 _.isNaN 的一个 BUG

    这篇文章并不在我的 underscore 源码解读计划中,直到 @pod4g 同学回复了我的 issue(详见 https://github.com/hanzichi/underscore-analy ...

  10. Android开发自学笔记(Android Studio)—4.界面编程与View组件简单介绍

    一.引言 Android应用开发最重要的一份内容就是界面的开发,无论你程序包含的内容多么优秀,如若没有一个良好的用户交互界面,最终也只是会被用户所遗弃.Android SDK提供了大量功能丰富的UI组 ...