您的位置:首页 > 其它

UVA 12663 High bridge, low bridge

2016-03-25 20:44 239 查看
题意:刚开始给你n个桥和他们分别对应的高度,接下来有m次涨潮,每次涨潮水的高度会上升到xi,接下来又会下降到yi,

每次涨潮时水重新淹没这座桥算作一次淹没,如果桥一直比水的位置低,则只算做一次淹没。

(1<=n,m,k<=1e5)

思路:先对桥的高度从小到大进行排序,

每次我们在线段树上对上一次被水退潮的高度+1到这次水淹没的高度的区间+1,最后统计有多少座桥淹没的次数大于等于k便可以了。

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include<bitset>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
const int maxn=100101;
int a[maxn],sumv[4*maxn],addv[4*maxn],L,R,v;
int k;

void build(int l,int r,int rt){
addv[rt]=0;
sumv[rt]=0;
if(l==r)
return ;
int m=(l+r)>>1;
build(lson);
build(rson);
}

void pushdown(int rt,int l){
if(addv[rt]){
addv[rt<<1]+=addv[rt];
addv[rt<<1|1]+=addv[rt];
sumv[rt<<1]+=addv[rt];
sumv[rt<<1|1]+=addv[rt];
addv[rt]=0;
}
}

void update(int l,int r,int rt){
if(L<=l&&R>=r){
addv[rt]+=v;
sumv[rt]+=v;
return ;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(lson);
if(R>m)
update(rson);
}

int query(int l,int r,int rt){
int ret=0;
if(l==r)
return sumv[rt]>=k;
pushdown(rt,r-l+1);
int m=(l+r)>>1;
ret+=query(lson);
ret+=query(rson);
return ret;
}

int c[maxn],d[maxn];
int main(){
int n,m,case1=1;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
build(1,n,1);
d[0]=0;
for(int i=1;i<=m;i++){
scanf("%d%d",&c[i],&d[i]);
L=lower_bound(a+1,a+n+1,d[i-1]+1)-a;
R=upper_bound(a+1,a+n+1,c[i])-a;
if(a[R]>c[i])
R--;
v=1;
update(1,n,1);
}
printf("Case %d: %d\n",case1++,query(1,n,1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: