题解?noipT1还需要题解?正解就是$n^2$大暴力。
考试的时候打了$n^2$的暴力,也想到了正解的优化,然而觉得它太麻烦了,而且$n^2$怎么优化也过不了50000啊,而且即使不优化前面30分我也能拿到。
然而就把正解放弃了……完戏。
然而这题ifelse打的我好恶心啊……
ps.linux终端还是挺良心的,y1给我报错了,不然凉凉……
题解:
一个方块内部的贡献为:abs(x1(i)-x2(i))*abs(y1(i)-y2(i))*2;
然后$n^2$考虑方块间的贡献。
直接枚举肯定会T,考虑将输入排序,当不符合条件是break,居然快了这么多。
有一个坑点:
开始我写的是:else if(x1(j)>x2(i)&&y1(j)>y2(i))break;
但其实:else if(x1(j)>x2(i))break;就可以了。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define MAXN 100010 8 #define LL long long 9 #define int LL 10 #define max(a,b) ((a)>(b)?(a):(b)) 11 #define ma(x,y) memset(x,y,sizeof(x)) 12 using namespace std; 13 int n,maxn,maxy; 14 int map[1010][1010]; 15 struct ques 16 { 17 int x1,x2,ty1,ty2; 18 #define x1(i) que[i].x1 19 #define x2(i) que[i].x2 20 #define ty1(i) que[i].ty1 21 #define ty2(i) que[i].ty2 22 friend bool operator < (ques a,ques b) 23 { 24 return a.x1==b.x1?a.ty1<b.ty1:a.x1<b.x1; 25 } 26 }que[MAXN]; 27 inline int read(); 28 void QJ2(); 29 signed main() 30 { 31 n=read(); 32 for(int i=1;i<=n;i++) 33 x1(i)=read(),ty1(i)=read(),x2(i)=read(),ty2(i)=read(); 34 QJ2(); 35 } 36 inline int read() 37 { 38 int s=0;char a=getchar(); 39 while(a<'0'||a>'9')a=getchar(); 40 while(a>='0'&&a<='9'){s=s*10+a-'0';a=getchar();} 41 return s; 42 } 43 void QJ2() 44 { 45 sort(que+1,que+n+1); 46 LL ans=0; 47 for(int i=1;i<=n;i++) 48 ans+=1ll*abs(x1(i)-x2(i))*abs(ty1(i)-ty2(i))*2; 49 for(int i=1;i<=n;i++) 50 for(int j=i+1;j<=n;j++) 51 { 52 if(i!=j) 53 { 54 if(ty1(j)==ty2(i)+1)//j上 55 { 56 int ttt=min(x2(j),x2(i))-max(x1(j),x1(i))+1; 57 if(ttt>0) 58 { 59 ans+=(ttt-1)*2; 60 if(abs(x1(i)-x1(j))>0)ans++; 61 if(abs(x2(i)-x2(j))>0)ans++; 62 } 63 else if(abs(x1(i)-x1(j))==1||abs(x2(i)-x2(j))==1)ans++; 64 } 65 else if(ty2(j)==ty1(i)-1)//j下 66 { 67 int ttt=min(x2(j),x2(i))-max(x1(j),x1(i))+1; 68 if(ttt>0) 69 { 70 ans+=(ttt-1)*2; 71 if(abs(x1(i)-x1(j))>0)ans++; 72 if(abs(x2(i)-x2(j))>0)ans++; 73 } 74 else if(abs(x1(i)-x1(j))==1||abs(x2(i)-x2(j))==1)ans++; 75 } 76 else if(x2(j)==x1(i)-1)//j左 77 { 78 int ttt=min(ty2(j),ty2(i))-max(ty1(j),ty1(i))+1; 79 if(ttt>0) 80 { 81 ans+=(ttt-1)*2; 82 if(abs(ty1(i)-ty1(j))>0)ans++; 83 if(abs(ty2(i)-ty2(j))>0)ans++; 84 } 85 } 86 else if(x1(j)==x2(i)+1)//j右 87 { 88 int ttt=min(ty2(j),ty2(i))-max(ty1(j),ty1(i))+1; 89 if(ttt>0) 90 { 91 ans+=(ttt-1)*2; 92 if(abs(ty1(i)-ty1(j))>0)ans++; 93 if(abs(ty2(i)-ty2(j))>0)ans++; 94 } 95 } 96 else if(x1(j)>x2(i)&&ty1(j)>ty2(i))break; 97 } 98 } 99 printf("%lld ",ans); 100 exit(0); 101 }