遍历一遍即可;
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1005; int num[ maxn ], vis[ maxn ]; struct node{ int x, y; }edge[ maxn ]; int main(){ int Case, n, k, sx, sy, ex, ey; cin >> Case; while( Case-- ){ cin >> n >> k; int Max = -1; memset( num, 0, sizeof( num ) ); memset( vis, 1, sizeof( vis ) ); for( int i = 0; i < n; ++i ){ cin >> sx >> sy >> ex >> ey; if( sx > ex ){ swap( sx, ex ); } edge[ i ].x = sx; edge[ i ].y = ex; if( Max < ex ) Max = ex; for( int i = sx; i <= ex; ++i ){ num[ i ]++; } } int ans = 0; for( int i = 0; i <= Max; ++i ){ int temp = num[ i ] - k; if( temp > 0 ){ ans += temp; for( int j = 0; j < temp; ++j ){ int cur = 0; int cnt = -1; for( int k = 0; k < n; ++k ){ if( edge[ k ].x <= i && edge[ k ].y >= i && vis[ k ] ){ if( cnt < edge[ k ].y - i + 1 ){ cnt = edge[ k ].y - i + 1; cur = k; } } } vis[ cur ] = false; for( int k = i; k <= edge[ cur ].y; ++k ){ num[ k ]--; } } } } cout << ans<< endl; } return 0; }