Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:一样的代码C++能过,G++TLE

Posted by miklcct at 2012-07-14 21:15:10 on Problem 2823
In Reply To:一样的代码C++能过,G++TLE Posted by:biran007 at 2009-08-10 01:09:51
> 为啥会这样?
> 这种情况经常出现么?
> 还有,我的O(nlogk)的要6秒,O(n)代码为啥也跑了5秒?
> 数据到底有多么巨大饿……


#include <cstdio>
#include <cstdlib>
#include <stdexcept>

using namespace std;

template<class T> class Fuck {
  public:
    Fuck(size_t n = 1000005);
    ~Fuck();
    T &front() {
        if (empty()) {
            throw runtime_error("Container is empty");
        }
        return *sp;
    }
    T &back() {
        if (empty()) {
            throw runtime_error("Container is empty");
        }
        return ep == data ? ep[n] : ep[-1];
    }
    const T &front() const;
    const T &back() const;
    void push_back(const T &);
    void push_front(const T &);
    void pop_back();
    void pop_front();
    size_t size() const {
        if (ep < sp) return -(sp - ep) + (n + 1);
        return ep - sp;
    }
    bool empty() const {
        return sp == ep;
    }
  private:
    size_t n;
    T *data, *sp, *ep;
};

int main() {
    // snipped
}

template<class T> Fuck<T>::Fuck(size_t nn) : n(nn), data(reinterpret_cast<T *>(malloc((n + 1) * sizeof(T)))), sp(data), ep(data) {}

template<class T> Fuck<T>::~Fuck() {
    while (!empty()) {
        pop_back();
    }
    free(data);
}

template<class T> void Fuck<T>::pop_back() {
    if (empty()) {
        throw runtime_error("Container is empty");
    }
    back().~T();
    if (ep == data) {
        ep = &data[n];
    } else {
        --ep;
    }
}

template<class T> void Fuck<T>::push_back(const T &value) {
    if (size() == n) {
        throw runtime_error("Container is full");
    }
    new(ep++) T(value);
    if (ep == data + (n + 1)) ep = data;
}

template<class T> void Fuck<T>::pop_front() {
    if (empty()) {
        throw runtime_error("Container is empty");
    }
    front().~T();
    ++sp;
    if (sp == data + (n + 1)) sp = data;
}

用STL個deque,寫極都TLE,我換咗個class去,用C++隊,AC,用G++隊一樣TLE

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator