Submission #749225

# Submission time Handle Problem Language Result Execution time Memory
749225 2023-05-27T14:57:22 Z GrindMachine Sprinkler (JOI22_sprinkler) C++17
100 / 100
1269 ms 102092 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
https://codeforces.com/blog/entry/101003?#comment-897731

*/

int MOD = 1e9 + 7;
const int N = 2e5 + 50 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
const int D = 40 + 5;

vector<ll> adj[N];
vector<ll> par(N);

void dfs(ll u, ll p){
    par[u] = p;
    trav(v, adj[u]){
        if(v == p) conts;
        dfs(v,u);
    }
}

ll mul[N][D];

void solve(int test_case)
{
    ll n; cin >> n >> MOD;
    rep1(i,n-1){
        ll u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    vector<ll> a(n+5);
    rep1(i,n) cin >> a[i];

    // add 40 extra nodes before root so corner cases need not be handled
    for(int i = n + 1; i <= n + 39; ++i){
        adj[i].pb(i+1);
    }
    adj[n+40].pb(1);

    dfs(n+1,-1);

    rep(i,N){
        rep(j,D){
            mul[i][j] = 1;
        }
    }

    ll q; cin >> q;

    while(q--){
        ll t; cin >> t;
        if(t == 1){
            ll u,d,w; cin >> u >> d >> w;
            vector<ll> order;
            rep(i,d+1){
                order.pb(u);
                u = par[u];
            }

            reverse(all(order));
            mul[order[0]][0] = (mul[order[0]][0] * w) % MOD;

            rep1(j,sz(order)-1){
                ll v = order[j];
                mul[v][j-1] = (mul[v][j-1] * w) % MOD;
                mul[v][j] = (mul[v][j] * w) % MOD;
            }
        }
        else{
            ll u; cin >> u;
            ll ans = a[u];

            for(ll i = 0; i <= 40; ++i, u = par[u]){
                ans = ans * mul[u][i] % MOD;
            }

            cout << ans << endl;
        }

    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}

Compilation message

sprinkler.cpp: In function 'void solve(int)':
sprinkler.cpp:31:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 | #define rep1(i, n) for(int i = 1; i <= n; ++i)
......
  117 |             rep1(j,sz(order)-1){
      |                  ~~~~~~~~~~~~~       
sprinkler.cpp:117:13: note: in expansion of macro 'rep1'
  117 |             rep1(j,sz(order)-1){
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 36 ms 77004 KB Output is correct
2 Correct 35 ms 76992 KB Output is correct
3 Correct 35 ms 76976 KB Output is correct
4 Correct 36 ms 77028 KB Output is correct
5 Correct 36 ms 77068 KB Output is correct
6 Correct 35 ms 77132 KB Output is correct
7 Correct 36 ms 77136 KB Output is correct
8 Correct 36 ms 77008 KB Output is correct
9 Correct 35 ms 77132 KB Output is correct
10 Correct 35 ms 77004 KB Output is correct
11 Correct 35 ms 77000 KB Output is correct
12 Correct 36 ms 76968 KB Output is correct
13 Correct 38 ms 76952 KB Output is correct
14 Correct 33 ms 77004 KB Output is correct
15 Correct 33 ms 77004 KB Output is correct
16 Correct 36 ms 77028 KB Output is correct
17 Correct 38 ms 77008 KB Output is correct
18 Correct 36 ms 77000 KB Output is correct
19 Correct 35 ms 76944 KB Output is correct
20 Correct 36 ms 76976 KB Output is correct
21 Correct 35 ms 77132 KB Output is correct
22 Correct 38 ms 76944 KB Output is correct
23 Correct 38 ms 77000 KB Output is correct
24 Correct 35 ms 76948 KB Output is correct
25 Correct 35 ms 76940 KB Output is correct
26 Correct 35 ms 77132 KB Output is correct
27 Correct 36 ms 77052 KB Output is correct
28 Correct 36 ms 77004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 77024 KB Output is correct
2 Correct 576 ms 97736 KB Output is correct
3 Correct 403 ms 98480 KB Output is correct
4 Correct 480 ms 100308 KB Output is correct
5 Correct 467 ms 98012 KB Output is correct
6 Correct 406 ms 97528 KB Output is correct
7 Correct 363 ms 98328 KB Output is correct
8 Correct 330 ms 98492 KB Output is correct
9 Correct 650 ms 102092 KB Output is correct
10 Correct 419 ms 101996 KB Output is correct
11 Correct 572 ms 97672 KB Output is correct
12 Correct 401 ms 98208 KB Output is correct
13 Correct 280 ms 98276 KB Output is correct
14 Correct 304 ms 98780 KB Output is correct
15 Correct 291 ms 98020 KB Output is correct
16 Correct 297 ms 98656 KB Output is correct
17 Correct 320 ms 99112 KB Output is correct
18 Correct 34 ms 77012 KB Output is correct
19 Correct 34 ms 77008 KB Output is correct
20 Correct 35 ms 76996 KB Output is correct
21 Correct 36 ms 76980 KB Output is correct
22 Correct 35 ms 76940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 77024 KB Output is correct
2 Correct 576 ms 97736 KB Output is correct
3 Correct 403 ms 98480 KB Output is correct
4 Correct 480 ms 100308 KB Output is correct
5 Correct 467 ms 98012 KB Output is correct
6 Correct 406 ms 97528 KB Output is correct
7 Correct 363 ms 98328 KB Output is correct
8 Correct 330 ms 98492 KB Output is correct
9 Correct 650 ms 102092 KB Output is correct
10 Correct 419 ms 101996 KB Output is correct
11 Correct 572 ms 97672 KB Output is correct
12 Correct 401 ms 98208 KB Output is correct
13 Correct 280 ms 98276 KB Output is correct
14 Correct 304 ms 98780 KB Output is correct
15 Correct 291 ms 98020 KB Output is correct
16 Correct 297 ms 98656 KB Output is correct
17 Correct 320 ms 99112 KB Output is correct
18 Correct 34 ms 77012 KB Output is correct
19 Correct 34 ms 77008 KB Output is correct
20 Correct 35 ms 76996 KB Output is correct
21 Correct 36 ms 76980 KB Output is correct
22 Correct 35 ms 76940 KB Output is correct
23 Correct 36 ms 76980 KB Output is correct
24 Correct 576 ms 97796 KB Output is correct
25 Correct 389 ms 98204 KB Output is correct
26 Correct 550 ms 102092 KB Output is correct
27 Correct 511 ms 97928 KB Output is correct
28 Correct 387 ms 98048 KB Output is correct
29 Correct 387 ms 97960 KB Output is correct
30 Correct 318 ms 98656 KB Output is correct
31 Correct 643 ms 100388 KB Output is correct
32 Correct 458 ms 102020 KB Output is correct
33 Correct 612 ms 97932 KB Output is correct
34 Correct 404 ms 98276 KB Output is correct
35 Correct 36 ms 77004 KB Output is correct
36 Correct 33 ms 77008 KB Output is correct
37 Correct 34 ms 77060 KB Output is correct
38 Correct 33 ms 77052 KB Output is correct
39 Correct 34 ms 77024 KB Output is correct
40 Correct 35 ms 77052 KB Output is correct
41 Correct 34 ms 76960 KB Output is correct
42 Correct 35 ms 77016 KB Output is correct
43 Correct 38 ms 77004 KB Output is correct
44 Correct 36 ms 77012 KB Output is correct
45 Correct 35 ms 77040 KB Output is correct
46 Correct 35 ms 77048 KB Output is correct
47 Correct 33 ms 77040 KB Output is correct
48 Correct 34 ms 77048 KB Output is correct
49 Correct 34 ms 77000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 77008 KB Output is correct
2 Correct 700 ms 98992 KB Output is correct
3 Correct 1079 ms 98412 KB Output is correct
4 Correct 689 ms 98152 KB Output is correct
5 Correct 622 ms 95008 KB Output is correct
6 Correct 447 ms 95144 KB Output is correct
7 Correct 454 ms 95324 KB Output is correct
8 Correct 354 ms 95528 KB Output is correct
9 Correct 683 ms 97584 KB Output is correct
10 Correct 1008 ms 99204 KB Output is correct
11 Correct 675 ms 94636 KB Output is correct
12 Correct 954 ms 95468 KB Output is correct
13 Correct 672 ms 96420 KB Output is correct
14 Correct 696 ms 97392 KB Output is correct
15 Correct 33 ms 77004 KB Output is correct
16 Correct 34 ms 76948 KB Output is correct
17 Correct 36 ms 76984 KB Output is correct
18 Correct 35 ms 77004 KB Output is correct
19 Correct 38 ms 76956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 76928 KB Output is correct
2 Correct 757 ms 99940 KB Output is correct
3 Correct 1039 ms 97572 KB Output is correct
4 Correct 692 ms 98736 KB Output is correct
5 Correct 729 ms 96516 KB Output is correct
6 Correct 537 ms 96692 KB Output is correct
7 Correct 501 ms 96560 KB Output is correct
8 Correct 401 ms 96636 KB Output is correct
9 Correct 759 ms 101468 KB Output is correct
10 Correct 1037 ms 99912 KB Output is correct
11 Correct 668 ms 97248 KB Output is correct
12 Correct 883 ms 95836 KB Output is correct
13 Correct 594 ms 96576 KB Output is correct
14 Correct 657 ms 97468 KB Output is correct
15 Correct 37 ms 77008 KB Output is correct
16 Correct 35 ms 77004 KB Output is correct
17 Correct 35 ms 77008 KB Output is correct
18 Correct 35 ms 76948 KB Output is correct
19 Correct 39 ms 77040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 77004 KB Output is correct
2 Correct 35 ms 76992 KB Output is correct
3 Correct 35 ms 76976 KB Output is correct
4 Correct 36 ms 77028 KB Output is correct
5 Correct 36 ms 77068 KB Output is correct
6 Correct 35 ms 77132 KB Output is correct
7 Correct 36 ms 77136 KB Output is correct
8 Correct 36 ms 77008 KB Output is correct
9 Correct 35 ms 77132 KB Output is correct
10 Correct 35 ms 77004 KB Output is correct
11 Correct 35 ms 77000 KB Output is correct
12 Correct 36 ms 76968 KB Output is correct
13 Correct 38 ms 76952 KB Output is correct
14 Correct 33 ms 77004 KB Output is correct
15 Correct 33 ms 77004 KB Output is correct
16 Correct 36 ms 77028 KB Output is correct
17 Correct 38 ms 77008 KB Output is correct
18 Correct 36 ms 77000 KB Output is correct
19 Correct 35 ms 76944 KB Output is correct
20 Correct 36 ms 76976 KB Output is correct
21 Correct 35 ms 77132 KB Output is correct
22 Correct 38 ms 76944 KB Output is correct
23 Correct 38 ms 77000 KB Output is correct
24 Correct 35 ms 76948 KB Output is correct
25 Correct 35 ms 76940 KB Output is correct
26 Correct 35 ms 77132 KB Output is correct
27 Correct 36 ms 77052 KB Output is correct
28 Correct 36 ms 77004 KB Output is correct
29 Correct 37 ms 77024 KB Output is correct
30 Correct 576 ms 97736 KB Output is correct
31 Correct 403 ms 98480 KB Output is correct
32 Correct 480 ms 100308 KB Output is correct
33 Correct 467 ms 98012 KB Output is correct
34 Correct 406 ms 97528 KB Output is correct
35 Correct 363 ms 98328 KB Output is correct
36 Correct 330 ms 98492 KB Output is correct
37 Correct 650 ms 102092 KB Output is correct
38 Correct 419 ms 101996 KB Output is correct
39 Correct 572 ms 97672 KB Output is correct
40 Correct 401 ms 98208 KB Output is correct
41 Correct 280 ms 98276 KB Output is correct
42 Correct 304 ms 98780 KB Output is correct
43 Correct 291 ms 98020 KB Output is correct
44 Correct 297 ms 98656 KB Output is correct
45 Correct 320 ms 99112 KB Output is correct
46 Correct 34 ms 77012 KB Output is correct
47 Correct 34 ms 77008 KB Output is correct
48 Correct 35 ms 76996 KB Output is correct
49 Correct 36 ms 76980 KB Output is correct
50 Correct 35 ms 76940 KB Output is correct
51 Correct 36 ms 76980 KB Output is correct
52 Correct 576 ms 97796 KB Output is correct
53 Correct 389 ms 98204 KB Output is correct
54 Correct 550 ms 102092 KB Output is correct
55 Correct 511 ms 97928 KB Output is correct
56 Correct 387 ms 98048 KB Output is correct
57 Correct 387 ms 97960 KB Output is correct
58 Correct 318 ms 98656 KB Output is correct
59 Correct 643 ms 100388 KB Output is correct
60 Correct 458 ms 102020 KB Output is correct
61 Correct 612 ms 97932 KB Output is correct
62 Correct 404 ms 98276 KB Output is correct
63 Correct 36 ms 77004 KB Output is correct
64 Correct 33 ms 77008 KB Output is correct
65 Correct 34 ms 77060 KB Output is correct
66 Correct 33 ms 77052 KB Output is correct
67 Correct 34 ms 77024 KB Output is correct
68 Correct 35 ms 77052 KB Output is correct
69 Correct 34 ms 76960 KB Output is correct
70 Correct 35 ms 77016 KB Output is correct
71 Correct 38 ms 77004 KB Output is correct
72 Correct 36 ms 77012 KB Output is correct
73 Correct 35 ms 77040 KB Output is correct
74 Correct 35 ms 77048 KB Output is correct
75 Correct 33 ms 77040 KB Output is correct
76 Correct 34 ms 77048 KB Output is correct
77 Correct 34 ms 77000 KB Output is correct
78 Correct 34 ms 77008 KB Output is correct
79 Correct 700 ms 98992 KB Output is correct
80 Correct 1079 ms 98412 KB Output is correct
81 Correct 689 ms 98152 KB Output is correct
82 Correct 622 ms 95008 KB Output is correct
83 Correct 447 ms 95144 KB Output is correct
84 Correct 454 ms 95324 KB Output is correct
85 Correct 354 ms 95528 KB Output is correct
86 Correct 683 ms 97584 KB Output is correct
87 Correct 1008 ms 99204 KB Output is correct
88 Correct 675 ms 94636 KB Output is correct
89 Correct 954 ms 95468 KB Output is correct
90 Correct 672 ms 96420 KB Output is correct
91 Correct 696 ms 97392 KB Output is correct
92 Correct 33 ms 77004 KB Output is correct
93 Correct 34 ms 76948 KB Output is correct
94 Correct 36 ms 76984 KB Output is correct
95 Correct 35 ms 77004 KB Output is correct
96 Correct 38 ms 76956 KB Output is correct
97 Correct 34 ms 76928 KB Output is correct
98 Correct 757 ms 99940 KB Output is correct
99 Correct 1039 ms 97572 KB Output is correct
100 Correct 692 ms 98736 KB Output is correct
101 Correct 729 ms 96516 KB Output is correct
102 Correct 537 ms 96692 KB Output is correct
103 Correct 501 ms 96560 KB Output is correct
104 Correct 401 ms 96636 KB Output is correct
105 Correct 759 ms 101468 KB Output is correct
106 Correct 1037 ms 99912 KB Output is correct
107 Correct 668 ms 97248 KB Output is correct
108 Correct 883 ms 95836 KB Output is correct
109 Correct 594 ms 96576 KB Output is correct
110 Correct 657 ms 97468 KB Output is correct
111 Correct 37 ms 77008 KB Output is correct
112 Correct 35 ms 77004 KB Output is correct
113 Correct 35 ms 77008 KB Output is correct
114 Correct 35 ms 76948 KB Output is correct
115 Correct 39 ms 77040 KB Output is correct
116 Correct 670 ms 95756 KB Output is correct
117 Correct 968 ms 98480 KB Output is correct
118 Correct 721 ms 101968 KB Output is correct
119 Correct 649 ms 97996 KB Output is correct
120 Correct 511 ms 97812 KB Output is correct
121 Correct 529 ms 98520 KB Output is correct
122 Correct 426 ms 98612 KB Output is correct
123 Correct 688 ms 100940 KB Output is correct
124 Correct 1170 ms 100340 KB Output is correct
125 Correct 712 ms 96840 KB Output is correct
126 Correct 1238 ms 98148 KB Output is correct
127 Correct 1269 ms 98604 KB Output is correct
128 Correct 877 ms 99336 KB Output is correct
129 Correct 1006 ms 100248 KB Output is correct