QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#233154#7439. 铃原露露HaccerKat0 872ms118404kbC++207.8kb2023-10-31 14:13:152023-10-31 14:13:16

Judging History

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

  • [2023-10-31 14:13:16]
  • 评测
  • 测评结果:0
  • 用时:872ms
  • 内存:118404kb
  • [2023-10-31 14:13:15]
  • 提交

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) {
        sweep[0].push_back({y, z - 1, 1});
        sweep[x + 1].push_back({y, z - 1, -1});
    }
    
    if (x > z && y > z) {
        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;
}

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);
    }
    
    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});
        int pre = 0, cur = 0;
        for (auto [p, x] : pos) {
            if (pre != p) {
                update(pre, p - 1, cur);
                pre = p;
            }
            
            cur += x;
        }
        
        for (auto [l, r, x, j] : sweepq[i]) {
            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: 29800kb

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:

-36
-2628
-528
-6
-78
-66
-903
-105
-1908
-276
-820
-325
-1485
-630
-190
-190
-561
-2195
-435
-406
-460
-465
-2485
-190
-21
-1275
-465
-1540
0
-703
-3741
-6
-561
-595
-6
-812
-10
-91
-2485
-105
-1225
-496
-2016
-15
-820
-1770
-2415
-120
-6
-820
-1711
-3880
-2145
-153
-15
-903
-325
-3
-2346
-21
-171
...

result:

wrong answer 1st numbers differ - expected: '9', found: '-36'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 872ms
memory: 118404kb

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:

-19999897291

result:

wrong answer 1st numbers differ - expected: '216932', found: '-19999897291'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%