CodeForces 61E Enemy is weak 求i<j<k && a[i]>a[j]>a[k] 的對數 樹狀數組

來源:互聯網
上載者:User

標籤:blog   http   os   io   for   2014   ar   line   

題目連結:點擊開啟連結

題意是求 i<j<k && a[i]>a[j]>a[k] 的對數

如果只有2元組那就是求逆序數的做法

三元組的話就用一個樹狀數組x表示 數字i前面有多少個比自己大的個數

然後每次給這個y數組求和,再把x中>a[i]的個數存入y中即可


#include <algorithm>#include <cctype>#include <cassert>#include <cstdio>#include <cstring>#include <climits>#include <vector>#include<iostream>using namespace std;#define ll long longinline void rd(int &ret){char c;do { c = getchar();} while(c < '0' || c > '9');ret = c - '0';while((c=getchar()) >= '0' && c <= '9')ret = ret * 10 + ( c - '0' );}#define N 1000005#define eps 1e-8#define inf 1000000ll n;struct node{ll c[N];inline ll lowbit(ll x){return x&-x;}void init(){memset(c, 0, sizeof c);}ll sum(ll x){ll ans = 0;while(x<=n+10)ans += c[x], x+=lowbit(x);return ans;}void change(ll x, ll y){while(x)c[x] +=y, x-=lowbit(x);}}x, y;int haifei[1000000], panting[1000000];int main(){ll i, j;while(cin>>n){ll ans = 0;for(i = 0; i < n; i++)rd(haifei[i]), panting[i] = haifei[i];x.init(); y.init();sort(haifei, haifei+n);for(i = 0; i < n; i++){ll b = (lower_bound(haifei, haifei+n, panting[i]) - haifei) +1;ll siz = y.sum(b);ans += siz;y.change(b, x.sum(b));x.change(b, 1);}cout<<ans<<endl;}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.