zoukankan      html  css  js  c++  java
  • POJ(2481)Cows 树状数组

    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <fstream>
    #include <vector>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    #include <stdlib.h>
    using namespace std ;
    const int maxn = 100005;
    int n;
    struct cow{
        int id;
        int l,r;
        friend bool operator<(const cow &a,const cow &b){
            if(a.r == b.r)
                return a.l < b.l;
            return a.r > b.r;
        }
    }cow[maxn];
    int c[maxn];
    int lowbit(int x){
        return x&(-x);
    }
    void update(int id,int val,int up){
        for(int i = id;i <= up;i+=lowbit(i))
            c[i] += val;
    }
    int query(int id){
        int ans = 0;
        for(int i = id;i > 0;i-=lowbit(i))
            ans += c[i];
        return ans;
    }
    int out[maxn];
    int main(){
        while(scanf("%d",&n)&&n){
            int up = -1;
            for(int i = 1;i <= n;i++){
                scanf("%d%d",&cow[i].l,&cow[i].r);
                cow[i].l++;
                cow[i].r++;
                cow[i].id = i;
                if(up < cow[i].l)
                    up = cow[i].l;
            }
            sort(cow+1,cow+n+1);
            //for(int i = 1;i <= n;i++)
              //  printf("%d %d\n",cow[i].l,cow[i].r);
            memset(c,0,sizeof(c)) ;
            out[cow[1].id]=0;
            update(cow[1].l,1,up);
            for(int i = 2;i <= n;i++){
                if(cow[i].l == cow[i-1].l && cow[i].r == cow[i-1].r)
                    out[cow[i].id] = out[cow[i-1].id];
                else
                    out[cow[i].id] = query(cow[i].l);
                update(cow[i].l,1,up);
            }
            printf("%d",out[1]) ;
            for(int i = 2;i <= n;i++)
                printf(" %d",out[i]);
            printf("\n");
        }
        return 0 ;
    }
  • 相关阅读:
    POJ-1330 Nearest Common Ancestors(倍增的LCA)
    POJ-1442 Black Box(手写堆优化)
    POJ-2442 Sequence(手写堆优化)
    BZOJ2506 calc
    BZOJ3396 [Usaco2009 Jan]Total flow 水流
    BZOJ3570 DZY Loves Physics I
    BZOJ1101 [POI2007]Zap
    BZOJ1110 [POI2007]砝码Odw
    BZOJ1555 KD之死
    BZOJ3476 [Usaco2014 Mar]The Lazy Cow
  • 原文地址:https://www.cnblogs.com/Roly/p/3065692.html
Copyright © 2011-2022 走看看