山东省第二届ACM - Ivan comes again!

题目链接:Ivan comes again!

题意

有一个矩阵,有三种操作,add A B代表给矩阵的第A行B列的元素标记。 find A B代表让你输出 某一个标记元素的下标 ,且满足对应的下标分别大于A ,B, 如果没有就输出-1 . remove 代表移走该位置的标记。

思路

一开始就想的用set存,但是没用过set里面存pair,开始set里存的结构体,结构体里再写个排序,但最终不太对,又换成pair了,不得不说是真的方便,add和remove函数用Insert和erase。至于查找则用upper_bound.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
using namespace std;
int main()
{
int n,A,B,caser = 1;
while(cin >> n && n){
printf("Case %d:\n",caser++);
string s;
set<pair<int,int> >S;
set<pair<int,int> >::iterator it;
for(int i = 1; i <= n ; ++i)
{
cin >> s >> A >> B;
if(s[0] == 'a')
{
S.insert({A,B});
}
else if(s[0] == 'r')
{
S.erase({A,B});
}
else if(s[0] == 'f')
{
it = S.upper_bound({A,B});
for(; it != S.end(); it ++)
{
if(it -> first > A && it -> second > B)
break;
}
if(it == S.end())
cout << "-1" << endl;
else
cout << it -> first << ' ' << it -> second << endl;
}
}
cout << '\n';
}
}