QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#534308#5494. 软件包管理器ancientaCompile Error//C++205.1kb2024-08-27 01:32:242024-08-27 01:32:24

Judging History

你现在查看的是最新测评结果

  • [2024-08-27 01:32:24]
  • 评测
  • [2024-08-27 01:32:24]
  • 提交

answer

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
LL father[100000];
LL son[100000];
LL sizes[100000];
LL ancestor[100000];
LL no[100000];
LL en[100000];
LL inde;
vector<LL> child[100000];
class segment_tree
{
public:
    segment_tree* left_tree;
    segment_tree* right_tree;
    LL left;
    LL right;
    LL middle;
    LL total;
    LL tag; //-1 for empty, 0 for default, and 1 for full
    segment_tree(LL left, LL right)
    {
        this->tag = -1;
        this->total = 0;
        this->left = left;
        this->right = right;
        this->middle = (left + right) / 2;
        if (left >= right) return;
        this->left_tree = new segment_tree(left, middle);
        this->right_tree = new segment_tree(middle + 1, right);
    }
    LL query(LL left, LL right)
    {
        //cout << "querying " << left << " " << right << endl;
        if (left > right) return 0;
        if (-1 == this->tag)
        {
            //cout << "tag -1 " << endl;
            return 0;
        }
        if (1 == this->tag)
        {
            //cout << "tag 1 " << endl;
            return right - left + 1;
        }
        if (left == right && this->left == left && this->right == right && 0 == this->tag) return 0;
        LL s = 0;
        if (left <= this->middle) s+= this->left_tree->query(left, min(right, this->middle));
        if (right > this->middle) s+= this->right_tree->query(max(left, this->middle + 1), right);
        //cout << "just return " << s << endl;
        return s;
    }
    void update(LL left, LL right, LL tag)
    {
        //cout << "updating " << left << " " << right << endl;
        if (left > right) return;
        if (this->left == left && this->right == right)
        {
            this->tag = tag;
            return;
        }
        if (0 != this->tag) this->left_tree->tag = this->tag;
        if (left <= this->middle)
        {

            this->left_tree->update(left, min(right, this->middle), tag);
        }
        if (0 != this->tag) this->right_tree->tag = this->tag;
        if (this->middle < right)
        {
            this->right_tree->update(max(left, this->middle + 1), right, tag);
        }
        this->tag = 0;
    }
};

void depth_first_search(LL node)
{
    LL s = 0;
    for (auto it = child[node].begin(); it != child[node].end(); ++it)
    {
        LL a = *it;
        depth_first_search(a);
        s += sizes[a];
    }
    sizes[node] = s + 1;
}
void depth_first_search2(LL node, LL node2)
{
    no[node] = inde;
    inde ++;
    ancestor[node] = node2;
    if (0 == child[node].size())
    {
        en[node] = inde - 1;
        return;
    }
    LL max_size = 0;
    for (auto it = child[node].begin(); it != child[node].end(); ++it)
    {
        LL a = *it;
        if (max_size < sizes[a])
        {
            max_size = sizes[a];
            son[node] = a;
        }
    }
    depth_first_search2(son[node], node2);
    for (auto it = child[node].begin(); it != child[node].end(); ++it)
    {
        LL a = *it;
        if (a == son[node]) continue;
        depth_first_search2(a, a);
    }
    en[node] = inde - 1;
}
int main()
{
    memset(father,-1,sizeof(father));
    memset(son,-1,sizeof(son));
    father[0] = 0;
    LL n;
    cin >> n;
    for (LL i = 1; i < n; ++i)
    {
        cin >> father[i];
        child[father[i]].push_back(i);
    }
    depth_first_search(0);
    /*for (LL i = 0; i < n; ++i)
    {
        cout << sizes[i] << endl;
    }*/
    inde = 0;
    depth_first_search2(0, 0);
    for (LL i = 0; i < n; ++i)
    {
        //cout << "node: " << i << endl;
        //cout << "no " << no[i] << endl;
        //cout << "father " << father[i] << endl;
        //cout << "ancestor " << ancestor[i] << endl;
    }
    segment_tree* tree = new segment_tree(0, n - 1);
    LL q;
    cin >> q;
    for (LL i = 0; i < q; ++i)
    {
        string s; LL x;
        cin >> s >> x;
        if ('i' == s[0])
        {
            LL answer = 0;
            if (0 == x)
            {
                answer = 1 - tree->query(0, 0);
                tree->update(0, 0, 1);
            }
            while (0 != x)
            {
                //cout << "test" << x << endl;
                if (x == ancestor[x])
                {
                    answer += (1 - tree->query(no[x], no[x]));
                    tree->update(no[x], no[x], 1);
                    x = father[x];
                }
                else
                {
                    //cout << no[x] << no[ancestor[x]] << endl;
                    answer += (no[x] - no[ancestor[x]] + 1 - tree->query(no[ancestor[x]], no[x]));
                    tree->update(no[ancestor[x]], no[x], 1);
                    x = father[ancestor[x]];
                }
            }
            cout << answer << endl;
        }
        else
        {
            LL answer = tree->query(no[x], en[x]);
            tree->update(no[x], en[x], -1);
            cout << answer << endl;
        }
    }
    return 0;
}

Details

answer.code: In function ‘int main()’:
answer.code:122:5: error: ‘memset’ was not declared in this scope
  122 |     memset(father,-1,sizeof(father));
      |     ^~~~~~
answer.code:3:1: note: ‘memset’ is defined in header ‘<cstring>’; did you forget to ‘#include <cstring>’?
    2 | #include <queue>
  +++ |+#include <cstring>
    3 | #include <vector>