把桥按照左边点坐标排序,左边相同按照右边。
然后依次插入树状数组,getsum就是在这个桥之前的桥,也就是这个桥产生的交点。
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <vector> 5 6 using namespace std; 7 8 const int maxn = 2e3+100; 9 int N,M,K,T; 10 11 struct line{ 12 int l,r; 13 line(int _l=0,int _r=0) :l(_l),r(_r){} 14 15 bool operator < (const line &b) const 16 { 17 if(l == b.l) return r < b.r; 18 else return l < b.l; 19 } 20 }; 21 22 vector <line> highway; 23 int c[maxn]; 24 25 int lowbit(int x) 26 { 27 return x&(-x); 28 } 29 30 void update(int x) 31 { 32 while(x <= M) 33 { 34 c[x] += 1; 35 x += lowbit(x); 36 } 37 } 38 39 long long getsum(int x) 40 { 41 long long sum = 0; 42 while(x >= 1) 43 { 44 sum += c[x]; 45 x -= lowbit(x); 46 } 47 return sum; 48 } 49 50 int main() 51 { 52 scanf("%d",&T); 53 int cas = 0; 54 while(T--) 55 { 56 scanf("%d%d%d",&N,&M,&K); 57 highway.clear(); 58 memset(c,0,sizeof c); 59 for(int i=0,l,r;i<K;i++) 60 { 61 scanf("%d%d",&l,&r); 62 highway.push_back(line(l,r)); 63 } 64 sort(highway.begin(),highway.end()); 65 66 long long res = 0; 67 for(int i=0;i<highway.size();i++) 68 { 69 update(highway[i].r); 70 res += i - getsum(highway[i].r) + 1; 71 } 72 73 printf("Test case %d: %lld\n",++cas,res); 74 } 75 }
时间: 2024-10-15 06:48:37