답안 #991582

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
991582 2024-06-02T14:09:57 Z snpmrnhlol Sprinkler (JOI22_sprinkler) C++17
100 / 100
505 ms 64596 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int D = 45;
vector <int> e[N];
vector <int> path;
int pr[N];
int bonus[N][D];
int v[N];
void dfs(int node, int p){
    pr[node] = p;
    for(auto i:e[node]){
        if(i == p)continue;
        dfs(i, node);
    }

}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,mod;
    cin>>n>>mod;
    for(int i = 0;i < n - 1;i++){
        int u,w;
        cin>>u>>w;
        e[u - 1].push_back(w - 1);
        e[w - 1].push_back(u - 1);
    }
    for(int i = 0;i < n;i++){
        cin>>v[i];
        for(int j = 0;j <= D;j++){
            bonus[i][j] = 1;
        }
    }
    dfs(0, -1);
    int q;
    cin>>q;
    for(int i = 0;i < q;i++){
        int tip;
        cin>>tip;
        ///limit:40
        if(tip == 1){
            int x,d,w;
            cin>>x>>d>>w;
            x--;
            int cnt = 0;
            for(int j = x;j != -1 && cnt <= d;j = pr[j]){
                bonus[j][d - cnt] = 1ll*bonus[j][d - cnt]*w%mod;
                //cout<<"update:"<<j<<' '<<d - cnt<<' '<<w<<'\n';
                cnt++;
            }
        }else{
            int x;
            cin>>x;
            x--;
            int ans = v[x];
            for(int j = 0;j < D && x != -1;j++){
                if(x == 0){
                    for(int k = j;k < D;k++){
                        ans = 1ll*ans*bonus[x][k]%mod;
                    }
                }else{
                    ans = 1ll*ans*bonus[x][j]%mod;
                    if(j != D - 1){
                        ans = 1ll*ans*bonus[x][j + 1]%mod;
                    }
                }
                x = pr[x];
            }
            cout<<ans<<'\n';
        }
    }
    return 0;
}

Compilation message

sprinkler.cpp: In function 'int main()':
sprinkler.cpp:32:25: warning: iteration 45 invokes undefined behavior [-Waggressive-loop-optimizations]
   32 |             bonus[i][j] = 1;
      |             ~~~~~~~~~~~~^~~
sprinkler.cpp:31:25: note: within this loop
   31 |         for(int j = 0;j <= D;j++){
      |                       ~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8888 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 9048 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 9048 KB Output is correct
16 Correct 1 ms 8796 KB Output is correct
17 Correct 1 ms 8796 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 1 ms 8796 KB Output is correct
21 Correct 2 ms 8936 KB Output is correct
22 Correct 1 ms 8796 KB Output is correct
23 Correct 2 ms 8796 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8928 KB Output is correct
28 Correct 1 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 377 ms 51816 KB Output is correct
3 Correct 217 ms 48508 KB Output is correct
4 Correct 373 ms 53080 KB Output is correct
5 Correct 298 ms 50016 KB Output is correct
6 Correct 240 ms 49940 KB Output is correct
7 Correct 257 ms 50516 KB Output is correct
8 Correct 213 ms 50888 KB Output is correct
9 Correct 484 ms 56156 KB Output is correct
10 Correct 224 ms 52816 KB Output is correct
11 Correct 429 ms 51796 KB Output is correct
12 Correct 226 ms 48464 KB Output is correct
13 Correct 144 ms 49088 KB Output is correct
14 Correct 160 ms 49096 KB Output is correct
15 Correct 166 ms 49280 KB Output is correct
16 Correct 163 ms 49044 KB Output is correct
17 Correct 167 ms 49744 KB Output is correct
18 Correct 2 ms 8792 KB Output is correct
19 Correct 3 ms 8952 KB Output is correct
20 Correct 1 ms 8796 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 377 ms 51816 KB Output is correct
3 Correct 217 ms 48508 KB Output is correct
4 Correct 373 ms 53080 KB Output is correct
5 Correct 298 ms 50016 KB Output is correct
6 Correct 240 ms 49940 KB Output is correct
7 Correct 257 ms 50516 KB Output is correct
8 Correct 213 ms 50888 KB Output is correct
9 Correct 484 ms 56156 KB Output is correct
10 Correct 224 ms 52816 KB Output is correct
11 Correct 429 ms 51796 KB Output is correct
12 Correct 226 ms 48464 KB Output is correct
13 Correct 144 ms 49088 KB Output is correct
14 Correct 160 ms 49096 KB Output is correct
15 Correct 166 ms 49280 KB Output is correct
16 Correct 163 ms 49044 KB Output is correct
17 Correct 167 ms 49744 KB Output is correct
18 Correct 2 ms 8792 KB Output is correct
19 Correct 3 ms 8952 KB Output is correct
20 Correct 1 ms 8796 KB Output is correct
21 Correct 3 ms 8796 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 1 ms 8796 KB Output is correct
24 Correct 386 ms 51716 KB Output is correct
25 Correct 234 ms 48420 KB Output is correct
26 Correct 354 ms 54608 KB Output is correct
27 Correct 301 ms 50000 KB Output is correct
28 Correct 231 ms 50228 KB Output is correct
29 Correct 228 ms 50360 KB Output is correct
30 Correct 187 ms 50884 KB Output is correct
31 Correct 434 ms 54868 KB Output is correct
32 Correct 216 ms 52820 KB Output is correct
33 Correct 364 ms 51808 KB Output is correct
34 Correct 206 ms 48644 KB Output is correct
35 Correct 2 ms 8796 KB Output is correct
36 Correct 2 ms 8844 KB Output is correct
37 Correct 1 ms 8796 KB Output is correct
38 Correct 1 ms 8796 KB Output is correct
39 Correct 2 ms 8796 KB Output is correct
40 Correct 1 ms 8796 KB Output is correct
41 Correct 2 ms 8796 KB Output is correct
42 Correct 2 ms 8796 KB Output is correct
43 Correct 2 ms 8796 KB Output is correct
44 Correct 2 ms 8796 KB Output is correct
45 Correct 2 ms 8796 KB Output is correct
46 Correct 2 ms 8796 KB Output is correct
47 Correct 1 ms 8796 KB Output is correct
48 Correct 2 ms 8796 KB Output is correct
49 Correct 3 ms 9048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 434 ms 53800 KB Output is correct
3 Correct 418 ms 61268 KB Output is correct
4 Correct 395 ms 60756 KB Output is correct
5 Correct 341 ms 57172 KB Output is correct
6 Correct 231 ms 57348 KB Output is correct
7 Correct 232 ms 57480 KB Output is correct
8 Correct 191 ms 58056 KB Output is correct
9 Correct 459 ms 60180 KB Output is correct
10 Correct 429 ms 61836 KB Output is correct
11 Correct 395 ms 56752 KB Output is correct
12 Correct 350 ms 57704 KB Output is correct
13 Correct 235 ms 58740 KB Output is correct
14 Correct 222 ms 59164 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 1 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8792 KB Output is correct
2 Correct 446 ms 54608 KB Output is correct
3 Correct 400 ms 60512 KB Output is correct
4 Correct 369 ms 61560 KB Output is correct
5 Correct 313 ms 58708 KB Output is correct
6 Correct 241 ms 58964 KB Output is correct
7 Correct 251 ms 58704 KB Output is correct
8 Correct 194 ms 59076 KB Output is correct
9 Correct 468 ms 63784 KB Output is correct
10 Correct 401 ms 62296 KB Output is correct
11 Correct 368 ms 59476 KB Output is correct
12 Correct 356 ms 57984 KB Output is correct
13 Correct 215 ms 58704 KB Output is correct
14 Correct 221 ms 59304 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 2 ms 8888 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 9048 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 9048 KB Output is correct
16 Correct 1 ms 8796 KB Output is correct
17 Correct 1 ms 8796 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8792 KB Output is correct
20 Correct 1 ms 8796 KB Output is correct
21 Correct 2 ms 8936 KB Output is correct
22 Correct 1 ms 8796 KB Output is correct
23 Correct 2 ms 8796 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8928 KB Output is correct
28 Correct 1 ms 8796 KB Output is correct
29 Correct 1 ms 8796 KB Output is correct
30 Correct 377 ms 51816 KB Output is correct
31 Correct 217 ms 48508 KB Output is correct
32 Correct 373 ms 53080 KB Output is correct
33 Correct 298 ms 50016 KB Output is correct
34 Correct 240 ms 49940 KB Output is correct
35 Correct 257 ms 50516 KB Output is correct
36 Correct 213 ms 50888 KB Output is correct
37 Correct 484 ms 56156 KB Output is correct
38 Correct 224 ms 52816 KB Output is correct
39 Correct 429 ms 51796 KB Output is correct
40 Correct 226 ms 48464 KB Output is correct
41 Correct 144 ms 49088 KB Output is correct
42 Correct 160 ms 49096 KB Output is correct
43 Correct 166 ms 49280 KB Output is correct
44 Correct 163 ms 49044 KB Output is correct
45 Correct 167 ms 49744 KB Output is correct
46 Correct 2 ms 8792 KB Output is correct
47 Correct 3 ms 8952 KB Output is correct
48 Correct 1 ms 8796 KB Output is correct
49 Correct 3 ms 8796 KB Output is correct
50 Correct 2 ms 8796 KB Output is correct
51 Correct 1 ms 8796 KB Output is correct
52 Correct 386 ms 51716 KB Output is correct
53 Correct 234 ms 48420 KB Output is correct
54 Correct 354 ms 54608 KB Output is correct
55 Correct 301 ms 50000 KB Output is correct
56 Correct 231 ms 50228 KB Output is correct
57 Correct 228 ms 50360 KB Output is correct
58 Correct 187 ms 50884 KB Output is correct
59 Correct 434 ms 54868 KB Output is correct
60 Correct 216 ms 52820 KB Output is correct
61 Correct 364 ms 51808 KB Output is correct
62 Correct 206 ms 48644 KB Output is correct
63 Correct 2 ms 8796 KB Output is correct
64 Correct 2 ms 8844 KB Output is correct
65 Correct 1 ms 8796 KB Output is correct
66 Correct 1 ms 8796 KB Output is correct
67 Correct 2 ms 8796 KB Output is correct
68 Correct 1 ms 8796 KB Output is correct
69 Correct 2 ms 8796 KB Output is correct
70 Correct 2 ms 8796 KB Output is correct
71 Correct 2 ms 8796 KB Output is correct
72 Correct 2 ms 8796 KB Output is correct
73 Correct 2 ms 8796 KB Output is correct
74 Correct 2 ms 8796 KB Output is correct
75 Correct 1 ms 8796 KB Output is correct
76 Correct 2 ms 8796 KB Output is correct
77 Correct 3 ms 9048 KB Output is correct
78 Correct 1 ms 8796 KB Output is correct
79 Correct 434 ms 53800 KB Output is correct
80 Correct 418 ms 61268 KB Output is correct
81 Correct 395 ms 60756 KB Output is correct
82 Correct 341 ms 57172 KB Output is correct
83 Correct 231 ms 57348 KB Output is correct
84 Correct 232 ms 57480 KB Output is correct
85 Correct 191 ms 58056 KB Output is correct
86 Correct 459 ms 60180 KB Output is correct
87 Correct 429 ms 61836 KB Output is correct
88 Correct 395 ms 56752 KB Output is correct
89 Correct 350 ms 57704 KB Output is correct
90 Correct 235 ms 58740 KB Output is correct
91 Correct 222 ms 59164 KB Output is correct
92 Correct 2 ms 8796 KB Output is correct
93 Correct 2 ms 8796 KB Output is correct
94 Correct 2 ms 8796 KB Output is correct
95 Correct 2 ms 8796 KB Output is correct
96 Correct 1 ms 8796 KB Output is correct
97 Correct 1 ms 8792 KB Output is correct
98 Correct 446 ms 54608 KB Output is correct
99 Correct 400 ms 60512 KB Output is correct
100 Correct 369 ms 61560 KB Output is correct
101 Correct 313 ms 58708 KB Output is correct
102 Correct 241 ms 58964 KB Output is correct
103 Correct 251 ms 58704 KB Output is correct
104 Correct 194 ms 59076 KB Output is correct
105 Correct 468 ms 63784 KB Output is correct
106 Correct 401 ms 62296 KB Output is correct
107 Correct 368 ms 59476 KB Output is correct
108 Correct 356 ms 57984 KB Output is correct
109 Correct 215 ms 58704 KB Output is correct
110 Correct 221 ms 59304 KB Output is correct
111 Correct 2 ms 8796 KB Output is correct
112 Correct 2 ms 8796 KB Output is correct
113 Correct 2 ms 8796 KB Output is correct
114 Correct 2 ms 8796 KB Output is correct
115 Correct 2 ms 8796 KB Output is correct
116 Correct 376 ms 58000 KB Output is correct
117 Correct 395 ms 60644 KB Output is correct
118 Correct 392 ms 64596 KB Output is correct
119 Correct 322 ms 60244 KB Output is correct
120 Correct 252 ms 59752 KB Output is correct
121 Correct 249 ms 60756 KB Output is correct
122 Correct 207 ms 61012 KB Output is correct
123 Correct 505 ms 63680 KB Output is correct
124 Correct 468 ms 63072 KB Output is correct
125 Correct 391 ms 59204 KB Output is correct
126 Correct 388 ms 60500 KB Output is correct
127 Correct 377 ms 61160 KB Output is correct
128 Correct 248 ms 61728 KB Output is correct
129 Correct 277 ms 61992 KB Output is correct