湫湫系列故事——消灭兔子
线段树维护区间最小值, 简单记录下, 下标写错啦, WA到死。
http://acm.hdu.edu.cn/showproblem.php?pid=4544
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> int f_min(int x,int y) {return x>y?y:x;} int f_max(int x,int y) {return x>y?x:y;} int f_abs(int x) {return x>0?x:(-x);} using namespace std; const int MM = 100011; #define maxint 10000000 #define debug puts("wrong"); typedef __int64 int64; //const __int64 maxint = 1000000000000; #define lson rt<<1 #define rson rt<<1|1 int64 N,M; struct Info { int64 val; int64 need; int64 id; }p[MM]; Info sum[MM<<2]; bool cmp(Info x,Info y) { if(x.val!=y.val) return x.val<y.val; else return x.need<y.need; } int64 B[MM]; void get_data() { int64 i,j,k; for(i=1;i<=N;i++) scanf("%I64d",&B[i]); for(i=1;i<=M;i++) scanf("%I64d",&p[i].val); for(i=1;i<=M;i++) { scanf("%I64d",&p[i].need); } sort(B+1,B+N+1); sort(p+1,p+M+1,cmp); // for(i=1;i<=M;i++) printf("%I64d ",p[i].need); printf("\n"); } int64 find(int64 x) { int64 l=1,r=M; while(l<=r) { int64 mid=(l+r)>>1; if(p[mid].val>=x) r=mid-1; else l=mid+1; } return l; } void push_up(int64 rt) { if(sum[lson].need>sum[rson].need) sum[rt]=sum[rson]; else sum[rt]=sum[lson]; } void build(int64 l,int64 r,int64 rt) { if(l==r) { sum[rt]=p[l]; sum[rt].id=l; return; } int64 mid=(l+r)>>1; build(l,mid,lson); build(mid+1,r,rson); push_up(rt); } void tran(int64 l,int64 r,int64 rt) { printf("%I64d %I64d %I64d\n",l,rt,sum[rt].need); if(l==r) {return;} int64 mid=(l+r)>>1; tran(l,mid,lson); tran(mid+1,r,rson); } Info query(int64 L,int64 R,int64 l,int64 r,int64 rt) { if(L<=l && r<=R) { return sum[rt]; } int64 mid=(l+r)>>1; Info ret,tmp; ret.need=maxint; //ret.val=maxint; ret.id=-1; if(L<=mid) { tmp=query(L,R,l,mid,lson); if(ret.need>tmp.need) ret=tmp; } if(R>mid) { tmp=query(L,R,mid+1,r,rson); if(ret.need>tmp.need) ret=tmp; } return ret; } void Update(int64 p,int64 l,int64 r,int64 rt) { if(l==r) { sum[rt].need=maxint; return; } int64 mid=(l+r)>>1; if(p<=mid) Update(p,l,mid,lson); else Update(p,mid+1,r,rson); push_up(rt); } void solve() { int64 i,j,k,tmp,id; Info mx; int64 ans=0; bool flag=true; build(1,M,1); // tran(1,M,1); tmp=M; for(i=N;i>=1;i--) { // tmp=find(B[i]); while(tmp>=1 && p[tmp].val>=B[i]) tmp--; // printf("%d***\n", tmp); mx=query(tmp+1,M,1,M,1); // printf("%I64d %I64d %I64d %I64d\n",B[i], tmp,mx.need,mx.val); // printf("%d\n",sum[1].need); // if(B[i]==4) tran(1,M,1); if(mx.need==maxint) {flag=false;break;} else ans+=mx.need; Update(mx.id,1,M,1); } if(!flag) puts("No"); else printf("%I64d\n", ans); } int main() { while(scanf("%I64d%I64d",&N,&M)!=EOF) get_data(),solve(); return 0; } /* 5 5 1 23 54 4 3 12 32 3 54 34 12 32 43 54 65 206 */