T3 surprise
Title Description
For various reasons, Kazuto people are now trapped in the Under World (hereinafter referred to as UW), and UW is about to usher in the ultimate stress test - Devil invasion.
There is only one God and the Administrator was eliminated, by the power of the original Knights of the integration is not enough. So Alice UW mobilized all the people, together with the integration knight to fight the inferno.
UW resident can vaguely see the base camp Mozu army. Integration of knights going to launch a surprise attack before the inferno invasion, attacks on the headquarters of the inferno!
To reduce the risk, Alice found you, a good scout, I hope you can carry out an investigation into an inferno in front of the headquarters of surprise, and calculate the difficulty of attack.
After investigation, you draw a map of the inferno base camp, and then found that the inferno is a stronghold of N × N grid map, a total of N army stationed in some of the grid (no two troops together ).
In the base camp, each has a k × k (1≤k≤N) sub-grid map contains exactly k armies, our attacks will increase the difficulty of 1:00.
Now you draw a map based on, tell Alice attacked difficulty of how the action.
Entry
The first line, a positive integer N, the size and number of military trellis diagram.
Next N lines of two integers, Xi, Yi, i represents the coordinates of the Army.
Ensure that each row and each column has exactly one army, that is, each and every Xi Yi is not the same.
Export
Sample input
5
1 1
3 2
2 4
5 5
4 3
Sample Output
10
prompt
Sample [explain]
Obviously, respectively (2,2) and (4,4) is the upper left, a sub-mesh bottom right vertices have three armies,
which contributed 1 point for our difficulty.
Similar subgrid in FIG. 10 can find the picture.
[Data] range
for data of 30%, N ≤ 100
data for 60%, N ≤ 5000
data for 100%, N ≤ 50000
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define Re register 7 using namespace std; 8 int read() 9 { 10 int f=1,x=0;char ch=getchar(); 11 while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} 13 return f*x; 14 } 15 const int maxn=50005; 16 int n,a[maxn],ans; 17 int lmax[maxn],lmin[maxn],rmax[maxn],rmin[maxn],t[2*maxn]; 18 void work(int l,int r,int mid) 19 { 20 lmax[mid]=lmin[mid]=a[mid]; 21 rmax[mid+1]=rmin[mid+1]=a[mid+1]; 22 for(int i=mid-1;i>=l;i--) 23 { 24 lmax[i]=max(lmax[i+1],a[i]); 25 lmin[i]=min(lmin[i+1],a[i]); 26 } 27 for(int i=mid+2;i<=r;i++) 28 { 29 Rmax of [I] = max (Rmax of [I- . 1 ], A [I]); 30 Rmin [I] = min (Rmin [I- . 1 ], A [I]); 31 is } 32 // both left 33 is for ( int I = L; I <= MID; I ++ ) 34 is { 35 int J = I + Lmax [I] - Lmin [I]; 36 IF (J> MID && J <= R & lt) 37 [ IF (Lmax [I ]> Rmax of [J] && Lmin [I] <Rmin [J]) // right and left two sections must not be equal elements, so we do not consider equals 38 is ANS ++ ; 39 } 40 //min IS ON left, max IS ON right 41 is int L = MID + . 1 , R & lt = MID + . 1 ; 42 is the while (L <= R & lt && Lmax [L]> Rmax of [L]) 43 is { 44 is T [Rmax of [L] -L + n- ] -; // traversed path is not legal 45 L ++ ; 46 is } 47 the while (R & lt <Lmin = R & lt && [L] < Rmin [R & lt]) 48 { 49 T [Rmax of [R & lt] -R & lt n-+] + +; // path traversed are legitimate 50 R & lt ++ ; 51 is } 52 is for ( intI = l; I <= mid; I ++) // I from l to mid movement, Lmin becomes large, Lmax becomes smaller, so that LR can be moved to the left 53 is { 54 is the while (L> mid + . 1 && Lmax [I] <Rmax of [ L- . 1 ]) // when L == mid + 1, exit the loop 55 { 56 is L - ; 57 is T [Rmax of [L] + n--L] ++ ; 58 } 59 the while (R & lt> MID + . 1 && Lmin [I]> Rmin [the R- . 1 ]) 60 { 61 is R-- ; 62 is T [Rmax of [R & lt] -R & lt n-+] - ; 63 is } 64 if(t[lmin[i]-i+n]>0) 65 ans+=t[lmin[i]-i+n]; 66 } 67 for(int i=mid+1;i<=r;i++) 68 t[rmax[i]-i+n]=0; 69 } 70 void divide(int l,int r) 71 { 72 if(l==r) 73 { 74 ans++; 75 return; 76 } 77 intMID = (L + R & lt) >> . 1 ; 78 Divide (L, MID); 79 Divide (MID + . 1 , R & lt); 80 Work (L, R & lt, MID); 81 Reverse (A + L, A + R & lt + . 1 ) ; 82 IF (((rl is an)% 2 ) == 0 ) MID -; // . 1 2. 5. 4. 3 after switching 54321 123 should still be in a range so that the MID -; 83 Work (L, R & lt, MID); 84 Reverse (A + L, + A + R & lt . 1 ); 85 } 86 int main () 87 { 88 n-= Read (); 89 for ( int i=1;i<=n;i++) 90 { 91 int x,y; 92 x=read();y=read(); 93 a[x]=y; 94 } 95 divide(1,n); 96 printf("%d",ans); 97 }
Both implementations are correct, the same essence
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define Re register 7 using namespace std; 8 int read() 9 { 10 int f=1,x=0;char ch=getchar(); 11 while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} 13 return f*x; 14 } 15 const int maxn=50005; 16 int n,a[maxn],ans; 17 int lmax[maxn],lmin[maxn],rmax[maxn],rmin[maxn],t[2*maxn]; 18 void work(int l,int r,int mid) 19 { 20 lmax[mid]=lmin[mid]=a[mid]; 21 rmax[mid+1]=rmin[mid+1]=a[mid+1]; 22 for(int i=mid-1;i>=l;i--) 23 { 24 lmax[i]=max(lmax[i+1],a[i]); 25 lmin[i]=min(lmin[i+1],a[i]); 26 } 27 for(int i=mid+2;i<=r;i++) 28 { 29 Rmax of [I] = max (Rmax of [I- . 1 ], A [I]); 30 Rmin [I] = min (Rmin [I- . 1 ], A [I]); 31 is } 32 // both left 33 is for ( int I = L; I <= MID; I ++ ) 34 is { 35 int J = I + Lmax [I] - Lmin [I]; 36 IF (J> MID && J <= R & lt) 37 [ IF (Lmax [I ]> Rmax of [J] && Lmin [I] <Rmin [J]) // right and left two sections must not be equal elements, so we do not consider the equal sign, following the same token 38 is ANS ++ ; 39 } 40 //IS ON left min, max IS ON right 41 is int L = mid + . 1 , R & lt = mid + . 1 ; 42 is for ( int I = mid; I> = l; i--) // I l from the mid to move, lmin smaller , Lmax becomes large, so that to be able to move to the right LR 43 is { 44 is the while (L <Lmax = R & lt && [I]> Rmax of [L]) // belonging to an illegal portion 45 { 46 is T [Rmax of [L] + n--L ] - ; 47 L ++ ; 48 } 49 the while (R & lt <Lmin = R & lt && [I] <Rmin [R & lt]) // 50 { 51 is T [Rmax of [R & lt] -R & lt n-+] ++; //合法的 52 R++; 53 } 54 if(t[lmin[i]-i+n]>0) 55 ans+=t[lmin[i]-i+n]; 56 } 57 for(int i=mid+1;i<=r;i++) 58 t[rmax[i]-i+n]=0; 59 } 60 void divide(int l,int r) 61 { 62 if(l==r) 63 { 64 ans++; 65 return ; 66 } 67 int MID = (L + R & lt) >> . 1 ; 68 Divide (L, MID); 69 Divide (MID + . 1 , R & lt); 70 Work (L, R & lt, MID); 71 is Reverse (A + L, R & lt + a + . 1 ); 72 IF (((rl is an)% 2 ) == 0 ) MID -; // after switching 12345 54321 123 should still be in a range so that the mid- -; 73 is Work (L, R & lt, MID); 74 Reverse (A + L, + A + R & lt . 1 ); 75 } 76 int main () 77 { 78 n=read(); 79 for(int i=1;i<=n;i++) 80 { 81 int x,y; 82 x=read();y=read(); 83 a[x]=y; 84 } 85 divide(1,n); 86 printf("%d",ans); 87 }