Codeforces round #261 (Div. 2) D tree Array application

Source: Internet
Author: User

[1, I] The number of medium to a [I] must be greater than [, JN] The number of medium to a [J] And I <J, number of such (I, j), I <J, but the valid number before I must be greater than the number after J, which looks like the number in reverse order, so it's easy to go To the tree array, but the processing is personal, as I encountered a lot of trouble in the game, although it took a lot of time, after jiege's education, it was actually possible to plug it into a tree array and look for it. It was very simple and clear.


int n;int aa[1000000 + 55];int bb[1000000 + 55];int c[1000000 + 55];map<int ,int >mp;ll lowbit(ll x) {return x&(-x);}void add(int i,int val) {while(i <= n) {c[i] += val;i += lowbit(i);}}ll get_sum(int i) {ll sum = 0;while(i) {sum += c[i];i -= lowbit(i);}return sum;}void init() {memset(c,0,sizeof(c));memset(aa,0,sizeof(aa));memset(bb,0,sizeof(bb));mp.clear();}int main() {while(scanf("%d",&n) == 1) {init();for(int i=1;i<=n;i++)scanf("%d",&aa[i]);for(int i=1;i<=n;i++) {mp[aa[i]]++;bb[i] = mp[aa[i]];add(bb[i],1);}mp.clear();ll ans = 0ll;for(int i=n;i>=1;i--) {add(bb[i],-1);mp[aa[i]]++;int tmp = mp[aa[i]];ans += i - get_sum(tmp) - 1;}cout<<ans<<endl;}return 0;}


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.