题意:给你一个二维平面,m个询问 横着和竖着划一条线,问你最大矩形面积是多少。
解题思路:
1)不知道为什么感觉不能用优先队列找最大值以后就脑抽的想到了线段树找最大值,真是悲剧的开始。
线段树 + map 迭代器
解题代码:
1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月18日 星期三 01时04分52秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 #define maxn 200005 26 using namespace std; 27 int w, h , n; 28 char str[10]; 29 int v; 30 struct node{ 31 int l , r , m ,v,lazy; 32 }; 33 map<int ,int> wmp; 34 map<int ,int> hmp; 35 class sg{ 36 public: 37 struct node tree[maxn*4]; 38 int L(int x) 39 { 40 return 2 * x; 41 } 42 int R(int x) 43 { 44 return 2* x + 1; 45 } 46 void build(int c, int l , int r ,int v) 47 { 48 tree[c].l = l ; 49 tree[c].r = r; 50 tree[c].m = (l + r)/2; 51 tree[c].lazy = 0 ; 52 if(l == r) 53 { 54 tree[c].v = v; 55 return; 56 } 57 build(L(c),l,tree[c].m,v); 58 build(R(c),tree[c].m+1,tree[c].r,v); 59 push_up(c); 60 } 61 void push_up(int c) 62 { 63 tree[c].v = max(tree[L(c)].v,tree[R(c)].v); 64 } 65 void push_down(int c) 66 { 67 if(tree[c].lazy != 0 ) 68 { 69 tree[L(c)].v = tree[c].lazy; 70 tree[R(c)].v = tree[c].lazy; 71 tree[L(c)].lazy = tree[c].lazy; 72 tree[R(c)].lazy = tree[c].lazy; 73 tree[c].lazy = 0 ; 74 } 75 } 76 void update(int c, int l , int r,int v) 77 { 78 if(l <= tree[c].l && r >= tree[c].r) 79 { 80 //printf("%d**%d ",tree[c].v,v); 81 tree[c].lazy = v; 82 tree[c].v = v; 83 return; 84 } 85 push_down(c); 86 if(l <= tree[c].m ) 87 update(L(c),l,r,v); 88 if(r > tree[c].m) 89 update(R(c),l,r,v); 90 push_up(c); 91 } 92 93 }t1,t2; 94 int main(){ 95 scanf("%d %d %d",&w,&h,&n); 96 t1.build(1,1,w,w); 97 t1.tree[1].lazy = w; 98 t2.build(1,1,h,h); 99 t2.tree[1].lazy = h; 100 wmp[0] = 1; 101 wmp[w] = 1; 102 hmp[0] = 1; 103 hmp[h] = 1; 104 map<int ,int >::iterator l,m,r; 105 int tmpv; 106 for(int i = 1;i <= n;i ++) 107 { 108 scanf("%s %d",str,&tmpv); 109 if(str[0] == 'H') 110 { 111 if(hmp.find(tmpv) == hmp.end()) 112 { 113 hmp[tmpv] = 1; 114 l = hmp.find(tmpv); 115 l --; 116 m = hmp.find(tmpv); 117 r = hmp.find(tmpv); 118 r ++ ; 119 t2.update(1,l->first,m->first,m->first- l->first); 120 t2.update(1,m->first+1,r->first,r->first - m->first); 121 // printf("%d %d ",m->first - l->first+1,r->first - m->first); 122 } 123 }else{ 124 if(wmp.find(tmpv)== wmp.end() ) 125 { 126 wmp[tmpv] = 1; 127 l = wmp.find(tmpv); 128 l --; 129 m = wmp.find(tmpv); 130 r = wmp.find(tmpv); 131 r ++ ; 132 // printf("%d %d ",m->first ,l->first); 133 //if(l->first == 1 ) 134 t1.update(1,l->first,m->first,m->first- l->first ); 135 t1.update(1,m->first+1,r->first,r->first - m->first); 136 // printf("%d %d %d ",t1.tree[1].v,m->first - l->first+1,r->first - m->first); 137 } 138 } 139 //printf("%d %d ",t1.tree[1].v,t2.tree[1].v); 140 printf("%I64d ",1ll*t1.tree[1].v*t2.tree[1].v); 141 } 142 return 0; 143 }
2)set + multiset ,时间效率好像低一点点,但是真的好写.
解题代码:
1 // File Name: c.1.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月18日 星期三 10时08分02秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 26 using namespace std; 27 int w,h , n; 28 set<int> wss,hs; 29 multiset<int> mwss,mhs; 30 char str[10]; 31 int main(){ 32 scanf("%d %d %d",&w,&h,&n); 33 wss.insert(0); 34 wss.insert(w); 35 hs.insert(h); 36 hs.insert(0); 37 mhs.insert(h); 38 mwss.insert(w); 39 set<int>::iterator l , r, m ; 40 multiset<int>::iterator tmp ; 41 for(int i = 1;i <= n;i ++) 42 { 43 int tmpv; 44 scanf("%s %d",str,&tmpv); 45 if(str[0]== 'H'){ 46 if(hs.find(tmpv) == hs.end()) 47 { 48 hs.insert(tmpv); 49 l = hs.find(tmpv); 50 l -- ; 51 r = hs.find(tmpv); 52 r ++; 53 int t = *r - *l; 54 // printf("%d ",t); 55 tmp = mhs.find(t); 56 int k = * tmp ; 57 mhs.erase(tmp); 58 mhs.insert(*r-tmpv); 59 mhs.insert(tmpv-*l); 60 61 } 62 }else{ 63 if(wss.find(tmpv) == wss.end()) 64 { 65 wss.insert(tmpv); 66 l = wss.find(tmpv); 67 l -- ; 68 r = wss.find(tmpv); 69 r ++; 70 int t = *r - *l; 71 // printf("%d ",t); 72 tmp = mwss.find(t); 73 int k = * tmp ; 74 mwss.erase(tmp); 75 mwss.insert(*r-tmpv); 76 mwss.insert(tmpv-*l); 77 } 78 79 } 80 tmp = mhs.end(); 81 tmp -- ; 82 LL sum = *tmp; 83 //printf("%d ",*tmp); 84 tmp = mwss.end(); 85 tmp -- ; 86 sum *= (*tmp); 87 //printf(" %d ",*tmp); 88 printf("%I64d ",sum); 89 } 90 return 0; 91 }