BestCoder Round #2

时间:2023-03-09 19:40:56
BestCoder Round #2

TIANKENG’s restaurant http://acm.hdu.edu.cn/showproblem.php?pid=4883

竟然暴力1.44*10^7  还要*T=100  竟然过了

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=;
int sum[M];
int main(){
int t,n;
while(~scanf("%d",&t)){
while(t--){
scanf("%d",&n);
mt(sum,);
while(n--){
int add,sx,sy,ex,ey;
scanf("%d %d:%d %d:%d",&add,&sx,&sy,&ex,&ey);
sx=sx*+sy+;
ex=ex*+ey;
for(int i=sx;i<=ex;i++) sum[i]+=add;
}
int big=;
for(int i=;i<M;i++){
big=max(big,sum[i]);
}
printf("%d\n",big);
}
}
return ;
}

线段树RE,因为题目中会出现sx>ex。判了就过。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define mt(a,b) memset(a,b,sizeof(a))
#define lrrt int L,int R,int rt
#define iall 1,n,1
#define imid int mid=(L+R)>>1
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
using namespace std;
const int M=;
int tree[M<<];
void pushdown(int rt){
if(tree[rt]){
tree[rt<<]+=tree[rt];
tree[rt<<|]+=tree[rt];
tree[rt]=;
}
}
void update(int x,int y,int val,lrrt){
if(x<=L&&R<=y){
tree[rt]+=val;
return ;
}
pushdown(rt);
imid;
if(mid>=x) update(x,y,val,lson);
if(mid<y) update(x,y,val,rson);
}
int big;
void query(lrrt){
if(L==R){
big=max(big,tree[rt]);
return ;
}
imid;
pushdown(rt);
query(lson);
query(rson);
}
int main(){
int t,n=,m;
while(~scanf("%d",&t)){
while(t--){
scanf("%d",&m);
mt(tree,);
while(m--){
int add,sx,sy,ex,ey;
scanf("%d %d:%d %d:%d",&add,&sx,&sy,&ex,&ey);
sx=sx*+sy;
ex=ex*+ey-;
if(sx>ex) continue;
update(sx,ex,add,iall);
}
big=;
query(iall);
printf("%d\n",big);
}
}
return ;
}

正解是o(n)算法,标记,左++,右--。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int M=;
int sum[M];
int main(){
int t,n=,m;
while(~scanf("%d",&t)){
while(t--){
scanf("%d",&m);
mt(sum,);
while(m--){
int add,sx,sy,ex,ey;
scanf("%d %d:%d %d:%d",&add,&sx,&sy,&ex,&ey);
sx=sx*+sy;
ex=ex*+ey;
if(sx>ex) continue;
sum[sx]+=add;
sum[ex]-=add;
}
int big=;
int now=;
for(int i=;i<M;i++){
now+=sum[i];
big=max(big,now);
}
printf("%d\n",big);
}
}
return ;
}

end