缺省源

#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;
#define infile(x) freopen(x,"r",stdin)
#define outfile(x) freopen(x,"w",stdout)
#define errfile(x) freopen(x,"w",stderr)
using ll=long long;using ull=unsigned long long;
char *p1,*p2,buf[1<<20];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
#ifdef linux
#define pc putchar_unlocked
#else
#define pc putchar
#endif
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using namespace std;
#define infile(x) freopen(x,"r",stdin)
#define outfile(x) freopen(x,"w",stdout)
#define errfile(x) freopen(x,"w",stderr)
using ll = long long;using ull = unsigned long long;
char *p1,*p2,buf[1<<23];
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?EOF:*p1++)
#ifdef linux
#define pc putchar_unlocked
#else
#define pc putchar
#endif
namespace IO{
    template<typename T>inline bool read(T &x){x=0;char s=gc();bool f=true;for(;(s<'0'||'9'<s);s=gc()) {if(s=='-') f=false;if(s==EOF)return false;}for(;'0'<=s&&s<='9';s=gc()) x=(x<<1)+(x<<3)+(s^48);if(!f) x=~x+1;return true;}
    inline bool read(double &x){x=0.0;char s=gc();bool f=true;for(;(s<'0'||'9'<s);s=gc()) {if(s=='-') f=false;if(s==EOF)return false;}for(;'0'<=s&&s<='9';s=gc()) x=(x*10)+(s^48);if(s!='.'){return true;}double res=0.1;s=gc();for(;'0'<=s&&s<='9';res/=10,s=gc()) x+=(s^48)*res;x=f?x:-x;return true;}
    inline bool read(string &str){string ().swap(str);char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false; for(;s!=' '&&s!='\n'&&s!=EOF;s=gc())str.push_back(s);return true;}
    inline bool read_line(string &str){string ().swap(str);char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false;for(;s!='\n'&&s!=EOF;s=gc()){str.push_back(s);}return true;}
    inline bool read_line(char *str){int len=0;char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false;for(;s!='\n'&&s!=EOF;s=gc()){str[len]=s;len++;}str[len]='\0';return true;}
    inline bool read(char &s){char x=gc();for(;x==' '||x=='\n';x=gc());if(x==EOF||x==' '||x=='\n')return false;s=x;return true;}
    inline bool read(char *s){int len=0;char x=gc();for(;x==' '||x=='\n';x=gc());if(x==EOF)return false;for(;x!=' '&&x!='\n'&&x!=EOF;x=gc())s[len++]=x;s[len]='\0';return true;}
    template<class T,class... Args> inline bool read(T &x,Args&... args){return (read(x)&&read(args...));}
    template<class T>inline void write(T x){static T st[45];int top=0;if(x<0)x=~x+1,pc('-');do{st[top++]=x%10;}while(x/=10);while(top)pc(st[--top]^48);}
    inline void write(char x){pc(x);}
    inline void write(string s){for(int i=0;s[i];++i) pc(s[i]);}
    inline void write(char *s){int len=strlen(s);for(int i=0;i<len;++i) pc(s[i]);}
    inline void write(const char *s){int len=strlen(s);for(int i=0;i<len;++i) pc(s[i]);}
    template<class T,class... Args> inline void write(T x,Args... args){write(x);write(args...);}
}using namespace IO;
signed main(){
    #ifndef ONLINE_JUDGE
        infile("in.in");outfile("out.out");
    #endif
    
}
signed main(){
    #ifndef ONLINE_JUDGE
        infile("in.in");outfile("out.out");
    #else
    #endif
    
}

rand

#include<bits/stdc++.h>
#include<sys/timeb.h>
using namespace std;
#define infile(x) freopen(x,"r",stdin)
#define outfile(x) freopen(x,"w",stdout)
#define errfile(x) freopen(x,"w",stderr)
using ll=long long;using ull=unsigned long long;
#ifdef linux
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif
struct timeb timer;
namespace IO{
    template<typename T>inline bool read(T &x){x=0;char s=gc();bool f=true;for(;(s<'0'||'9'<s);s=gc()) {if(s=='-') f=false;if(s==EOF)return false;}for(;'0'<=s&&s<='9';s=gc()) x=(x<<1)+(x<<3)+(s^48);if(!f) x=~x+1;return true;}
    inline bool read(double &x){x=0.0;char s=gc();bool f=true;for(;(s<'0'||'9'<s);s=gc()) {if(s=='-') f=false;if(s==EOF)return false;}for(;'0'<=s&&s<='9';s=gc()) x=(x*10)+(s^48);if(s!='.'){return true;}double res=0.1;s=gc();for(;'0'<=s&&s<='9';res/=10,s=gc()) x+=(s^48)*res;x=f?x:-x;return true;}
    inline bool read(string &str){string res;str.swap(res);char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false; for(;s!=' '&&s!='\n'&&s!=EOF;s=gc())str.push_back(s);return true;}
    inline bool read_line(string &str){string res;str.swap(res);char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false;for(;s!='\n'&&s!=EOF;s=gc()){str.push_back(s);}return true;}
    inline bool read_line(char *str){int len=0;char s=gc();for(;s==' '||s=='\n';s=gc());if(s==EOF) return false;for(;s!='\n'&&s!=EOF;s=gc()){str[len]=s;len++;}str[len]='\0';return true;}
    inline bool read(char &s){char x=gc();for(;x==' '||x=='\n';x=gc());if(x==EOF||x==' '||x=='\n')return false;s=x;return true;}
    inline bool read(char *s){int len=0;char x=gc();for(;x==' '||x=='\n';x=gc());if(x==EOF)return false;for(;x!=' '&&x!='\n'&&x!=EOF;x=gc())s[len++]=x;s[len]='\0';return true;}
    template<class T,class... Args> inline bool read(T &x,Args&... args){return (read(x)&&read(args...));}
    template<class T>inline void write(T x){static T st[45];int top=0;if(x<0)x=~x+1,pc('-');do{st[top++]=x%10;}while(x/=10);while(top)pc(st[--top]^48);}
    inline void write(char x){pc(x);}
    inline void write(string s){for(int i=0;s[i];++i) pc(s[i]);}
    inline void write(char *s){int len=strlen(s);for(int i=0;i<len;++i) pc(s[i]);}
    inline void write(const char *s){int len=strlen(s);for(int i=0;i<len;++i) pc(s[i]);}
    template<class T,class... Args> inline void write(T x,Args... args){write(x);write(args...);}
}using namespace IO;
signed main(){
    #ifndef ONLINE_JUDGE
        infile("in.in");outfile("out.out");
    #else
    #endif
    ftime(&timer);
    srand(timer.time*1000+timer.millitm);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
}

while

#include <bits/stdc++.h>
using namespace std;
void Black() { printf("\033[37m"); }
void Red() { printf("\033[31m"); }
void Green() { printf("\033[32m"); }
void Yellow() { printf("\033[33m"); }
void Blue() { printf("\033[34m"); }
void Purple() { printf("\033[35m"); }
void DeepGreen() { printf("\033[36m"); }
void White() { printf("\033[37m"); }
int sys(string cmd) { return system(cmd.c_str()); }
#define nnow steady_clock::now()
#define dc duration_cast<duration<double>>(t2 - t1)
#define time ut.count()
#define pc putchar
#define pr printf
#define ps puts
#define Wall ps("--------------------------------------------")
#define CE(x)  Re(), ps("Error"), Bl(), Wall, exit(1);
#define COM(x) Pu(), pr("Compile " #x " "), Bl();
#define WA()   Wh(), pr("%03d ", i), Re(), ps("Wrong Answer"), Bl(), exit(1);
#define Bls "\033[37m"
#define Res "\033[31m"
#define Grs "\033[32m"
#define Yes "\033[33m"
#define BLs "\033[34m"
#define Pus "\033[35m"
#define DGs "\033[36m"
void BLK() { pr("\033[30m"); }
void Bl() { pr("\033[37m"); }
void Re() { pr("\033[31m"); }
void Gr() { pr("\033[32m"); }
void Ye() { pr("\033[33m"); }
void BL() { pr("\033[34m"); }
void Pu() { pr("\033[35m"); }
void DG() { pr("\033[36m"); }
void Wh() { pr("\033[37m"); }
void Bo() { pr("\033[1m"); }
#include <thread>
#include <mutex>
typedef long long ll;
using namespace std;
using namespace chrono;
string args[10], targ[10];
 
int cnt;
bool no_com = 0, out_ac = 0, qstop = 0;
int cas = 1000; double t = 1000; bool fast = 0; bool fast2 = 0; bool online = 0;
int mem = 128 * 1024; int Max = 10;
queue<thread> q;
 
bool Compile(string filename, string tf) {
    if (sys("g++ " + tf + ".cpp -o " + filename + " -Wall -Wno-unused-result -DDEBUG -DDP " + (fast ? " -O2" : (fast2 ? "" : " -O2 -fsanitize=address,undefined")) + (online ? "" : " -DONLINE_JUDGE"))) return Bo(), 0;
    Bo(), Gr(), ps("Done"), Bl();
    return 1;
}
 
mutex m;
double cpp_tot = 0, std_tot = 0, rd_tot = 0;
ll cpp_mem = 0, std_mem = 0, rd_mem = 0;
int cas_tot = 0, unac_tot = 0;
 
bool Test(int i) {
    char s = 'P', s2 = 'P';
    double cpp = NAN, std = NAN, rd = NAN;
    int cppm = -1, stdm  = -1, rdm = -1;
    FILE * fe; int ret; char tmp[34];
    // run rand
    string tm = (string) "./data/tm" + to_string(i).c_str() + ".log";
    ret = sys("ulimit -s " + to_string(mem + 100) + " && /usr/bin/time -f \"%e %M\" -o " + tm + " ./" + args[3] + " > ./data/rd" + to_string(i));
    if (ret) goto out;
    fe = fopen(tm.c_str(), "r"); fscanf(fe, "%lf%d", &rd, &rdm); rd *= 1000; fclose(fe);
    // run cpp
    ret = sys("ulimit -s " + to_string(mem + 100) + " && timeout 2> sb " + to_string(t / 1000 + 0.3) + " /usr/bin/time -f \"%e %M\" -o " + tm + " ./" + args[1] + " < ./data/rd" + to_string(i) + " > ./data/cpp" + to_string(i));
    if (ret == 31744) { s = 'T'; goto out; }
    if (ret == 35584) { s = 'M'; goto out; }
    if (ret) { s = 'R'; goto out; }
    fe = fopen(tm.c_str(), "r"); fscanf(fe, "%lf%d", &cpp, &cppm); cpp *= 1000; fclose(fe);
    if (cpp >= t) { s = 'T'; goto out; }
    if (cppm > mem) { s = 'M'; goto out; }
    // run std
    ret = sys("ulimit -s " + to_string(mem + 100) + " && timeout 2> sb " + to_string(t / 1000 + 0.3) + " /usr/bin/time -f \"%e %M\" -o " + tm + " ./" + args[2] + " < ./data/rd" + to_string(i) + " > ./data/std" + to_string(i));
    if (ret == 31744) { s2 = 'T'; goto out; }
    if (ret == 35584) { s2 = 'M'; goto out; }
    if (ret) { s2 = 'R'; goto out; }
    fe = fopen(tm.c_str(), "r"); fscanf(fe, "%lf%d", &std, &stdm); std *= 1000; fclose(fe);
    if (std >= t) { s2 = 'T'; goto out; }
    if (stdm > mem) { s2 = 'M'; goto out; }
    // diff
    if (sys("diff -Z ./data/cpp" + to_string(i) + " ./data/std" + to_string(i) + " > ./data/diff.log")) s = s2 = 'W';
    else s = s2 = 'A';
    // output
    out:
    lock_guard<mutex> Lock(m);
    ++cas_tot;
    if (s != 'A') ++unac_tot;
    if (s == 'A') cpp_tot += cpp, std_tot += std, rd_tot += rd, cpp_mem += cppm, std_mem += stdm, rd_mem += rdm;
    if (s != 'A' || out_ac) {
        if (s == 'A') Gr();
        else if (s == 'T' || s2 == 'T') Ye();
        else if (s == 'R' || s2 == 'R') Pu();
        else if (s == 'M' || s2 == 'M') BLK();
        else if (s == 'W') Re();
        pr("%03d ", i);
        putchar(s), putchar(' '), putchar(s2);
        pr(" %.0lfms %.0lfms %.0lfms %dMB %dMB %dMB\n", rd, cpp, std, rdm / 1024, cppm / 1024, stdm / 1024), Bl();
        if (qstop && s != 'A') { Wall, abort(); }
    }
    if (s != 'P' && s2 != 'P') sys(("rm " + tm).c_str());
    if (s != 'P') sys("rm ./data/cpp" + to_string(i));
    if (s2 != 'P') sys("rm ./data/std" + to_string(i));
    if (s == 'A') sys("rm ./data/rd" + to_string(i));
    return 0;
}
 
int main(int argv, char **argc) {
    Bo(), sys("rm -f ./data/*");
    for (int i = 1; i < argv; ++i) {
        string tmp = argc[i];
        if (tmp == "-n") no_com = 1;
        // no_com complie
        else if (tmp == "-l") ++i, cas = stoi(string(argc[i]));
        // testcases
        else if (tmp == "-t") ++i, t = stoi(string(argc[i]));
        // time limit
        else if (tmp == "-f") fast = 1;
        // fast mode (no_com fsanitize)
        else if (tmp == "-ff") fast2 = 1;
        // fast2 mode (no_com O2 & fsanitize)
        else if (tmp == "-m") ++i, mem = stoi(string(argc[i])) * 1024;
        // memory limit
        else if (tmp == "-a") out_ac = 1;
        // echo accept message
        else if (tmp == "-s") qstop = 1;
        // immediately stop when unaccepted
        else if (tmp == "-c") ++i, Max = stoi(string(argc[i]));
        // max number of threads
        else if (tmp == "-o") online = 1;
        // not online judge
        else args[++cnt] = argc[i];
    }
    targ[1] = args[1], targ[2] = args[2], targ[3] = args[3];
    pr("%scpp: %s%s.cpp\n", Pus, BLs, args[1].c_str());
    pr("%sstd: %s%s.cpp\n", Pus, BLs, args[2].c_str());
    pr("%srand: %s%s.cpp\n", Pus, BLs, args[3].c_str());
    if (fast) pr("%sFast Mode %sOn\n", Pus, BLs), pr("%sFsanitize %sDisabled\n", Pus, BLs);
    else if (fast2) pr("%sFast II Mode %sOn\n", Pus, BLs), pr("%sO2 %sDisabled\n", Pus, BLs), pr("%sFsanitize %sDisabled\n", Pus, BLs);
    if (no_com) pr("%sCompile %sDisabled\n", Pus, BLs);
    pr("%sTest Count: %s%d\n", Pus, BLs, cas);
    pr("%sThread Number: %s%d\n", Pus, BLs, Max);
    pr("%sTime Limit: %s%dms\n", Pus, BLs, int(t));
    pr("%sMemory Limit: %s%dMB\n", Pus, BLs, mem / 1024), Bl();
    Wall;
    if (!no_com) {
        COM(cpp); if (!Compile(args[1], targ[1])) CE(cpp);
        COM(std); if (!Compile(args[2], targ[2])) CE(std);
        COM(rand); if (!Compile(args[3], targ[3])) CE(rand);
    }
    Pu(), ps("Now Begin Test"), Bl(), Wall;
    steady_clock::time_point t1, t2; duration<double> ut;
    t1 = nnow;
    for (int i = 1; i != (cas + 1); ++i) {
        if ((int) q.size() > Max) q.front().join(), q.pop();
        q.push(thread(Test, i));
        if (i % 100 == 0) {
            lock_guard<mutex> Lock(m);
            int ac = cas_tot - unac_tot;
            printf("%s%d %sCases, %s%d %sWrongs, %s%.2lf%%%s\n", BLs, cas_tot, Pus, BLs, unac_tot, Pus, BLs, ac * 1.0 / cas_tot * 100, Pus);
            t2 = nnow; ut = dc;
            printf("Total: %s%.3lfs\n", BLs, ut.count());
            printf("%sAverage: %s%.0lfms %.0lfms %.0lfms %.0lfMB %.0lfMB %.0lfMB\n", Pus, BLs, rd_tot / ac, cpp_tot / ac, std_tot / ac, 1.0 * rd_mem / ac / 1024, 1.0 * cpp_mem / ac / 1024, 1.0 * std_mem / ac / 1024), Bl();
            Wall;
        }
    }
    while (!q.empty()) q.front().join(), q.pop();
    t2 = nnow; ut = dc;
    int ac = cas_tot - unac_tot;
    printf("%s%d %sCases, %s%d %sWrongs, %s%.2lf%%%s\n", BLs, cas_tot, Pus, BLs, unac_tot, Pus, BLs, ac * 1.0 / cas_tot * 100, Pus);
    printf("Total: %s%.3lfs\n", BLs, ut.count());
    printf("%sAverage: %s%.0lfms %.0lfms %.0lfms %.0lfMB %.0lfMB %.0lfMB\n", Pus, BLs, rd_tot / ac, cpp_tot / ac, std_tot / ac, 1.0 * rd_mem / ac / 1024, 1.0 * cpp_mem / ac / 1024, 1.0 * std_mem / ac / 1024), Bl();
    Bl(), Wall;
    sys("rm -f ./data/.f*");
    sys("rm sb");
    Gr(), ps("Finished"), Bl(), Wall;
}

具体用法:
1.标准输入输出,开一个data文件夹
2.编译时加上 -pthread 后接 my std data (省略拓展名.cpp)
即 g++ -pthread while.cpp -o while && ./while my std data
接下来的,省略即为默认值
3.-l n :将测试点数设为n个(默认为1000个)
4.-t time :将时间限制设为time毫秒(默认为1000ms)
5.-m Mem :将内存限制设为MemMB(默认为128MB)
6.-f :关闭fsanitize
7.-ff :关闭O2和fsantize
8.-a :返回Accepted信息
9.-s :出现错误时立即终止
(转载自eafoo

posted @ 2024-04-17 17:29  CuFeO4  阅读(13)  评论(0编辑  收藏  举报