Codeforcecs 459D Pashmak and Parmida's problem 樹狀數組 OR 分治

來源:互聯網
上載者:User

標籤:main   class   c++   pre   namespace   --   定義   div   test   

http://codeforces.com/contest/459/problem/D

題意:定義:f(l,r,x) [l,r]內x的個數 ,n個數,a[i] n<=1e5,a[i]<=1e9

問i<j時滿足f(1,i,a[i]) > f(j,n,a[j]) 的(i,j)對數?

預先處理出尾碼f(j) 插入到BIT中 枚舉i,查詢mp[a[i]]-1,後更新BIT即可.

#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int,int> ii;const ll mod=1e9+7;const ll inf=1e18;const int N=5e6+20;const int M=2e6;//f(1,i,a[i]) > f(j,n,a[j]) ll c[N],n,a[N]; map<ll,ll> mp,mk;int lowbit(int x){    return x&(-x);}void add(int p,int x){    for(int i=p;i<=M;i+=lowbit(i))        c[i]+=x;    }ll sum(int p){    ll res=0;    for(int i=p;i>=1;i-=lowbit(i))        res+=c[i];    return res;}int main(){     //ios::sync_with_stdio(false);    while(cin>>n)    {        mp.clear(),mk.clear();        memset(c,0,sizeof(c));        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        for(int i=n;i>=1;i--)        {            mp[a[i]]++;                add(mp[a[i]],1);        }            ll ans=0;        for(int i=1;i<n;i++)        {            mk[a[i]]++;            add(mp[a[i]],-1);            mp[a[i]]--;            ans+=sum(mk[a[i]]-1);        }                    cout<<ans<<endl;    }    return 0;}

標程的分治解法

#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int INF = 1000000000;const int MAX = 1000005;int a[MAX], tmp[MAX], cnt[MAX], le[MAX], ri[MAX];long long solve(int l, int r){    if (r - l < 2)        return 0;    int mid = (l + r) / 2;    long long ret = solve(l, mid) + solve(mid, r);    int p1 = l, p2 = mid;    while (p1 != mid || p2 != r)    {        int val1 = (p1 < mid ? le[p1] : INF);        int val2 = (p2 < r ? ri[p2] : INF);        if (val1 <= val2)        {            p1++;            ret += p2 - mid;        }        else            p2++;    }    merge(le + l, le + mid, le + mid, le + r, tmp);    for (int i = 0; i < r - l; i++)        le[i + l] = tmp[i];    merge(ri + l, ri + mid, ri + mid, ri + r, tmp);    for (int i = 0; i < r - l; i++)        ri[i + l] = tmp[i];    return ret;}int main(){    ios::sync_with_stdio(false);    int n;    cin >> n;    for (int i = 0; i < n; i++)    {        cin >> a[i];        tmp[i] = a[i];    }    sort(tmp, tmp + n);    for (int i = 0; i < n; i++)        a[i] = lower_bound(tmp, tmp + n, a[i]) - tmp;    for (int i = 0; i < n; i++)    {        cnt[a[i]]++;        le[i] = cnt[a[i]];    }    memset(cnt, 0, sizeof(cnt));    for (int i = n - 1; i >= 0; i--)    {        cnt[a[i]]++;        ri[i] = cnt[a[i]];    }    cout << solve(0, n) << endl;    return 0;}

 

Codeforcecs 459D Pashmak and Parmida's problem 樹狀數組 OR 分治

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.