QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#552006#6805. Data StructureToboWA 12219ms410932kbC++203.6kb2024-09-07 19:43:342024-09-07 19:43:34

Judging History

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

  • [2024-09-07 19:43:34]
  • 评测
  • 测评结果:WA
  • 用时:12219ms
  • 内存:410932kb
  • [2024-09-07 19:43:34]
  • 提交

answer

#include <bits/stdc++.h>
using i64 = long long;
using namespace std;

const int N = 3e5 + 1, B = 501;
int n, m, dep[N], in[N], out[N], dfs_tot;
vector<int> adj[N];
struct ask
{
    int opt, a, x, y, z;
} b[N];
void dfs(int cur, int fa)
{
    dep[cur] = dep[fa] + 1;
    in[cur] = ++dfs_tot;
    for (int i : adj[cur])
        dfs(i, cur);
    out[cur] = dfs_tot;
}

int bfs_order[N], bfs_tot, tim[N];
vector<pair<int, int>> node[N];
void bfs()
{
    queue<int> que;
    que.push(1);
    bfs_tot = bfs_order[1] = tim[1] = 1;
    while (!que.empty())
    {
        int cur = que.front();
        que.pop();
        for (int i : adj[cur])
        {
            bfs_order[i] = ++bfs_tot;
            tim[bfs_tot] = i;
            que.push(i);
        }
    }
}

struct BIT
{
    int bit[N];
    int lowbit(int x)
    {
        return x & -x;
    }
    void add(int x, int y)
    {
        while (x <= n)
        {
            bit[x] += y;
            x += lowbit(x);
        }
    }
    int query(int x)
    {
        int res = 0;
        while (x)
        {
            res += bit[x];
            x -= lowbit(x);
        }
        return res;
    }
} tr[B];

int ans[N];
void solve()
{
    cin >> n >> m;
    for (int i = 2, x; i <= n; i++)
    {
        cin >> x;
        adj[x].push_back(i);
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> b[i].opt >> b[i].a;
        if (b[i].opt == 1)
            cin >> b[i].x >> b[i].y >> b[i].z;
    }
    dfs(1, 0);
    for (int c = 1; c <= 500; c++)
    {
        for (int i = 1; i <= m; i++)
        {
            auto &[opt, a, x, y, z] = b[i];
            if (opt == 1 and x == c)
                tr[(dep[a] + y) % c].add(in[a], z), tr[(dep[a] + y) % c].add(out[a] + 1, -z);
            else if (opt == 2)
                ans[i] += tr[dep[a] % c].query(in[a]);
        }
        for (int i = 1; i <= m; i++)
        {
            auto &[opt, a, x, y, z] = b[i];
            if (opt == 1 and x == c)
                tr[(dep[a] + y) % c].add(in[a], -z), tr[(dep[a] + y) % c].add(out[a] + 1, z);
        }
    }

    bfs();
    for (int i = 1; i <= n; i++)
        node[dep[i]].push_back({in[i], bfs_order[i]});
    for (int i = 1; i <= n; i++)
    {
        auto &t = node[i];
        sort(t.begin(), t.end());
    }
    for (int i = 1; i <= m; i++)
    {
        auto &[opt, a, x, y, z] = b[i];
        if (opt == 1 and x <= 500)
            continue;
        if (opt == 1)
        {
            int cur = dep[a] + y;
            while (cur <= n)
            {
                auto &t = node[cur];
                auto it1 = lower_bound(t.begin(), t.end(), pair<int, int>{in[a], 0});
                auto it2 = lower_bound(t.begin(), t.end(), pair<int, int>{out[a] + 1, 0});
                if (it1 != it2)
                {
                    int l = it1->second, r = l + (it2 - it1) - 1;
                    tr[0].add(l, z), tr[0].add(r + 1, -z);
                }
                cur += x;
            }
        }
        else
            ans[i] += tr[0].query(tim[a]);
    }

    for (int i = 1; i <= m; i++)
        if (b[i].opt == 2)
            cout << ans[i] << '\n';

    memset(tr[0].bit, 0, sizeof(tr[0].bit));
    memset(ans, 0, sizeof(ans));
    for (int i = 1; i <= n; i++)
    {
        adj[i].clear();
        node[i].clear();
    }
    bfs_tot = dfs_tot = 0;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
        solve();
}
/*
1
5 5
1 1 2 1
1 1 5 4 1
1 1 4 1 5
1 2 1 0 4
2 3
2 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 12168kb

input:

1
5 5
1 1 2 1
1 1 5 4 1
1 1 4 1 5
1 2 1 0 4
2 3
2 1

output:

5
0

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 12219ms
memory: 410932kb

input:

4
300000 300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98...

output:

1130
1509
2343
2426
4524
5096
8031
10312
15081
15081
0
21803
28276
507
29392
31408
31656
36062
507
37628
40202
45396
47064
48157
48157
50507
50866
60827
63565
0
398
71612
398
73533
76075
76524
398
78559
81801
1205
94975
1205
104994
106433
109726
110163
115355
116799
118169
119008
119008
398
120377
1...

result:

wrong answer 1612th lines differ - expected: '29187', found: '575647'