Codeforces 261 d

Source: Internet
Author: User

Question link:

Solution report: a sequence of A1, A2, a3 ......... an, f (I, j, x) AK is equal to the number of X (I <= k <= J), so that I <J, determine how many pairs of I and j make f (1, I, AI)> F (J, N, AJ ).

Scan this sequence from left to right. num1 [I] is equal to the number of [I] equals to, similarly, you can obtain num2 [I] by scanning from right to left, maintain the number of num2 [I] scanned from right to left in a tree array, and add all the values to the tree array first, then, scan num1 [I] from the left to the right to determine the number of num1 [I] In num2 each time, at the same time, after judgment, we need to subtract 1 from the number of num2 [I], because the position of num1 [I] has exceeded the position of num2, therefore, num2 [I] cannot be used to calculate the number in the future. It should be deleted. The purpose of using a tree array is to quickly determine how many num2 [I] are smaller than num1 [I], so as to achieve).

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<map> 5 #include<algorithm> 6 using namespace std; 7 #define LL long long 8 #define maxn 1000005 9 LL n,tot;10 map<LL,LL> mp1,mp2;11 LL a[maxn],num1[maxn],num2[maxn],num3[maxn];12 LL tree[maxn];13 LL find(LL d,int l,int r)14 {15     while(l < r)16     {17         int mid = (l + r) / 2;18         if(d <= num1[mid]) r = mid;19         else l = mid + 1;20     }21     if(num1[l] != d) return l - 1;22     else return l;23 }24 void insert(int l,int d)25 {26     for(int i = l;i <= n;i += (-i & i))27     tree[i] += d;28 }29 LL sum(int l)30 {31     LL tot = 0;32     for(int i = l;i > 0;i -= (-i & i))33     tot += tree[i];34     return tot;35 }36 37 int main()38 {39     40     while(scanf("%lld",&n)!=EOF)41     {42         for(int i = 1;i <= n;++i)43         scanf("%lld",&a[i]);44         memset(tree,0,sizeof(tree));45         memset(num1,0,sizeof(num1));46         memset(num2,0,sizeof(num2));47         mp1.clear();48         mp2.clear();49         for(int i  = 1;i <= n;++i)50         {51             if(mp1.insert(pair<LL,LL> (a[i],1)).second == 1)52             num1[i] = 1;53             else54             {55                 mp1[a[i]] = mp1[a[i]] + 1;56                 num1[i] = mp1[a[i]];57             }58         }59         for(int i = n;i >= 1;--i)60         {61             if(mp2.insert(pair<LL,LL> (a[i],1)).second == 1)62             num2[i] = 1;63             else64             {65                 mp2[a[i]] = mp2[a[i]] + 1;66                 num2[i] = mp2[a[i]];67             }68         }69         for(int i = 1;i <= n;++i)70         insert(num2[i],1);71         tot = 0;72         for(int i = 1;i <= n;++i)73         {74             insert(num2[i],-1);75             tot += sum(num1[i]-1);76         }77         printf("%lld\n",tot);78     }79     return 0;80 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.