CPPSET - Tập hợp động

Tác giả: RR

Ngôn ngữ: C++

//Written by technolt
#include <iostream>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>

using namespace std;

#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define DOWN(i,a,b) for(int i = a; i >= b; i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define doc(x); scanf("%d",&x);
#define in(x) printf("%d\n",x)


set <int > positive,negative;

int main() {
    typeof(positive.begin()) it;
    while (1) {
        int command,x;
        doc(command);
        if (command==0) return 0;
        else if (command==1) {
            doc(x);
            positive.insert(x);
            negative.insert(-x);
        } else if (command==2) {
            doc(x);
            positive.erase(x);
            negative.erase(-x);
        } else if (command==3) {
            if (positive.empty()) printf("empty\n");
            else {
                it=positive.begin();
                in(*it);
            }
        } else if (command==4) {
            if (positive.empty()) printf("empty\n");
            else {
                it=positive.end();
                it--;
                in(*it);
            }
        } else if (command==5) {
            doc(x);
                
            if (positive.empty()) 
                printf("empty\n");
            else {
                it=positive.upper_bound(x); 
                if (it==positive.end()) printf("no\n");
                else in(*it);
            }
        } else if (command==6) {
            doc(x);
            if (positive.empty()) printf("empty\n");
            else {
                it=positive.lower_bound(x);
                if (it==positive.end()) printf("no\n") ;
                else in(*it);
            }
        } else if (command==7) {
            doc(x);
            if (negative.empty()) printf("empty\n");
            else {
                it=negative.upper_bound(-x);
                if (it==negative.end()) printf("no\n");
                else in(-*it);
            }
        } else if (command==8) {
            doc(x);
            if (negative.empty()) printf("empty\n");
            else {
                it=negative.lower_bound(-x);
                if (it==negative.end()) printf("no\n");
                else in(-*it);
            }
        }
    }
	return 0;
}

Download