QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#233907#7439. 铃原露露HaccerKat0 1ms3684kbC++206.0kb2023-11-01 08:04:172023-11-01 08:04:18

Judging History

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

  • [2023-11-01 08:04:18]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3684kb
  • [2023-11-01 08:04:17]
  • 提交

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 = 105;
const int LOG = 20;
const int inf = 1e9;
const double eps = 1e-11;
int n, m, k, qq;
int a[N], f[N], p[N];
vector<int> adj[N];
int dep[N], tin[N], logA[N * 2];
int rmq[N * 2][LOG];
bool vis[N];
vector<int> euler;
int timer = 0;
void dfs(int u) {
    vis[u] = true, tin[u] = timer++;
    euler.push_back(u);
    for (int v : adj[u]) {
        if (!vis[v]) {
            dep[v] = dep[u] + 1;
            dfs(v);
            euler.push_back(u);
            timer++;
        }
    }
}

void precomp() {
    int sz = euler.size();
    for (int i = 2; i <= sz; i++) logA[i] = logA[i >> 1] + 1;
    for (int i = 0; i < sz; i++) {
        rmq[i][0] = euler[i];
    }
    
    for (int j = 1; j < LOG; j++) {
        for (int i = 0; i + (1 << j) <= sz; i++) {
            int x = (1 << j - 1);
            int lx = dep[rmq[i][j - 1]], rx = dep[rmq[i + x][j - 1]];
            rmq[i][j] = (lx < rx ? rmq[i][j - 1] : rmq[i + x][j - 1]);
        }
    }
}

int getlca(int u, int v) {
    int l = tin[u], r = tin[v];
    if (l > r) swap(l, r);
    int len = r - l + 1;
    int lg = logA[len], x = 1 << lg;
    int lx = dep[rmq[l][lg]], rx = dep[rmq[r - x + 1][lg]];
    return (lx < rx ? rmq[l][lg] : rmq[r - x + 1][lg]);
}

int getdist(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[getlca(u, v)];
}

bool ok[N][N];
void solve() {
    cin >> n >> qq;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        p[a[i]] = i;
    }
    
    for (int i = 1; i < n; i++) {
        cin >> f[i];
        f[i]--;
        adj[f[i]].push_back(i);
    }
    
    dfs(0);
    precomp();
    while (qq--) {
        int l, r;
        cin >> l >> r;
        l--, r--;
        for (int i = l; i <= r; i++) {
            for (int j = i; j <= r; j++) {
                int lca = getlca(p[i], p[j]);
                ok[i][j] = (a[lca] >= l && a[lca] <= r);
            }
        }
        
        for (int sz = 1; sz <= r - l; sz++) {
            for (int i = l; i <= r - sz; i++) {
                int j = i + sz;
                ok[i][j] &= ok[i + 1][j] & ok[i][j - 1];
            }
        }
        
        ll out = 0;
        for (int i = l; i <= r; i++) {
            for (int j = i; j <= r; j++) {
                out += ok[i][j];
            }
        }
        
        cout << out << "\n";
    }
}

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

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3684kb

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
193
37
4
13
13
120
17
2080
33
121
29
68
53
27
28
44
2278
40
110
200
40
197
28
8
157
49
69
1
42
221
7
44
38
4
677
8
14
184
16
63
94
174
7
115
169
185
17
4
52
169
4186
177
26
6
143
27
4
184
11
20
39
8
3482
178
69
170
68
52
16
2
17
6
48
82
47
11
1378
1035
152
219
181
202
9
1
117
208
8
42
145
188
56
3...

result:

wrong answer 2nd numbers differ - expected: '87', found: '193'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

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:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%