UVA12663 线段树+二分




分析:线段树模板+二分查找

读入每座桥的高度,然后排序。

m次洪水,每次二分查找在高度在区间【Bi-1,A】的桥的个数,然后区间更新。


代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 100000;
int n,m,k;
int a,b;
int h[maxn+10];
struct node{
    int lc,rc;
    int add;
    int sum;
}tree[maxn*4+10];
int ans;

void pushdown(int root, int x){
    if (tree[root].add){
        tree[root<<1].sum += tree[root].add*(x-(x>>1));
        tree[root<<1|1].sum += tree[root].add*(x>>1);
        tree[root<<1].add += tree[root].add;
        tree[root<<1|1].add += tree[root].add;
        tree[root].add = 0;
    }
}

void build(int root,int L, int R){
    tree[root].lc = L;
    tree[root].rc = R;
    tree[root].add = 0;
    tree[root].sum = 0;

    if (L==R) return;
    int mid = (L+R)>>1;
    build(root<<1,L,mid);
    build(root<<1|1,mid+1,R);
}

void update(int root , int L, int R, int x){
    if (tree[root].lc == L && tree[root].rc == R){
        tree[root].sum += x*(R-L+1);
        tree[root].add += x;
        return;
    }
    pushdown(root,tree[root].rc-tree[root].lc+1);
    int mid = (tree[root].lc+tree[root].rc)>>1;
    if (R<=mid) update(root<<1,L,R,x);
    else if (L>mid) update(root<<1|1,L,R,x);
    else {
        update(root<<1,L,mid,x);
        update(root<<1|1,mid+1,R,x);
    }
}

void query(int root, int L, int R){
     if (L==tree[root].lc && R==tree[root].rc) {
         ans += tree[root].sum;
         return;
     }
     pushdown(root,tree[root].rc-tree[root].lc+1);
     int mid = (tree[root].lc+tree[root].rc)>>1;
     if (R<=mid) query(root<<1,L,R);
     else if (L>mid) query(root<<1|1,L,R);
     else {
        query(root<<1,L,mid);
        query(root<<1|1,mid+1,R);
     }
}

void init(){
   for (int i=1; i<=n; i++) scanf("%d",&h[i]);
   sort(h+1,h+n+1);
   build(1,1,n);
}

int kase = 0;
int main(){
    int s1,s2;
    while (scanf("%d %d %d",&n,&m,&k)!=EOF){
        init();
        int high = 1;
        while (m--){
            scanf("%d %d",&a,&b);
            s1 = lower_bound(h+1,h+n+1,high)-h;
            s2 = lower_bound(h+1,h+n+1,a)-h;

            if (s2>n) s2 = n;
            while (h[s1]<=high) s1++;
            while (a<h[s2]) s2--;
            if (s1<=s2) update(1,s1,s2,1);
            high = b;
        }

        int total = 0;
        for (int i=1; i<=n; i++) {
            ans = 0;
            query(1,i,i);
            if (ans>=k) total++;
        }
        printf("Case %d: %d\n",++kase,total);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值