답안 #1003676

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003676 2024-06-20T15:04:48 Z onbert Sprinkler (JOI22_sprinkler) C++17
41 / 100
4000 ms 178780 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 5, maxN = 8e5 + 5, m = 43;
int n, M;
vector<int> adj[maxn];
int newid[maxn], d[maxn], fa[maxn], a[maxn];
bool vis[maxn];

pair<int,int> range[maxn][m];
void dfs(int u) {
    range[u][0] = {newid[u], newid[u]};
    if (adj[u].size() == 0) return;
    for (int v:adj[u]) dfs(v);
    int sz = adj[u].size();
    int l = 0, r = sz - 1;
    for (int i=1;i<m;i++) {
        while (l < sz && range[adj[u][l]][i-1].first==-1) l++;
        while (r >= 0 && range[adj[u][r]][i-1].first==-1) r--;
        if (l>r) break;
        range[u][i] = {range[adj[u][l]][i-1].first, range[adj[u][r]][i-1].second};
    }
}

int seg[maxN];
void build(int id, int l, int r) {
    seg[id] = 1;
    if (l==r) {
        seg[id] = a[l];
        return;
    }
    int mid = (l+r)/2;
    build(id*2, l, mid); build(id*2+1, mid+1, r);
}
void update(int id, int l, int r, int findl, int findr, int val) {
    if (r<findl || findr<l) return;
    if (findl<=l && r<=findr) {
        seg[id] = seg[id] * val % M;
        return;
    }
    int mid = (l+r)/2;
    update(id*2, l, mid, findl, findr, val);
    update(id*2+1, mid+1, r, findl, findr, val);
}
int qry(int id, int l, int r, int target) {
    if (r<target || target<l) return 1;
    if (l==r) return seg[id];
    int mid = (l+r)/2;
    return (seg[id] * qry(id*2, l, mid, target) % M) * qry(id*2+1, mid+1, r, target) % M;
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> M;
    for (int i=1;i<=n-1;i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    int A[n+1];
    for (int i=1;i<=n;i++) cin >> A[i];
    queue<int> Q;
    Q.push(1);
    vis[1] = true, d[1] = 0, fa[1] = -1;
    int cnter = 0;
    while (Q.size() > 0) {
        int u = Q.front();
        cnter++;
        newid[u] = cnter;
        for (int v:adj[u]) if (!vis[v]) {
            vis[v] = true, fa[v] = u, d[v] = d[u] + 1;
            adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
            Q.push(v);
        }
        Q.pop();
    }
    for (int i=1;i<=n;i++) for (int j=0;j<m;j++) range[i][j] = {-1, -1};
    dfs(1);
    for (int i=1;i<=n;i++) a[newid[i]] = A[i];
    build(1, 1, n);
    int q;
    cin >> q;
    while (q--) {
        int t;
        cin >> t;
        if (t==1) {
            int x, D, w;
            cin >> x >> D >> w;
            int v = x;
            for (int dep = d[x]+D; dep>=d[x]-D && dep >= 0; dep--) {
                if (fa[v]!=-1 && dep-d[v] + d[x]-d[v] + 2 <= D) v = fa[v];
                update(1, 1, n, range[v][dep-d[v]].first, range[v][dep-d[v]].second, w);
            }
        } else if (t==2) {
            int x;
            cin >> x;
            cout << qry(1, 1, n, newid[x]) << endl;
        }
        // for (int i=1;i<=n;i++) cout << qry(1, 1, n, newid[i]) << " "; cout << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5168 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 5 ms 5980 KB Output is correct
5 Correct 4 ms 5980 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5180 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 4 ms 5208 KB Output is correct
15 Correct 4 ms 5176 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5120 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5208 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5224 KB Output is correct
27 Correct 3 ms 5208 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 695 ms 170980 KB Output is correct
3 Correct 524 ms 171344 KB Output is correct
4 Correct 624 ms 175968 KB Output is correct
5 Correct 545 ms 171088 KB Output is correct
6 Correct 577 ms 171092 KB Output is correct
7 Correct 561 ms 171856 KB Output is correct
8 Correct 498 ms 173504 KB Output is correct
9 Correct 727 ms 178768 KB Output is correct
10 Correct 604 ms 178512 KB Output is correct
11 Correct 699 ms 170808 KB Output is correct
12 Correct 471 ms 171448 KB Output is correct
13 Correct 338 ms 172992 KB Output is correct
14 Correct 321 ms 172916 KB Output is correct
15 Correct 373 ms 171700 KB Output is correct
16 Correct 322 ms 172112 KB Output is correct
17 Correct 356 ms 172624 KB Output is correct
18 Correct 2 ms 5208 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 695 ms 170980 KB Output is correct
3 Correct 524 ms 171344 KB Output is correct
4 Correct 624 ms 175968 KB Output is correct
5 Correct 545 ms 171088 KB Output is correct
6 Correct 577 ms 171092 KB Output is correct
7 Correct 561 ms 171856 KB Output is correct
8 Correct 498 ms 173504 KB Output is correct
9 Correct 727 ms 178768 KB Output is correct
10 Correct 604 ms 178512 KB Output is correct
11 Correct 699 ms 170808 KB Output is correct
12 Correct 471 ms 171448 KB Output is correct
13 Correct 338 ms 172992 KB Output is correct
14 Correct 321 ms 172916 KB Output is correct
15 Correct 373 ms 171700 KB Output is correct
16 Correct 322 ms 172112 KB Output is correct
17 Correct 356 ms 172624 KB Output is correct
18 Correct 2 ms 5208 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5140 KB Output is correct
23 Correct 3 ms 4956 KB Output is correct
24 Correct 709 ms 170924 KB Output is correct
25 Correct 591 ms 171544 KB Output is correct
26 Correct 695 ms 178780 KB Output is correct
27 Correct 605 ms 171052 KB Output is correct
28 Correct 527 ms 171572 KB Output is correct
29 Correct 549 ms 161724 KB Output is correct
30 Correct 504 ms 171348 KB Output is correct
31 Correct 736 ms 175184 KB Output is correct
32 Correct 686 ms 176212 KB Output is correct
33 Correct 631 ms 165792 KB Output is correct
34 Correct 565 ms 160012 KB Output is correct
35 Correct 3 ms 5208 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 5212 KB Output is correct
38 Correct 3 ms 5212 KB Output is correct
39 Correct 3 ms 5124 KB Output is correct
40 Correct 3 ms 4956 KB Output is correct
41 Correct 3 ms 4956 KB Output is correct
42 Correct 3 ms 4956 KB Output is correct
43 Correct 2 ms 5100 KB Output is correct
44 Correct 3 ms 5208 KB Output is correct
45 Correct 3 ms 4956 KB Output is correct
46 Correct 3 ms 5212 KB Output is correct
47 Correct 2 ms 5212 KB Output is correct
48 Correct 3 ms 5212 KB Output is correct
49 Correct 3 ms 5208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 1312 ms 168116 KB Output is correct
3 Execution timed out 4048 ms 173396 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 1272 ms 171576 KB Output is correct
3 Execution timed out 4024 ms 169108 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5168 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 5 ms 5980 KB Output is correct
5 Correct 4 ms 5980 KB Output is correct
6 Correct 3 ms 5980 KB Output is correct
7 Correct 3 ms 5980 KB Output is correct
8 Correct 3 ms 5980 KB Output is correct
9 Correct 3 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5180 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 4 ms 5208 KB Output is correct
15 Correct 4 ms 5176 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5120 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5208 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5224 KB Output is correct
27 Correct 3 ms 5208 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 2 ms 4956 KB Output is correct
30 Correct 695 ms 170980 KB Output is correct
31 Correct 524 ms 171344 KB Output is correct
32 Correct 624 ms 175968 KB Output is correct
33 Correct 545 ms 171088 KB Output is correct
34 Correct 577 ms 171092 KB Output is correct
35 Correct 561 ms 171856 KB Output is correct
36 Correct 498 ms 173504 KB Output is correct
37 Correct 727 ms 178768 KB Output is correct
38 Correct 604 ms 178512 KB Output is correct
39 Correct 699 ms 170808 KB Output is correct
40 Correct 471 ms 171448 KB Output is correct
41 Correct 338 ms 172992 KB Output is correct
42 Correct 321 ms 172916 KB Output is correct
43 Correct 373 ms 171700 KB Output is correct
44 Correct 322 ms 172112 KB Output is correct
45 Correct 356 ms 172624 KB Output is correct
46 Correct 2 ms 5208 KB Output is correct
47 Correct 3 ms 5212 KB Output is correct
48 Correct 3 ms 5212 KB Output is correct
49 Correct 3 ms 5212 KB Output is correct
50 Correct 3 ms 5140 KB Output is correct
51 Correct 3 ms 4956 KB Output is correct
52 Correct 709 ms 170924 KB Output is correct
53 Correct 591 ms 171544 KB Output is correct
54 Correct 695 ms 178780 KB Output is correct
55 Correct 605 ms 171052 KB Output is correct
56 Correct 527 ms 171572 KB Output is correct
57 Correct 549 ms 161724 KB Output is correct
58 Correct 504 ms 171348 KB Output is correct
59 Correct 736 ms 175184 KB Output is correct
60 Correct 686 ms 176212 KB Output is correct
61 Correct 631 ms 165792 KB Output is correct
62 Correct 565 ms 160012 KB Output is correct
63 Correct 3 ms 5208 KB Output is correct
64 Correct 2 ms 5212 KB Output is correct
65 Correct 2 ms 5212 KB Output is correct
66 Correct 3 ms 5212 KB Output is correct
67 Correct 3 ms 5124 KB Output is correct
68 Correct 3 ms 4956 KB Output is correct
69 Correct 3 ms 4956 KB Output is correct
70 Correct 3 ms 4956 KB Output is correct
71 Correct 2 ms 5100 KB Output is correct
72 Correct 3 ms 5208 KB Output is correct
73 Correct 3 ms 4956 KB Output is correct
74 Correct 3 ms 5212 KB Output is correct
75 Correct 2 ms 5212 KB Output is correct
76 Correct 3 ms 5212 KB Output is correct
77 Correct 3 ms 5208 KB Output is correct
78 Correct 2 ms 4956 KB Output is correct
79 Correct 1312 ms 168116 KB Output is correct
80 Execution timed out 4048 ms 173396 KB Time limit exceeded
81 Halted 0 ms 0 KB -