POJ 3067 Japan,poj3067japan

來源:互聯網
上載者:User

POJ 3067 Japan,poj3067japan

題意: 東海岸有x個城市,西海岸有y個。x與y 之間有很多高速公路。問k條路有多少個交叉點。

我就是求的逆序對,把east當作 pos 按照從大到小排序。然後插入。接下來的就跟求逆序對的一樣了。

線段樹或者數狀數組都能過。

注意最後要用long long。

(午夜一發,寫完吃個麵包睡覺。

#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<bitset>#include<vector>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,b) for(int i=a;i<b;i++)#define FOR_(i,a,b) for(int i=a;i>=b;i--)#define sf scanf#define pf printf#define all(v) (v).begin(),(v).end()#define acfun std::ios::sync_with_stdio(false)#define SIZE (1000  +2)#define MOD 1000000007using namespace std;int a[SIZE*4];int up;void update(int l,int r,int o){    if(l==r)        a[o]++;    else    {        int m=(l+r)>>1;        if(up<=m)update(l,m,o*2);        else update(m+1,r,o*2+1);        a[o]=a[o*2]+a[o*2+1];    }}int ql,qr;int query(int l,int r,int o){    if(l>=ql&&r<=qr)return a[o];    int m=(l+r)>>1;    int ans=0;    if(ql<=m)ans+=query(l,m,o*2);    if(qr>m)ans+=query(m+1,r,o*2+1);    return ans;}struct node{    int e,w;    bool friend operator <(node a,node b)    {        if(a.e==b.e)            return a.w>b.w;        return a.e>b.e;    }}way[SIZE*SIZE];int main(){    int t;    int east,west,n;    sf("%d",&t);    FOR(tt,1,t+1)    {        sf("%d%d%d",&east,&west,&n);        FOR(i,0,n)        sf("%d%d",&way[i].e,&way[i].w);        sort(way,way+n);        CLR(a,0);        LL ans=0;        FOR(i,0,n)        {            up=way[i].w;            update(1,west,1);            ql=1,qr=way[i].w-1;            if(ql<=qr)            ans+=query(1,west,1);        }        pf("Test case %d: %lld\n",tt,ans);    }}


聯繫我們

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