QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#233169#7439. 铃原露露HaccerKat25 880ms117380kbC++208.0kb2023-10-31 14:34:572023-10-31 14:34:59

Judging History

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

  • [2023-10-31 14:34:59]
  • 评测
  • 测评结果:25
  • 用时:880ms
  • 内存:117380kb
  • [2023-10-31 14:34:57]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
template<typename T>
int SIZE(T (&t)){
    return t.size();
}

template<typename T, size_t N>
int SIZE(T (&t)[N]){
    return N;
}

string to_string(char t){
    return "'" + string({t}) + "'";
}

string to_string(bool t){
    return t ? "true" : "false";
}

string to_string(const string &t, int x1=0, int x2=1e9){
    string ret = "";
    for(int i = min(x1,SIZE(t)), _i = min(x2,SIZE(t)-1); i <= _i; ++i){
        ret += t[i];
    }
    return '"' + ret + '"';
}

string to_string(const char* t){
    string ret(t);
    return to_string(ret);
}

template<size_t N>
string to_string(const bitset<N> &t, int x1=0, int x2=1e9){
    string ret = "";
    for(int i = min(x1,SIZE(t)); i <= min(x2,SIZE(t)-1); ++i){
        ret += t[i] + '0';
    }
    return to_string(ret);
}

template<typename T, typename... Coords>
string to_string(const T (&t), int x1=0, int x2=1e9, Coords... C);

template<typename T, typename S>
string to_string(const pair<T, S> &t){
    return "(" + to_string(t.first) + ", " + to_string(t.second) + ")";
}

template<typename T, typename... Coords>
string to_string(const T (&t), int x1, int x2, Coords... C){
    string ret = "[";
    x1 = min(x1, SIZE(t));
    auto e = begin(t);
    advance(e,x1);
    for(int i = x1, _i = min(x2,SIZE(t)-1); i <= _i; ++i){
        ret += to_string(*e, C...) + (i != _i ? ", " : "");
        e = next(e);
    }
    return ret + "]";
}

template<int Index, typename... Ts>
struct print_tuple{
    string operator() (const tuple<Ts...>& t) {
        string ret = print_tuple<Index - 1, Ts...>{}(t);
        ret += (Index ? ", " : "");
        return ret + to_string(get<Index>(t));
    }
};

template<typename... Ts>
struct print_tuple<0, Ts...> {
    string operator() (const tuple<Ts...>& t) {
        return to_string(get<0>(t));
    }
};

template<typename... Ts>
string to_string(const tuple<Ts...>& t) {
    const auto Size = tuple_size<tuple<Ts...>>::value;
    return print_tuple<Size - 1, Ts...>{}(t);
}

void dbgr(){;}
template<typename Heads, typename... Tails>
void dbgr(Heads H, Tails... T){
    cout << to_string(H) << " | ";
    dbgr(T...);
}

void dbgs(){;}
template<typename Heads, typename... Tails>
void dbgs(Heads H, Tails... T){
    cout << H << " ";
    dbgs(T...);
}

/*
formatted functions:
*/

/*
consider __VA_ARGS__ as a whole:
dbgv() prints values only
dbg() prints name and values
*/
#define dbgv(...) cout << to_string(__VA_ARGS__) << endl;

#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgv(__VA_ARGS__);
//#define dbg(...)

/*
consider __VA_ARGS__ as a sequence of arguments:
dbgr() prints values only
dbgm() prints names and values
*/
#define dbgr(...) dbgr(__VA_ARGS__); cout << endl;

#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgr(__VA_ARGS__);

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
// using u128 = __uint128_t;
// using i128 = __int128;
const int mod = 1000000007;
const int N = 200005;
const int LOG = 20;
const int inf = 1e9;
const double eps = 1e-11;
int n, m, k, qq;
int a[N], f[N];
vector<int> adj[N];
vector<array<int, 3>> sweep[N];
vector<array<int, 4>> sweepq[N];
ll out[N];
set<int> s[N];
void addrect(int z, int x, int y) {
    if (x > y) swap(x, y);
    if (x < z && y < z) {
        // dbgm(z, x, y, 0);
        sweep[0].push_back({y, z - 1, 1});
        sweep[x + 1].push_back({y, z - 1, -1});
    }
    
    if (x > z && y > z) {
        // dbgm(z, x, y, 1);
        sweep[z + 1].push_back({y, n - 1, 1});
        sweep[x + 1].push_back({y, n - 1, -1});
    }
}

void dfs(int u) {
    for (int &v : adj[u]) {
        dfs(v);
        if (s[v].size() > s[adj[u][0]].size()) {
            swap(v, adj[u][0]);
        }
    }
    
    if (!adj[u].empty()) swap(s[u], s[adj[u][0]]);
    for (int &v : adj[u]) {
        if (v == adj[u][0]) continue;
        for (int x : s[v]) {
            auto it = s[u].upper_bound(x);
            if (it != s[u].end()) {
                int y = *it;
                addrect(a[u], x, y);
            }
            
            if (it != s[u].begin()) {
                it--;
                int y = *it;
                addrect(a[u], x, y);
            } 
        }
        
        for (int x : s[v]) {
            s[u].insert(x);
        }
    }
    
    s[u].insert(a[u]);
}

struct Node {
    int mn = 0, cnt = 1, lazy = 0, lazyadd = 0;
    ll sum = 0;  
};

Node t[N * 4];
const int dummy = N * 4 - 1;
void push(int u, int v) {
    t[v].mn += t[u].lazyadd, t[v].lazyadd += t[u].lazyadd;
    if (t[u].mn == t[v].mn) {
        t[v].sum += (ll)t[v].cnt * t[u].lazy, t[v].lazy += t[u].lazy;
    }
}

void prop(int v) {
    push(v, v * 2);
    push(v, v * 2 + 1);
    t[v].lazy = 0, t[v].lazyadd = 0;
}

void comb(int v) {
    t[v].sum = t[v * 2].sum + t[v * 2 + 1].sum;
    t[v].mn = t[v * 2].mn, t[v].cnt = t[v * 2].cnt;
    if (t[v * 2 + 1].mn < t[v].mn) {
        t[v].mn = t[v * 2 + 1].mn, t[v].cnt = t[v * 2 + 1].cnt;
    }
    
    if (t[v * 2 + 1].mn == t[v].mn) t[v].cnt += t[v * 2 + 1].cnt;
}

void build(int v = 1, int tl = 0, int tr = n - 1) {
    if (tl == tr) return;
    int mid = (tl + tr) / 2;
    build(v * 2, tl, mid);
    build(v * 2 + 1, mid + 1, tr);
    comb(v);
}

void update(int ql, int qr, int x, int v = 1, int tl = 0, int tr = n - 1) {
    if (tl > qr || tr < ql) return;
    if (tl >= ql && tr <= qr) {
        t[dummy].lazyadd = x;
        push(dummy, v);
        return;
    }
    
    prop(v);
    int mid = (tl + tr) / 2;
    update(ql, qr, x, v * 2, tl, mid);
    update(ql, qr, x, v * 2 + 1, mid + 1, tr);
    comb(v);
}

ll query(int ql, int qr, int v = 1, int tl = 0, int tr = n - 1) {
    if (tl > qr || tr < ql) return 0;
    if (tl >= ql && tr <= qr) return t[v].sum;
    prop(v);
    int mid = (tl + tr) / 2;
    ll L = query(ql, qr, v * 2, tl, mid);
    ll R = query(ql, qr, v * 2 + 1, mid + 1, tr);
    return L + R;
}

void solve() {
    t[dummy].lazy = 1;
    cin >> n >> qq;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    
    for (int i = 1; i < n; i++) {
        cin >> f[i];
        f[i]--;
        adj[f[i]].push_back(i);
    }
    
    // dbg(adj);
    build();
    dfs(0);
    for (int i = 0; i < qq; i++) {
        int l, r;
        cin >> l >> r;
        l--, r--;
        int sz = r - l + 1;
        out[i] = (ll)sz * (1 - sz) / 2;
        if (l != 0) sweepq[l - 1].push_back({l, r, -1, i});
        sweepq[r].push_back({l, r, 1, i});
    }
    
    for (int i = 0; i < n; i++) {
        vector<pi> pos;
        for (auto [l, r, x] : sweep[i]) {
            pos.push_back({l, x});
            pos.push_back({r + 1, -x});
        }
        
        sort(pos.begin(), pos.end());
        pos.push_back({n, 0});
        // dbg(pos);
        int pre = 0, cur = 0;
        for (auto [p, x] : pos) {
            if (pre != p) {
                // dbgm(pre, p - 1, cur, i);
                update(pre, p - 1, cur);
                pre = p;
            }
            
            cur += x;
        }
        
        for (auto [l, r, x, j] : sweepq[i]) {
            // dbgm(query(l, r), x);
            out[j] += query(l, r) * x;
        }
    }
    
    for (int i = 0; i < qq; i++) {
        cout << out[i] << "\n";
    }
}

int32_t main() {
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 28536kb

input:

100 100
5 29 12 16 25 36 18 37 27 47 34 40 20 3 1 42 26 19 33 41 6 22 8 58 32 62 24 15 35 17 59 30 50 61 43 49 39 67 44 21 13 31 68 69 65 64 10 28 38 54 70 63 9 46 66 52 23 7 48 60 55 56 51 2 57 11 53 14 45 4 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 99 100
...

output:

9
87
37
4
13
13
48
17
175
32
46
29
66
40
23
28
43
97
34
37
38
39
85
28
8
64
37
67
1
42
104
7
43
38
4
53
8
14
86
16
61
36
78
7
45
74
84
17
4
51
74
312
80
26
6
56
27
4
83
11
20
39
8
194
78
67
75
66
51
16
2
14
6
47
80
47
11
85
61
59
99
81
88
9
1
43
92
7
22
55
86
56
32
17
27
83
70
10
24
26

result:

wrong answer 18th numbers differ - expected: '113', found: '97'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 25
Accepted

Test #21:

score: 25
Accepted
time: 843ms
memory: 117136kb

input:

200000 1
73119 155820 110077 139724 136809 18709 57745 43535 89117 43647 20295 60551 108184 188031 180437 52363 72969 130559 179796 75852 53879 96998 63387 76458 193661 142318 28260 40465 80050 188507 143795 141018 94880 71333 7644 109237 105208 109509 9779 159914 135096 47638 175577 182927 173100 1...

output:

216932

result:

ok 1 number(s): "216932"

Test #22:

score: 0
Accepted
time: 469ms
memory: 111536kb

input:

200000 1
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 69 130 61 135 87 128 73 64 63 77 62 102 139 98 88 132 95 93 80 78 86 83 72 107 101 138 89 124 76 65 120 108 67 110 79 60 82 92...

output:

87087190

result:

ok 1 number(s): "87087190"

Test #23:

score: 0
Accepted
time: 249ms
memory: 92520kb

input:

200000 1
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 99 100...

output:

2076001703

result:

ok 1 number(s): "2076001703"

Test #24:

score: 0
Accepted
time: 433ms
memory: 107268kb

input:

200000 1
47227 15222 1 601 13189 48081 179711 183599 15223 34950 38918 47228 183600 15224 602 66302 48290 15225 25808 194847 603 604 191310 26619 1705 15226 191431 52924 48260 197639 56975 66099 16584 15874 17996 18105 183601 54245 20399 34983 27509 66303 18106 191432 52925 56976 191433 194848 19764...

output:

2023389577

result:

ok 1 number(s): "2023389577"

Test #25:

score: 0
Accepted
time: 282ms
memory: 94544kb

input:

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

output:

2417178387

result:

ok 1 number(s): "2417178387"

Test #26:

score: 0
Accepted
time: 631ms
memory: 102000kb

input:

200000 1
1 2 3 4 5 6 7 19 56 43 58 30 54 42 16 13 38 27 12 31 44 46 36 29 28 26 37 59 41 53 50 35 11 17 52 40 48 34 60 23 9 51 55 49 32 33 66 20 47 67 68 24 71 86 80 64 39 78 75 10 62 83 94 73 91 97 101 85 138 93 22 98 104 99 14 18 109 102 116 107 8 79 69 25 74 110 82 65 72 100 88 45 120 95 131 108 ...

output:

554647120

result:

ok 1 number(s): "554647120"

Test #27:

score: 0
Accepted
time: 289ms
memory: 91980kb

input:

200000 1
1 2 3 74880 4 49878 5 28854 21274 5791 7 6 116048 116047 116046 37131 37129 37130 25975 25974 99094 99095 99096 37132 8 47470 183292 183293 9 10 74521 74522 25976 116049 47471 47472 47473 47474 47475 47476 47477 47478 47483 47479 47482 47480 47481 74524 74523 116050 37135 37134 37133 74525 ...

output:

872886033

result:

ok 1 number(s): "872886033"

Test #28:

score: 0
Accepted
time: 866ms
memory: 117380kb

input:

200000 1
1 6 2 3 5 8 10 4 7 9 11 12 13 14 15 16 17 22 18 19 21 20 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 50 51 48 49 55 52 53 54 56 57 58 59 60 64 65 63 62 61 67 70 66 68 69 71 75 73 76 74 72 78 80 77 79 84 83 81 82 85 86 87 90 88 89 97 91 93 92 94 95 102 98 96 10...

output:

2222736478

result:

ok 1 number(s): "2222736478"

Test #29:

score: 0
Accepted
time: 880ms
memory: 116604kb

input:

200000 1
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 38 61 78 49 104 93 103 53 95 87 60 43 44 35 50 45 89 46 72 67 63 56 77 102 86 79 41 70 59 96 88 54 57 69 51 98 55 91 84 68 42 80 73 66 81 48 90 71 47 37 94 40 83 65 100 75 99 39 85 76 36 52 97 74 92...

output:

2222744424

result:

ok 1 number(s): "2222744424"

Test #30:

score: 0
Accepted
time: 331ms
memory: 93708kb

input:

200000 1
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 99 100...

output:

555293737

result:

ok 1 number(s): "555293737"

Subtask #4:

score: 0
Skipped

Dependency #1:

0%