【题意】
题意太麻烦,自己读去。。。。
【分析】
其实就是一个树形结构,每次询问两个叶子节点的最近公共祖先。
数据小,暴力之。。。
水题。。。
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<algorithm> 5 #define maxn 2220 6 7 using namespace std; 8 9 struct node{ 10 int xl,yl; 11 int xr,yr; 12 int num; 13 int deep; 14 int flag; 15 int l; 16 int r; 17 int fat; 18 void output() 19 { 20 printf("%d %d %d %d\n",xl,yl,xr,yr); 21 printf("l = %d r = %d %d\n",l,r,fat); 22 } 23 bool inside(int x,int y) 24 { 25 return (xl<x && x<xr && yl<y&&y<yr); 26 } 27 bool in(int x,int y) 28 { 29 return (xl<=x && x<=xr && yl<=y&&y<=yr); 30 } 31 }seq[maxn]; 32 int tot; 33 int total; 34 void build(int xl,int yl,int xr,int yr,int root,int fat) 35 { 36 seq[root].xl = xl; 37 seq[root].yl = yl; 38 seq[root].xr = xr; 39 seq[root].yr = yr; 40 seq[root].deep = 0; 41 seq[root].l = 0; 42 seq[root].r = 0; 43 seq[root].flag = 0; 44 seq[root].fat = fat; 45 seq[root].num = 0; 46 //printf("root = %d %d %d %d %d\n",root,xl,yl,xr,yr); 47 return ; 48 } 49 int n,m; 50 51 int get(int xl,int yl,int xr,int yr) 52 { 53 for (int i=tot;i>=1;i--) 54 if (seq[i].flag == 0 && seq[i].in(xl,yl) && seq[i].in(xr,yr)) 55 return i; 56 return 0; 57 } 58 void update(int xl,int yl,int xr,int yr) 59 { 60 int root = get(xl,yl,xr,yr); 61 62 int xxl = seq[root].xl; 63 int yyl = seq[root].yl; 64 int xxr = seq[root].xr; 65 int yyr = seq[root].yr; 66 67 seq[root].flag = 1; 68 if (xl == xxl){ 69 build(xxl,yyl,xr,yr,++tot,root); 70 seq[root].l = tot; 71 build(xl,yl,xxr,yyr,++tot,root); 72 seq[root].r = tot; 73 }else{ 74 build(xxl,yyl,xr,yr,++tot,root); 75 seq[root].l = tot; 76 build(xl,yl,xxr,yyr,++tot,root); 77 seq[root].r = tot; 78 } 79 return ; 80 } 81 int find(int x,int y) 82 { 83 for (int i=tot;i>=1;i--) 84 if (seq[i].flag==0 && seq[i].inside(x,y)) 85 return i; 86 return 0; 87 } 88 89 bool fff[maxn][maxn]; 90 void dfs(int root,int niuniu) 91 { 92 //printf("%d %d\n",root,niuniu); 93 fff[niuniu][root] = true; 94 if (seq[root].l > 0) 95 dfs(seq[root].l,niuniu); 96 if (seq[root].r > 0) 97 dfs(seq[root].r,niuniu); 98 return ; 99 } 100 void dfs1(int root,int deep) 101 { 102 int l = seq[root].l; 103 int r = seq[root].r; 104 seq[root].deep = deep; 105 if (l > 0){ 106 dfs1(l,deep+1); 107 seq[root].num += seq[l].num; 108 } 109 if (r > 0){ 110 dfs1(r,deep+1); 111 seq[root].num += seq[r].num; 112 } 113 return ; 114 } 115 void init() 116 { 117 total = 0; 118 for (int i=1;i<=tot;i++) 119 if (seq[i].flag==0){ 120 seq[i].num = 1; 121 total++;//这个地方写错成tot++,错了好久! 122 } 123 memset(fff,0,sizeof(fff)); 124 dfs1(1,1); 125 // puts("niuniu"); 126 for (int i=1;i<=tot;i++) 127 dfs(i,i); 128 129 return ; 130 } 131 int solve(int u,int v) 132 { 133 for (int i=tot;i>=1;i--) 134 { 135 if (fff[i][u] && fff[i][v]){ 136 int res = total - seq[i].num + 1; 137 return res; 138 } 139 } 140 return 0; 141 } 142 int main() 143 { 144 int xl,yl,xr,yr; 145 while (scanf("%d%d%d%d",&xl,&yl,&xr,&yr)==4) 146 { 147 tot = 0; 148 build(xl,yl,xr,yr,++tot,0); 149 scanf("%d%d",&n,&m); 150 for (int i=1;i<=n;i++) 151 { 152 scanf("%d%d%d%d",&xl,&yl,&xr,&yr); 153 if (xl == xr) 154 { 155 if (yl > yr) 156 { 157 swap(yl,yr); 158 } 159 } 160 if (yl==yr) 161 { 162 if (xl > xr) 163 { 164 swap(xl,xr); 165 } 166 } 167 update(xl,yl,xr,yr); 168 } 169 for (int i=1;i<=tot;i++){ 170 //printf("%d***********\n",i); 171 // seq[i].output(); 172 } 173 init(); 174 //puts("dg"); 175 for (int i=1;i<=m;i++) 176 { 177 scanf("%d%d%d%d",&xl,&yl,&xr,&yr); 178 int p1 = find(xl,yl); 179 int p2 = find(xr,yr); 180 // printf("%d %d\n",p1,p2); 181 int ans = solve(p1,p2); 182 printf("%d\n",ans); 183 } 184 } 185 return 0; 186 }