Beispiel #1
0
int main() {
    setST();
    int n, mn, arg;
    int base_line = 100005;
    int tot = 0, leave = 0, tmp;
    char cmd[20];
    scanf("%d %d", &n, &mn);
    while(n--) {
        scanf("%s %d", cmd, &arg);
        if(cmd[0] == 'I') {
            if(arg >= mn) {
                modify(arg+base_line, 1), tot++;
            }
        } else if(cmd[0] == 'A') {
            base_line -= arg; // 基線下降
        } else if(cmd[0] == 'S') {
            if(arg == 0)    continue;
            tmp = query(0, M-1, base_line, base_line+arg+mn-1, 1);
            tot -= tmp, leave += tmp;
            modifyCLR(0, M-1, base_line, base_line+arg+mn-1, 1);
            base_line += arg; // 基線上升
        } else {
            if(tot < arg) {
                puts("-1");
                continue;
            }
            int s;
            arg = tot-arg+1;
            for(s = 1; s < M;) {
                relax(s);
                if(ST[s<<1] < arg)
                    arg -= ST[s<<1], s = s<<1|1;
                else
                    s = s<<1;
            }
            printf("%d\n", s-M-base_line);
        }
    }
    printf("%d\n", leave);
    return 0;
}
int OnlineSession::run(istream &in, bool interactive)
{
    map<string, int> commands;
    _prepareCommands(commands);

    int res = 0;
    int count = 0;

    int cmd;
    string command;

    string sarg1, sarg2;
    int iarg1, iarg2, iarg3, iarg4, iarg5, iarg6, iarg7, iarg8;
    SeriesDistanceMetric *metric;
    double darg1;
    GroupableTimeSeriesSet *t;

    clock_t time;

    res = 0;

    while (!in.eof()) {

        if (res != 0)
            getout() << "Command returned with status " << res << "." << endl;

        if (interactive) {
            int width = getout().width();
            getout() << "[";

            getout().width(3);
            getout() << count;
            getout().width(width);

            getout() << "] > ";
            getout().flush();
        }

        in >> command;

        if (command.size() > 0 && !in.eof()) {
            cmd = commands[command];
        } else {
            cmd = _EXIT;
            getout() << endl;
        }

        time = clock();

        try {
            res = 0;

            switch (cmd) {
            case 0:
                getout() << "Unknown command '" << command << "'. Type 'help' for help." << endl;

                res = 1;
                break;

            case _EXIT:
                getout() << "Quitting..." << endl;

                return 0;

            case _HELP:
                _print_help(getout());

                break;

            case _DEBUG:
                in >> iarg1;

                getout() << "Setting verbosity to " << iarg1 << "." << endl;

                verbosity = iarg1;

                break;

            case _LOAD_TSS:
                in >> sarg1;

                getout() << "Loading Time Series Set from file '" << sarg1 << "'." << endl;

                res = loaddb(sarg1.c_str());
                if (res == 0) {
                    getout() << "Dataset successfully loaded. Index: " << datasets.size()-1 << endl;
                } else {
                    getout() << "Failed to load dataset." << endl;
                }

                break;

            case _SAVE_TSS:
                in >> iarg1;
                in >> sarg2;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Saving Time Series Set " << iarg1 << ":" << t->getName() << " to file '" << sarg2 << "'." << endl;

                res = savedb(iarg1, sarg2.c_str());
                if (res == 0)
                    getout() << "Dataset successfully saved." << endl;
                else
                    getout() << "Failed to save dataset." << endl;

                break;

            case _OLOAD_TSS:
                in >> sarg1;
                in >> iarg1 >> iarg2 >> iarg3;

                getout() << "Loading Time Series Set from file '" << sarg1 << "' with N=" << iarg1 << ", L=" << iarg2 << ", and D=" << iarg3 << "." << endl;

                res = loadolddb(sarg1.c_str(), iarg1, iarg2, iarg3);
                if (res == 0) {
                    getout() << "Dataset successfully loaded. Index: " << datasets.size()-1 << endl;
                } else {
                    getout() << "Failed to load dataset." << endl;
                }

                break;

            case _OSAVE_TSS:
                in >> iarg1;
                in >> sarg2;

                checkIndex(iarg1);
                t = datasets[iarg1];

                getout() << "Saving Time Series Set " << iarg1 << ":" << t->getName() << " to file '" << sarg2 << "'." << endl;

                res = saveolddb(iarg1, sarg2.c_str());
                if (res == 0)
                    getout() << "Dataset successfully saved." << endl;
                else
                    getout() << "Failed to save dataset." << endl;

                break;

            case _DROP_TSS:
                in >> iarg1;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Dropping Time Series Set " << iarg1 << ":" << t->getName() << "." << endl;

                killdb(iarg1);

                break;

            case _RAND_TSS:
                in >> iarg1 >> iarg2 >> iarg3;

                getout() << "Generating random Time Series Set with N=" << iarg1 << ", L=" << iarg2 << ", and range=" << iarg3 << "." << endl;

                res = randdb(iarg3, iarg1, iarg2);
                if (res == 0)
                    getout() << "Dataset successfully loaded. Index: " << datasets.size()-1 << endl;
                else
                    getout() << "Failed to load dataset." << endl;

                break;

            case _LIST_TSS:

                res = printdbs();

                break;

            case _NORM_TSS:
                in >> iarg1;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Normalizing Time Series Set " << iarg1 << ":" << t->getName() << "." << endl;

                t->normalize();

                break;

            case _KSIM_TSS:
                in >> iarg1 >> iarg2 >> iarg3 >> iarg4 >> iarg5 >> iarg6;

                checkIndex(iarg1);
                checkIndex(iarg2);
                t = datasets[iarg1];
                getout() << "Searching similar sequences for Time Series Set " << iarg1 << ":" << t->getName();
                t = datasets[iarg2];
                getout() << ", query string at " << iarg3 << " in dataset " << iarg2 << ":" << t->getName();
                getout() << " in interval [" << iarg4 << "," << iarg5 << "] with strategy="
                     << iarg6 << "." << endl;

                similar(iarg1, iarg2, iarg3, TimeInterval(iarg4, iarg5), iarg6);

                break;

            case _OUTLIER_TSS:
                in >> iarg1 >> iarg2;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Searching for outlier group in dataset " << iarg1 << ":" << t->getName() << " for length=" << iarg2 << "." << endl;

                outlier(iarg1, iarg2);

                break;

            case _TSS_DIST:
                in >> iarg1 >> iarg2 >> iarg3 >> iarg4;
                in >> iarg5 >> iarg6 >> iarg7 >> iarg8;
                in >> sarg1;

                checkIndex(iarg1);
                checkIndex(iarg5);

                metric = getDistMetric(sarg1.c_str());
                if (metric == NULL) {
                    getout() << "Unknown method: " << sarg1.c_str() << endl;
                    res = -1;
                    break;
                }

                getout() << "Using distance metric " << metric->name << " to find distance:" << endl;
                getout() << "A: DB:" << iarg1 << ", " << iarg2 << "@[" << iarg3 << "," << iarg4 << "]." << endl;
                getout() << "B: DB:" << iarg5 << ", " << iarg6 << "@[" << iarg7 << "," << iarg8 << "]." << endl;
                getout() << "Distance: " << printdist(iarg1, iarg5,
                                                      iarg2, iarg6,
                                                      TimeInterval(iarg3, iarg4), TimeInterval(iarg7, iarg8),
                                                      metric) << endl;

                break;

            case _LS_DIST:

                printDistMetrics();

                break;

            case _GROUP_TSS:
                in >> iarg1;

                checkIndex(iarg1);
                t = datasets[iarg1];

                getout() << "Generating new grouping for Time Series Set "
                     << iarg1 << ":" << t->getName() << " with ST " << defaultST << "." << endl;

                res = initdbgroups(iarg1, defaultST);

                break;

            case _GROUP_ST_TSS:
                in >> iarg1;
                in >> darg1;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Generating new grouping for Time Series Set "
                     << iarg1 << ":" << t->getName() << " with ST " << darg1 << "." << endl;

                res = initdbgroups(iarg1, darg1);

                break;

            case _LOAD_GROUP_TSS:
                in >> iarg1;
                in >> sarg1;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Loading grouping data for Time Series Set "
                     << iarg1 << ":" << t->getName() << " at " << sarg1 << "." << endl;

                res = loaddbgroups(iarg1, sarg1.c_str());

                break;

            case _SAVE_GROUP_TSS:
                in >> iarg1;
                in >> sarg1;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Saving grouping data for Time Series Set "
                     << iarg1 << ":" << t->getName() << " to " << sarg1 << "." << endl;

                res = savedbgroups(iarg1, sarg1.c_str());

                break;

            case _DROP_GROUP_TSS:
                in >> iarg1;

                checkIndex(iarg1);
                t = datasets[iarg1];
                getout() << "Dropping grouping data for Time Series Set "
                     << iarg1 << ":" << t->getName() << "." << endl;

                res = killdbgroups(iarg1);

                break;

            case _DESC_TSS:
                in >> iarg1;

                checkIndex(iarg1);
                res = descdb(iarg1);

                break;

            case _PRINT_TSS:
                in >> iarg1;

                checkIndex(iarg1);
                res = printdb(iarg1);

                break;

            case _PRINT_INTERVAL:
                in >> iarg1 >> iarg2 >> iarg3 >> iarg4;

                checkIndex(iarg1);
                res = printint(iarg1, iarg2, TimeInterval(iarg3, iarg4));

                break;

            case _SET_DEF_ST:
                in >> darg1;

                getout() << "Setting default ST to " << darg1 << "." << endl;

                res = setST(darg1);

                break;

            case _GET_DEF_ST:
                getout() << "default ST = " << getST() << "." << endl;

                break;

            case _SET_DEF_R:
                in >> iarg1;

                getout() << "Setting default R constraint to " << iarg1 << "." << endl;

                res = setR(iarg1);

                break;

            case _GET_DEF_R:
                getout() << "default R = " << getR() << "." << endl;

                break;
            }

        } catch (exception &e) {
            getout() << "Caught exception attempting operation:" << endl;
            getout() << e.what() << endl;
        }

        time = clock() - time;
        getout() << "Command used " << ((float)time/CLOCKS_PER_SEC) << " seconds." << endl << endl;

        count++;
    }

    return 0;
}