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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <map> #include <stack> using namespace std; #define MAXN 50005 bool ok[MAXN]; int main(){ int n = 0,m = 0; while (cin >> n >> m) { stack<int> op; set<int> broken; memset(ok, true,sizeof(ok)); broken.clear(); while (!op.empty()) { op.pop(); } ok[0] = false; for (int i = 1; i <= m; i++) { char cmd; int arg; scanf(" %c",&cmd); if (cmd == 'D') { scanf(" %d",&arg); op.push(arg); broken.insert(arg); ok[arg] = false; }else if(cmd == 'Q'){ scanf(" %d",&arg); if (broken.size() == 0) { cout << n << endl; continue; } set<int>::iterator it = lower_bound(broken.begin(), broken.end(), arg); if (*it == arg) { cout << 0 << endl; continue; }else{ int l = 0,r = 0; if (it != broken.end()) { r = *it; r--; }else{ r = n; } if (it == broken.begin()) { l = 0; }else{ it--; l = *it; } //cout << "+" << l << "+" << r << endl; cout << r-l << endl; } }else{ if (op.empty()) { continue; } ok[op.top()] = true; broken.erase(broken.find(op.top())); op.pop(); } } } return 0; } |
这个是用STL水过去的啊,可不是什么正规做法。