http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3674
离散化+线段树,还有 longlong。
AC Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 #define maxn 200005 8 struct node{ 9 int cnt,id; 10 }setree[maxn<<3]; 11 struct op{ 12 int l,r,h,id; 13 }mes[maxn]; 14 struct{ 15 int l,r; 16 }opp[maxn]; 17 int sorted[maxn<<1]; 18 long long ans[maxn]; 19 bool cmp(struct op a,struct op b) 20 { 21 return a.h>b.h; 22 } 23 void build(int l,int r,int rt) 24 { 25 setree[rt].cnt=0; 26 setree[rt].id=0; 27 if(l==r) 28 return ; 29 int m=(l+r)>>1; 30 build(lson); 31 build(rson); 32 } 33 int binsearch(int l,int r,int num) 34 { 35 int m=(l+r)>>1; 36 if(sorted[m]==num) 37 return m; 38 if(sorted[m]>num) 39 return binsearch(l,m-1,num); 40 return binsearch(m+1,r,num); 41 } 42 void pushdown1(int rt) 43 { 44 if(setree[rt].id>0){ 45 if(setree[rt<<1].id==0) 46 setree[rt<<1].id=setree[rt].id; 47 if(setree[rt<<1|1].id==0) 48 setree[rt<<1|1].id=setree[rt].id; 49 setree[rt].id=0; 50 } 51 } 52 void update1(int l,int r,int rt,int L,int R,int id) 53 { 54 if(setree[rt].id>0) 55 return; 56 if(L<=l&&r<=R){ 57 setree[rt].id=id; 58 return; 59 } 60 pushdown1(rt); 61 int m=(l+r)>>1; 62 if(L<=m) 63 update1(lson,L,R,id); 64 if(R>m) 65 update1(rson,L,R,id); 66 } 67 void update(int l,int r,int rt,int L,int R) 68 { 69 if(L<=l&&r<=R){ 70 setree[rt].cnt+=1; 71 return; 72 } 73 int m=(l+r)>>1; 74 if(L<=m) 75 update(lson,L,R); 76 if(R>m) 77 update(rson,L,R); 78 } 79 void pushdown(int rt) 80 { 81 if(setree[rt].cnt>0){ 82 setree[rt<<1].cnt+=setree[rt].cnt; 83 setree[rt<<1|1].cnt+=setree[rt].cnt; 84 setree[rt].cnt=0; 85 } 86 if(setree[rt].id>0){ 87 if(setree[rt<<1].id==0) 88 setree[rt<<1].id=setree[rt].id; 89 if(setree[rt<<1|1].id==0) 90 setree[rt<<1|1].id=setree[rt].id; 91 } 92 } 93 void query(int l,int r,int rt) 94 { 95 if(l==r){ 96 ans[setree[rt].id]+=(long long)setree[rt].cnt*(sorted[r]-sorted[l-1]); 97 return; 98 } 99 pushdown(rt); 100 int m=(l+r)>>1; 101 query(lson); 102 query(rson); 103 } 104 int main() 105 { 106 int n,m; 107 while(~scanf("%d%d",&n,&m)){ 108 int kk=0; 109 memset(ans,0,sizeof(ans)); 110 for(int i=0;i<n;i++){ 111 scanf("%d%d",&opp[i].l,&opp[i].r); 112 sorted[kk++]=opp[i].l; 113 sorted[kk++]=opp[i].r; 114 } 115 for(int i=0;i<m;i++){ 116 scanf("%d%d%d",&mes[i].l,&mes[i].r,&mes[i].h); 117 mes[i].id=i+1; 118 sorted[kk++]=mes[i].l; 119 sorted[kk++]=mes[i].r; 120 } 121 122 sort(mes,mes+m,cmp); 123 sort(sorted,sorted+kk); 124 int k=1; 125 for(int i=1;i<kk;i++) 126 if(sorted[i]!=sorted[i-1]) 127 sorted[k++]=sorted[i]; 128 build(1,k-1,1); 129 for(int i=0;i<m;i++){ 130 int l=binsearch(0,k-1,mes[i].l); 131 int r=binsearch(0,k-1,mes[i].r); 132 update1(1,k-1,1,l+1,r,mes[i].id); 133 } 134 for(int i=0;i<n;i++){ 135 int l=binsearch(0,k-1,opp[i].l); 136 int r=binsearch(0,k-1,opp[i].r); 137 update(1,k-1,1,l+1,r); 138 } 139 query(1,k-1,1); 140 for(int i=1;i<=m;i++) 141 printf("%lld\n",ans[i]); 142 printf("\n"); 143 } 144 return 0; 145 }