错误记录:
题目说输入在int范围内,但是运算过程中可能超int;后来开了很多longlong就过了
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<map> 6 using namespace std; 7 #define fi first 8 #define se second 9 #define mp make_pair 10 #define pb push_back 11 typedef long long ll; 12 typedef unsigned long long ull; 13 typedef pair<int,int> pi; 14 struct Q 15 { 16 ll x1,x2,y;int c,type; 17 }q[20100]; 18 int nq; 19 bool operator<(const Q &a,const Q &b) 20 { 21 return a.y<b.y||(a.y==b.y&&a.type<b.type); 22 } 23 int n,w,h; 24 ll ans; 25 namespace S 26 { 27 #define lc (num<<1) 28 #define rc (num<<1|1) 29 #define mid (l+((r-l)>>1)) 30 ll d[200100],addv[200100]; 31 void build(int l,int r,int num) 32 { 33 if(l==r) {d[num]=addv[num]=0;return;} 34 build(l,mid,lc);build(mid+1,r,rc); 35 d[num]=addv[num]=0; 36 } 37 int L,R;ll x; 38 void pd(int l,int r,int num) 39 { 40 d[lc]+=addv[num];d[rc]+=addv[num]; 41 addv[lc]+=addv[num];addv[rc]+=addv[num]; 42 addv[num]=0; 43 } 44 void _addx(int l,int r,int num) 45 { 46 if(L<=l&&r<=R) 47 { 48 d[num]+=x;addv[num]+=x; 49 return; 50 } 51 pd(l,r,num); 52 if(L<=mid) _addx(l,mid,lc); 53 if(mid<R) _addx(mid+1,r,rc); 54 d[num]=max(d[lc],d[rc]); 55 } 56 ll _que(int l,int r,int num) 57 { 58 if(L<=l&&r<=R) return d[num]; 59 pd(l,r,num); 60 ll ans=0; 61 if(L<=mid) ans=max(ans,_que(l,mid,lc)); 62 if(mid<R) ans=max(ans,_que(mid+1,r,rc)); 63 return ans; 64 } 65 #undef lc 66 #undef rc 67 #undef mid 68 } 69 map<ll,ll> ma; 70 ll t0[200100]; 71 int main() 72 { 73 int i;ll a,b,c; 74 while(scanf("%d%d%d",&n,&w,&h)==3) 75 { 76 nq=0; 77 for(i=1;i<=n;i++) 78 { 79 scanf("%lld%lld%lld",&a,&b,&c); 80 q[++nq].y=a-w+1;q[nq].x1=b-h+1;q[nq].x2=b;q[nq].c=c;q[nq].type=1; 81 q[++nq].y=a+1;q[nq].x1=b-h+1;q[nq].x2=b;q[nq].c=c;q[nq].type=0; 82 } 83 ans=0; 84 t0[0]=0; 85 for(i=1;i<=nq;i++) t0[++t0[0]]=q[i].x1,t0[++t0[0]]=q[i].x2; 86 sort(t0+1,t0+t0[0]+1);t0[0]=unique(t0+1,t0+t0[0]+1)-t0-1; 87 ma.clear(); 88 for(i=1;i<=t0[0];i++) ma[t0[i]]=i; 89 for(i=1;i<=nq;i++) q[i].x1=ma[q[i].x1],q[i].x2=ma[q[i].x2]; 90 sort(q+1,q+nq+1); 91 S::build(1,t0[0],1); 92 for(i=1;i<=nq;i++) 93 { 94 if(q[i].type==0) 95 { 96 S::L=q[i].x1;S::R=q[i].x2;S::x=-q[i].c; 97 S::_addx(1,t0[0],1); 98 } 99 else 100 { 101 S::L=q[i].x1;S::R=q[i].x2;S::x=q[i].c; 102 S::_addx(1,t0[0],1); 103 } 104 if(i==nq||q[i].y!=q[i+1].y) 105 { 106 S::L=1;S::R=t0[0]; 107 ans=max(ans,S::_que(1,t0[0],1)); 108 } 109 } 110 printf("%lld ",ans); 111 } 112 return 0; 113 }