Submission #749226

# Submission time Handle Problem Language Result Execution time Memory
749226 2023-05-27T14:58:21 Z GrindMachine Sprinkler (JOI22_sprinkler) C++17
100 / 100
1228 ms 102024 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
https://github.com/koosaga/olympiad/blob/master/JOI/camp22_sprinkler.cpp

*/

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)
......
  118 |             rep1(j,sz(order)-1){
      |                  ~~~~~~~~~~~~~       
sprinkler.cpp:118:13: note: in expansion of macro 'rep1'
  118 |             rep1(j,sz(order)-1){
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 53 ms 76996 KB Output is correct
2 Correct 38 ms 76928 KB Output is correct
3 Correct 42 ms 76924 KB Output is correct
4 Correct 41 ms 77052 KB Output is correct
5 Correct 44 ms 77008 KB Output is correct
6 Correct 35 ms 77068 KB Output is correct
7 Correct 36 ms 77016 KB Output is correct
8 Correct 35 ms 77132 KB Output is correct
9 Correct 37 ms 77016 KB Output is correct
10 Correct 34 ms 77004 KB Output is correct
11 Correct 36 ms 77004 KB Output is correct
12 Correct 44 ms 76944 KB Output is correct
13 Correct 41 ms 76972 KB Output is correct
14 Correct 45 ms 77000 KB Output is correct
15 Correct 35 ms 77012 KB Output is correct
16 Correct 35 ms 77012 KB Output is correct
17 Correct 45 ms 77036 KB Output is correct
18 Correct 35 ms 77004 KB Output is correct
19 Correct 39 ms 77004 KB Output is correct
20 Correct 37 ms 77004 KB Output is correct
21 Correct 39 ms 76996 KB Output is correct
22 Correct 38 ms 76948 KB Output is correct
23 Correct 37 ms 77016 KB Output is correct
24 Correct 41 ms 76968 KB Output is correct
25 Correct 39 ms 77016 KB Output is correct
26 Correct 39 ms 76964 KB Output is correct
27 Correct 41 ms 77000 KB Output is correct
28 Correct 39 ms 77008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 76924 KB Output is correct
2 Correct 639 ms 97756 KB Output is correct
3 Correct 397 ms 97060 KB Output is correct
4 Correct 559 ms 100352 KB Output is correct
5 Correct 515 ms 97940 KB Output is correct
6 Correct 391 ms 97488 KB Output is correct
7 Correct 358 ms 98260 KB Output is correct
8 Correct 322 ms 98532 KB Output is correct
9 Correct 661 ms 101968 KB Output is correct
10 Correct 460 ms 100828 KB Output is correct
11 Correct 678 ms 97732 KB Output is correct
12 Correct 391 ms 97032 KB Output is correct
13 Correct 275 ms 97212 KB Output is correct
14 Correct 275 ms 97060 KB Output is correct
15 Correct 284 ms 96964 KB Output is correct
16 Correct 279 ms 96672 KB Output is correct
17 Correct 301 ms 97424 KB Output is correct
18 Correct 36 ms 77012 KB Output is correct
19 Correct 36 ms 77052 KB Output is correct
20 Correct 36 ms 77016 KB Output is correct
21 Correct 36 ms 77056 KB Output is correct
22 Correct 35 ms 77084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 76924 KB Output is correct
2 Correct 639 ms 97756 KB Output is correct
3 Correct 397 ms 97060 KB Output is correct
4 Correct 559 ms 100352 KB Output is correct
5 Correct 515 ms 97940 KB Output is correct
6 Correct 391 ms 97488 KB Output is correct
7 Correct 358 ms 98260 KB Output is correct
8 Correct 322 ms 98532 KB Output is correct
9 Correct 661 ms 101968 KB Output is correct
10 Correct 460 ms 100828 KB Output is correct
11 Correct 678 ms 97732 KB Output is correct
12 Correct 391 ms 97032 KB Output is correct
13 Correct 275 ms 97212 KB Output is correct
14 Correct 275 ms 97060 KB Output is correct
15 Correct 284 ms 96964 KB Output is correct
16 Correct 279 ms 96672 KB Output is correct
17 Correct 301 ms 97424 KB Output is correct
18 Correct 36 ms 77012 KB Output is correct
19 Correct 36 ms 77052 KB Output is correct
20 Correct 36 ms 77016 KB Output is correct
21 Correct 36 ms 77056 KB Output is correct
22 Correct 35 ms 77084 KB Output is correct
23 Correct 33 ms 77004 KB Output is correct
24 Correct 572 ms 97608 KB Output is correct
25 Correct 416 ms 96372 KB Output is correct
26 Correct 508 ms 102024 KB Output is correct
27 Correct 507 ms 97760 KB Output is correct
28 Correct 433 ms 97788 KB Output is correct
29 Correct 379 ms 97556 KB Output is correct
30 Correct 318 ms 97724 KB Output is correct
31 Correct 634 ms 100360 KB Output is correct
32 Correct 451 ms 99276 KB Output is correct
33 Correct 608 ms 97628 KB Output is correct
34 Correct 415 ms 94572 KB Output is correct
35 Correct 36 ms 76988 KB Output is correct
36 Correct 35 ms 77048 KB Output is correct
37 Correct 37 ms 77048 KB Output is correct
38 Correct 36 ms 77000 KB Output is correct
39 Correct 37 ms 77000 KB Output is correct
40 Correct 37 ms 77004 KB Output is correct
41 Correct 36 ms 77032 KB Output is correct
42 Correct 37 ms 76996 KB Output is correct
43 Correct 36 ms 77004 KB Output is correct
44 Correct 36 ms 77140 KB Output is correct
45 Correct 36 ms 77004 KB Output is correct
46 Correct 36 ms 77040 KB Output is correct
47 Correct 37 ms 76944 KB Output is correct
48 Correct 36 ms 76988 KB Output is correct
49 Correct 36 ms 76952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 77004 KB Output is correct
2 Correct 730 ms 99084 KB Output is correct
3 Correct 1040 ms 97364 KB Output is correct
4 Correct 758 ms 98008 KB Output is correct
5 Correct 662 ms 94708 KB Output is correct
6 Correct 459 ms 94852 KB Output is correct
7 Correct 453 ms 95148 KB Output is correct
8 Correct 376 ms 95164 KB Output is correct
9 Correct 712 ms 97420 KB Output is correct
10 Correct 1041 ms 98128 KB Output is correct
11 Correct 648 ms 94588 KB Output is correct
12 Correct 938 ms 93876 KB Output is correct
13 Correct 637 ms 94540 KB Output is correct
14 Correct 681 ms 95348 KB Output is correct
15 Correct 36 ms 77016 KB Output is correct
16 Correct 37 ms 77004 KB Output is correct
17 Correct 36 ms 76980 KB Output is correct
18 Correct 38 ms 76948 KB Output is correct
19 Correct 38 ms 76968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 77012 KB Output is correct
2 Correct 724 ms 99636 KB Output is correct
3 Correct 1057 ms 95424 KB Output is correct
4 Correct 715 ms 97600 KB Output is correct
5 Correct 660 ms 95300 KB Output is correct
6 Correct 506 ms 95308 KB Output is correct
7 Correct 465 ms 95052 KB Output is correct
8 Correct 392 ms 95056 KB Output is correct
9 Correct 873 ms 100092 KB Output is correct
10 Correct 1228 ms 96524 KB Output is correct
11 Correct 769 ms 95952 KB Output is correct
12 Correct 1010 ms 92492 KB Output is correct
13 Correct 720 ms 93520 KB Output is correct
14 Correct 757 ms 94776 KB Output is correct
15 Correct 38 ms 77004 KB Output is correct
16 Correct 39 ms 77040 KB Output is correct
17 Correct 39 ms 76940 KB Output is correct
18 Correct 38 ms 76980 KB Output is correct
19 Correct 38 ms 76992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 76996 KB Output is correct
2 Correct 38 ms 76928 KB Output is correct
3 Correct 42 ms 76924 KB Output is correct
4 Correct 41 ms 77052 KB Output is correct
5 Correct 44 ms 77008 KB Output is correct
6 Correct 35 ms 77068 KB Output is correct
7 Correct 36 ms 77016 KB Output is correct
8 Correct 35 ms 77132 KB Output is correct
9 Correct 37 ms 77016 KB Output is correct
10 Correct 34 ms 77004 KB Output is correct
11 Correct 36 ms 77004 KB Output is correct
12 Correct 44 ms 76944 KB Output is correct
13 Correct 41 ms 76972 KB Output is correct
14 Correct 45 ms 77000 KB Output is correct
15 Correct 35 ms 77012 KB Output is correct
16 Correct 35 ms 77012 KB Output is correct
17 Correct 45 ms 77036 KB Output is correct
18 Correct 35 ms 77004 KB Output is correct
19 Correct 39 ms 77004 KB Output is correct
20 Correct 37 ms 77004 KB Output is correct
21 Correct 39 ms 76996 KB Output is correct
22 Correct 38 ms 76948 KB Output is correct
23 Correct 37 ms 77016 KB Output is correct
24 Correct 41 ms 76968 KB Output is correct
25 Correct 39 ms 77016 KB Output is correct
26 Correct 39 ms 76964 KB Output is correct
27 Correct 41 ms 77000 KB Output is correct
28 Correct 39 ms 77008 KB Output is correct
29 Correct 36 ms 76924 KB Output is correct
30 Correct 639 ms 97756 KB Output is correct
31 Correct 397 ms 97060 KB Output is correct
32 Correct 559 ms 100352 KB Output is correct
33 Correct 515 ms 97940 KB Output is correct
34 Correct 391 ms 97488 KB Output is correct
35 Correct 358 ms 98260 KB Output is correct
36 Correct 322 ms 98532 KB Output is correct
37 Correct 661 ms 101968 KB Output is correct
38 Correct 460 ms 100828 KB Output is correct
39 Correct 678 ms 97732 KB Output is correct
40 Correct 391 ms 97032 KB Output is correct
41 Correct 275 ms 97212 KB Output is correct
42 Correct 275 ms 97060 KB Output is correct
43 Correct 284 ms 96964 KB Output is correct
44 Correct 279 ms 96672 KB Output is correct
45 Correct 301 ms 97424 KB Output is correct
46 Correct 36 ms 77012 KB Output is correct
47 Correct 36 ms 77052 KB Output is correct
48 Correct 36 ms 77016 KB Output is correct
49 Correct 36 ms 77056 KB Output is correct
50 Correct 35 ms 77084 KB Output is correct
51 Correct 33 ms 77004 KB Output is correct
52 Correct 572 ms 97608 KB Output is correct
53 Correct 416 ms 96372 KB Output is correct
54 Correct 508 ms 102024 KB Output is correct
55 Correct 507 ms 97760 KB Output is correct
56 Correct 433 ms 97788 KB Output is correct
57 Correct 379 ms 97556 KB Output is correct
58 Correct 318 ms 97724 KB Output is correct
59 Correct 634 ms 100360 KB Output is correct
60 Correct 451 ms 99276 KB Output is correct
61 Correct 608 ms 97628 KB Output is correct
62 Correct 415 ms 94572 KB Output is correct
63 Correct 36 ms 76988 KB Output is correct
64 Correct 35 ms 77048 KB Output is correct
65 Correct 37 ms 77048 KB Output is correct
66 Correct 36 ms 77000 KB Output is correct
67 Correct 37 ms 77000 KB Output is correct
68 Correct 37 ms 77004 KB Output is correct
69 Correct 36 ms 77032 KB Output is correct
70 Correct 37 ms 76996 KB Output is correct
71 Correct 36 ms 77004 KB Output is correct
72 Correct 36 ms 77140 KB Output is correct
73 Correct 36 ms 77004 KB Output is correct
74 Correct 36 ms 77040 KB Output is correct
75 Correct 37 ms 76944 KB Output is correct
76 Correct 36 ms 76988 KB Output is correct
77 Correct 36 ms 76952 KB Output is correct
78 Correct 36 ms 77004 KB Output is correct
79 Correct 730 ms 99084 KB Output is correct
80 Correct 1040 ms 97364 KB Output is correct
81 Correct 758 ms 98008 KB Output is correct
82 Correct 662 ms 94708 KB Output is correct
83 Correct 459 ms 94852 KB Output is correct
84 Correct 453 ms 95148 KB Output is correct
85 Correct 376 ms 95164 KB Output is correct
86 Correct 712 ms 97420 KB Output is correct
87 Correct 1041 ms 98128 KB Output is correct
88 Correct 648 ms 94588 KB Output is correct
89 Correct 938 ms 93876 KB Output is correct
90 Correct 637 ms 94540 KB Output is correct
91 Correct 681 ms 95348 KB Output is correct
92 Correct 36 ms 77016 KB Output is correct
93 Correct 37 ms 77004 KB Output is correct
94 Correct 36 ms 76980 KB Output is correct
95 Correct 38 ms 76948 KB Output is correct
96 Correct 38 ms 76968 KB Output is correct
97 Correct 36 ms 77012 KB Output is correct
98 Correct 724 ms 99636 KB Output is correct
99 Correct 1057 ms 95424 KB Output is correct
100 Correct 715 ms 97600 KB Output is correct
101 Correct 660 ms 95300 KB Output is correct
102 Correct 506 ms 95308 KB Output is correct
103 Correct 465 ms 95052 KB Output is correct
104 Correct 392 ms 95056 KB Output is correct
105 Correct 873 ms 100092 KB Output is correct
106 Correct 1228 ms 96524 KB Output is correct
107 Correct 769 ms 95952 KB Output is correct
108 Correct 1010 ms 92492 KB Output is correct
109 Correct 720 ms 93520 KB Output is correct
110 Correct 757 ms 94776 KB Output is correct
111 Correct 38 ms 77004 KB Output is correct
112 Correct 39 ms 77040 KB Output is correct
113 Correct 39 ms 76940 KB Output is correct
114 Correct 38 ms 76980 KB Output is correct
115 Correct 38 ms 76992 KB Output is correct
116 Correct 822 ms 95124 KB Output is correct
117 Correct 1195 ms 92648 KB Output is correct
118 Correct 960 ms 98968 KB Output is correct
119 Correct 821 ms 94932 KB Output is correct
120 Correct 578 ms 94784 KB Output is correct
121 Correct 512 ms 95176 KB Output is correct
122 Correct 451 ms 94964 KB Output is correct
123 Correct 772 ms 99140 KB Output is correct
124 Correct 1102 ms 93376 KB Output is correct
125 Correct 711 ms 93256 KB Output is correct
126 Correct 1184 ms 89200 KB Output is correct
127 Correct 1037 ms 89368 KB Output is correct
128 Correct 786 ms 90068 KB Output is correct
129 Correct 865 ms 91076 KB Output is correct