sdut 2159 Ivan comes again! C++ STL set
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2159
Ivan comes again!
Time Limit: 1000ms Memory limit: 65536K
200000 operations:
1)add: Mark an element in the matrix. The element wasn’t marked before it is marked.
2)remove: Delete an element’s mark. The element was marked before the element’s mark is deleted.
3)find:
Show an element’s row and column, and return a marked element’s row
and column, where the marked element’s row and column are larger than
the showed element’s row and column respectively. If there are multiple
solutions, return the element whose row is the smallest; and if there
are still multiple solutions, return the element whose column is the
smallest. If there is no solution, return -1.
#include <cstdio> #include <iostream> #include <set> using namespace std; set<pair<int,int> > S; set<pair<int,int> >::iterator it; int main() { int n,cases=0,x,y; char str[10]; while(scanf("%d",&n),n) { S.clear(); printf("Case %d:\n",++cases); while(n--) { getchar(); scanf("%s%d%d",str,&x,&y); switch(str[0]) { case 'a': S.insert(make_pair(x,y)); break; case 'r': S.erase(make_pair(x,y)); break; case 'f': for(it=S.upper_bound(make_pair(x,y)); it!=S.end(); it++) { if(it->first>x&&it->second>y) break; } if(it==S.end()) puts("-1"); else printf("%d %d\n",it->first,it->second); break; } } puts(""); } return 0; }