Submission #877046

# Submission time Handle Problem Language Result Execution time Memory
877046 2023-11-22T19:29:33 Z Ice_man Sprinkler (JOI22_sprinkler) C++14
100 / 100
835 ms 113336 KB
#include <iostream>
#include <chrono>
#include <vector>
#include <cstring>

#define maxn 200005
#define maxlog 20
#define maxd 45
#define INF 1000000010
#define LINF 1000000000000000005
#define endl '\n'
#define pb(x) push_back(x)
#define X first
#define Y second
#define control cout<<"passed"<<endl;

#pragma GCC optimize("O3" , "Ofast" , "fast-math" , "unroll-loops")
#pragma GCC target(avx2)

using namespace std;

std::chrono::high_resolution_clock::time_point startT, currT;
constexpr double TIME_MULT = 1;

double timePassed()
{
    using namespace std::chrono;
    currT = high_resolution_clock::now();
    double time = duration_cast<duration<double>>(currT - startT).count();
    return time * TIME_MULT;
}

struct query
{
    long long node , distance , weight;
    query(){};
    query(long long node2 , long long distance2 , long long weight2)
    {
        node = node2;
        distance = distance2;
        weight = weight2;
    }
};

long long n;
long long mod;
vector <long long> v[maxn];
vector < query > queries;
long long q;
long long num[maxn];

void read()
{
    cin >> n >> mod;

    long long from , to;
    for(long long i = 1; i <= (n - 1); i++)
    {
        cin >> from >> to;

        v[from].pb(to);
        v[to].pb(from);
    }

    for(long long i = 1; i <= n; i++) cin >> num[i];

    cin >> q;

    queries.resize(q + 1);
    long long t;

    for(long long i = 1; i <= q; i++)
    {
        cin >> t;
        if(t == 1) cin >> queries[i].node >> queries[i].distance >> queries[i].weight;
        else
        {
            cin >> queries[i].node;
            queries[i].distance = -1;
        }
    }
}

long long depth[maxn];
long long parent[maxn];

void dfs(long long node , long long cur_parent)
{

    for(long long nb : v[node]) if(nb != cur_parent)
    {
        parent[nb] = node;
        depth[nb] = depth[node] + 1;

        dfs(nb , node);

    }

}

long long multiply[maxn][maxd];

void rec(long long node , long long distance , long long weight)
{

    ///cout << node << " " << distance << " " << weight << endl;

    if(node == 0) return;
    if(distance == -1) return;

    rec(parent[node] , distance - 1 , weight);

    if(node == 1) for(long long i = 0; i <= distance; i++) multiply[node][i] = (multiply[node][i] * weight) % mod;
    else
    {
        multiply[node][distance] *= weight;
        multiply[node][distance] %= mod;

        if(distance)
        {
            multiply[node][distance - 1] *= weight;
            multiply[node][distance - 1] %= mod;
        }

    }

}

long long find_number(long long node , long long cur_depth)
{
    if(node == 1) return (multiply[node][cur_depth] % mod);
    if(cur_depth >= 1) return (multiply[node][cur_depth] % mod);

    return find_number(parent[node] , cur_depth + 1) % mod;

}


void solve()
{
    ///memset(multiply , 1 , sizeof(multiply));

    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j <= 40; j++) multiply[i][j] = 1;
    }


    for(long long i = 1; i <= q; i++)
    {
        if(queries[i].distance == -1)
        {

            long long ans = num[queries[i].node];
            long long node = queries[i].node;

            ///cout << ". " << ans << endl;

            for(long long j = 0; j <= 40; j++)
            {
                ans *= multiply[node][j];
                ans %= mod;

                ///cout << "= " << node << " " << multiply[node][j] << " " << j << endl;

                /**if(parent[node] == 0) break;
                node = parent[node];*/

                if(parent[node] != 0) node = parent[node];
                else break;

            }

            /**ans *= find_number(queries[i].node , 0) << endl;
            ans %= mod;*/

            cout << ans << endl;

        }
        else rec(queries[i].node , queries[i].distance , queries[i].weight);
    }


}

int main()
{

#ifdef ONLINE_JUDGE
    freopen("taxi.in", "r", stdin);
    freopen("taxi.out", "w", stdout);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    startT = std::chrono::high_resolution_clock::now();

    read();
    dfs(1 , 0);
    ///cout << "--------------" << endl;

    solve();

    return 0;
}

Compilation message

sprinkler.cpp:18:20: warning: '#pragma GCC option' is not a string [-Wpragmas]
   18 | #pragma GCC target(avx2)
      |                    ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10332 KB Output is correct
2 Correct 2 ms 10332 KB Output is correct
3 Correct 2 ms 10332 KB Output is correct
4 Correct 3 ms 12668 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 10332 KB Output is correct
10 Correct 2 ms 10332 KB Output is correct
11 Correct 2 ms 10328 KB Output is correct
12 Correct 3 ms 10360 KB Output is correct
13 Correct 3 ms 10360 KB Output is correct
14 Correct 2 ms 10332 KB Output is correct
15 Correct 2 ms 10344 KB Output is correct
16 Correct 2 ms 10332 KB Output is correct
17 Correct 2 ms 10372 KB Output is correct
18 Correct 2 ms 10332 KB Output is correct
19 Correct 2 ms 10332 KB Output is correct
20 Correct 3 ms 10332 KB Output is correct
21 Correct 3 ms 10332 KB Output is correct
22 Correct 3 ms 10332 KB Output is correct
23 Correct 3 ms 10332 KB Output is correct
24 Correct 2 ms 10584 KB Output is correct
25 Correct 2 ms 10332 KB Output is correct
26 Correct 2 ms 10368 KB Output is correct
27 Correct 2 ms 10368 KB Output is correct
28 Correct 2 ms 10328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10332 KB Output is correct
2 Correct 371 ms 108616 KB Output is correct
3 Correct 226 ms 109392 KB Output is correct
4 Correct 333 ms 111652 KB Output is correct
5 Correct 296 ms 108944 KB Output is correct
6 Correct 269 ms 108408 KB Output is correct
7 Correct 230 ms 109244 KB Output is correct
8 Correct 184 ms 109500 KB Output is correct
9 Correct 506 ms 113084 KB Output is correct
10 Correct 260 ms 113336 KB Output is correct
11 Correct 354 ms 108688 KB Output is correct
12 Correct 280 ms 109040 KB Output is correct
13 Correct 174 ms 109120 KB Output is correct
14 Correct 180 ms 109752 KB Output is correct
15 Correct 173 ms 108852 KB Output is correct
16 Correct 177 ms 109648 KB Output is correct
17 Correct 180 ms 109908 KB Output is correct
18 Correct 3 ms 10328 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10396 KB Output is correct
21 Correct 3 ms 10332 KB Output is correct
22 Correct 3 ms 10248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10332 KB Output is correct
2 Correct 371 ms 108616 KB Output is correct
3 Correct 226 ms 109392 KB Output is correct
4 Correct 333 ms 111652 KB Output is correct
5 Correct 296 ms 108944 KB Output is correct
6 Correct 269 ms 108408 KB Output is correct
7 Correct 230 ms 109244 KB Output is correct
8 Correct 184 ms 109500 KB Output is correct
9 Correct 506 ms 113084 KB Output is correct
10 Correct 260 ms 113336 KB Output is correct
11 Correct 354 ms 108688 KB Output is correct
12 Correct 280 ms 109040 KB Output is correct
13 Correct 174 ms 109120 KB Output is correct
14 Correct 180 ms 109752 KB Output is correct
15 Correct 173 ms 108852 KB Output is correct
16 Correct 177 ms 109648 KB Output is correct
17 Correct 180 ms 109908 KB Output is correct
18 Correct 3 ms 10328 KB Output is correct
19 Correct 3 ms 10588 KB Output is correct
20 Correct 3 ms 10396 KB Output is correct
21 Correct 3 ms 10332 KB Output is correct
22 Correct 3 ms 10248 KB Output is correct
23 Correct 2 ms 10328 KB Output is correct
24 Correct 406 ms 108628 KB Output is correct
25 Correct 256 ms 109268 KB Output is correct
26 Correct 358 ms 113288 KB Output is correct
27 Correct 338 ms 109028 KB Output is correct
28 Correct 233 ms 109144 KB Output is correct
29 Correct 252 ms 108792 KB Output is correct
30 Correct 189 ms 109500 KB Output is correct
31 Correct 450 ms 111696 KB Output is correct
32 Correct 325 ms 113232 KB Output is correct
33 Correct 357 ms 108624 KB Output is correct
34 Correct 258 ms 109156 KB Output is correct
35 Correct 3 ms 10368 KB Output is correct
36 Correct 2 ms 10344 KB Output is correct
37 Correct 2 ms 10332 KB Output is correct
38 Correct 2 ms 10332 KB Output is correct
39 Correct 2 ms 10332 KB Output is correct
40 Correct 2 ms 10332 KB Output is correct
41 Correct 3 ms 10332 KB Output is correct
42 Correct 3 ms 10332 KB Output is correct
43 Correct 2 ms 10332 KB Output is correct
44 Correct 2 ms 10332 KB Output is correct
45 Correct 3 ms 10332 KB Output is correct
46 Correct 3 ms 10332 KB Output is correct
47 Correct 3 ms 10328 KB Output is correct
48 Correct 2 ms 10332 KB Output is correct
49 Correct 2 ms 10332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10332 KB Output is correct
2 Correct 499 ms 110384 KB Output is correct
3 Correct 835 ms 109948 KB Output is correct
4 Correct 518 ms 109532 KB Output is correct
5 Correct 375 ms 106064 KB Output is correct
6 Correct 278 ms 106120 KB Output is correct
7 Correct 254 ms 106560 KB Output is correct
8 Correct 192 ms 106332 KB Output is correct
9 Correct 638 ms 108636 KB Output is correct
10 Correct 815 ms 110484 KB Output is correct
11 Correct 395 ms 105700 KB Output is correct
12 Correct 578 ms 106580 KB Output is correct
13 Correct 401 ms 107392 KB Output is correct
14 Correct 377 ms 108100 KB Output is correct
15 Correct 2 ms 10332 KB Output is correct
16 Correct 2 ms 10332 KB Output is correct
17 Correct 2 ms 10164 KB Output is correct
18 Correct 2 ms 10332 KB Output is correct
19 Correct 2 ms 10332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10332 KB Output is correct
2 Correct 578 ms 111172 KB Output is correct
3 Correct 784 ms 108908 KB Output is correct
4 Correct 468 ms 109932 KB Output is correct
5 Correct 387 ms 107604 KB Output is correct
6 Correct 281 ms 107796 KB Output is correct
7 Correct 282 ms 107604 KB Output is correct
8 Correct 199 ms 107660 KB Output is correct
9 Correct 504 ms 112656 KB Output is correct
10 Correct 771 ms 111168 KB Output is correct
11 Correct 372 ms 108180 KB Output is correct
12 Correct 542 ms 106792 KB Output is correct
13 Correct 355 ms 107568 KB Output is correct
14 Correct 404 ms 108660 KB Output is correct
15 Correct 2 ms 10328 KB Output is correct
16 Correct 2 ms 10528 KB Output is correct
17 Correct 2 ms 10332 KB Output is correct
18 Correct 3 ms 10356 KB Output is correct
19 Correct 3 ms 10328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10332 KB Output is correct
2 Correct 2 ms 10332 KB Output is correct
3 Correct 2 ms 10332 KB Output is correct
4 Correct 3 ms 12668 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 3 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 2 ms 10332 KB Output is correct
10 Correct 2 ms 10332 KB Output is correct
11 Correct 2 ms 10328 KB Output is correct
12 Correct 3 ms 10360 KB Output is correct
13 Correct 3 ms 10360 KB Output is correct
14 Correct 2 ms 10332 KB Output is correct
15 Correct 2 ms 10344 KB Output is correct
16 Correct 2 ms 10332 KB Output is correct
17 Correct 2 ms 10372 KB Output is correct
18 Correct 2 ms 10332 KB Output is correct
19 Correct 2 ms 10332 KB Output is correct
20 Correct 3 ms 10332 KB Output is correct
21 Correct 3 ms 10332 KB Output is correct
22 Correct 3 ms 10332 KB Output is correct
23 Correct 3 ms 10332 KB Output is correct
24 Correct 2 ms 10584 KB Output is correct
25 Correct 2 ms 10332 KB Output is correct
26 Correct 2 ms 10368 KB Output is correct
27 Correct 2 ms 10368 KB Output is correct
28 Correct 2 ms 10328 KB Output is correct
29 Correct 2 ms 10332 KB Output is correct
30 Correct 371 ms 108616 KB Output is correct
31 Correct 226 ms 109392 KB Output is correct
32 Correct 333 ms 111652 KB Output is correct
33 Correct 296 ms 108944 KB Output is correct
34 Correct 269 ms 108408 KB Output is correct
35 Correct 230 ms 109244 KB Output is correct
36 Correct 184 ms 109500 KB Output is correct
37 Correct 506 ms 113084 KB Output is correct
38 Correct 260 ms 113336 KB Output is correct
39 Correct 354 ms 108688 KB Output is correct
40 Correct 280 ms 109040 KB Output is correct
41 Correct 174 ms 109120 KB Output is correct
42 Correct 180 ms 109752 KB Output is correct
43 Correct 173 ms 108852 KB Output is correct
44 Correct 177 ms 109648 KB Output is correct
45 Correct 180 ms 109908 KB Output is correct
46 Correct 3 ms 10328 KB Output is correct
47 Correct 3 ms 10588 KB Output is correct
48 Correct 3 ms 10396 KB Output is correct
49 Correct 3 ms 10332 KB Output is correct
50 Correct 3 ms 10248 KB Output is correct
51 Correct 2 ms 10328 KB Output is correct
52 Correct 406 ms 108628 KB Output is correct
53 Correct 256 ms 109268 KB Output is correct
54 Correct 358 ms 113288 KB Output is correct
55 Correct 338 ms 109028 KB Output is correct
56 Correct 233 ms 109144 KB Output is correct
57 Correct 252 ms 108792 KB Output is correct
58 Correct 189 ms 109500 KB Output is correct
59 Correct 450 ms 111696 KB Output is correct
60 Correct 325 ms 113232 KB Output is correct
61 Correct 357 ms 108624 KB Output is correct
62 Correct 258 ms 109156 KB Output is correct
63 Correct 3 ms 10368 KB Output is correct
64 Correct 2 ms 10344 KB Output is correct
65 Correct 2 ms 10332 KB Output is correct
66 Correct 2 ms 10332 KB Output is correct
67 Correct 2 ms 10332 KB Output is correct
68 Correct 2 ms 10332 KB Output is correct
69 Correct 3 ms 10332 KB Output is correct
70 Correct 3 ms 10332 KB Output is correct
71 Correct 2 ms 10332 KB Output is correct
72 Correct 2 ms 10332 KB Output is correct
73 Correct 3 ms 10332 KB Output is correct
74 Correct 3 ms 10332 KB Output is correct
75 Correct 3 ms 10328 KB Output is correct
76 Correct 2 ms 10332 KB Output is correct
77 Correct 2 ms 10332 KB Output is correct
78 Correct 3 ms 10332 KB Output is correct
79 Correct 499 ms 110384 KB Output is correct
80 Correct 835 ms 109948 KB Output is correct
81 Correct 518 ms 109532 KB Output is correct
82 Correct 375 ms 106064 KB Output is correct
83 Correct 278 ms 106120 KB Output is correct
84 Correct 254 ms 106560 KB Output is correct
85 Correct 192 ms 106332 KB Output is correct
86 Correct 638 ms 108636 KB Output is correct
87 Correct 815 ms 110484 KB Output is correct
88 Correct 395 ms 105700 KB Output is correct
89 Correct 578 ms 106580 KB Output is correct
90 Correct 401 ms 107392 KB Output is correct
91 Correct 377 ms 108100 KB Output is correct
92 Correct 2 ms 10332 KB Output is correct
93 Correct 2 ms 10332 KB Output is correct
94 Correct 2 ms 10164 KB Output is correct
95 Correct 2 ms 10332 KB Output is correct
96 Correct 2 ms 10332 KB Output is correct
97 Correct 2 ms 10332 KB Output is correct
98 Correct 578 ms 111172 KB Output is correct
99 Correct 784 ms 108908 KB Output is correct
100 Correct 468 ms 109932 KB Output is correct
101 Correct 387 ms 107604 KB Output is correct
102 Correct 281 ms 107796 KB Output is correct
103 Correct 282 ms 107604 KB Output is correct
104 Correct 199 ms 107660 KB Output is correct
105 Correct 504 ms 112656 KB Output is correct
106 Correct 771 ms 111168 KB Output is correct
107 Correct 372 ms 108180 KB Output is correct
108 Correct 542 ms 106792 KB Output is correct
109 Correct 355 ms 107568 KB Output is correct
110 Correct 404 ms 108660 KB Output is correct
111 Correct 2 ms 10328 KB Output is correct
112 Correct 2 ms 10528 KB Output is correct
113 Correct 2 ms 10332 KB Output is correct
114 Correct 3 ms 10356 KB Output is correct
115 Correct 3 ms 10328 KB Output is correct
116 Correct 422 ms 107096 KB Output is correct
117 Correct 544 ms 109348 KB Output is correct
118 Correct 479 ms 113156 KB Output is correct
119 Correct 424 ms 109072 KB Output is correct
120 Correct 294 ms 108644 KB Output is correct
121 Correct 311 ms 109456 KB Output is correct
122 Correct 208 ms 109436 KB Output is correct
123 Correct 540 ms 112168 KB Output is correct
124 Correct 817 ms 111556 KB Output is correct
125 Correct 408 ms 107860 KB Output is correct
126 Correct 592 ms 109280 KB Output is correct
127 Correct 657 ms 109860 KB Output is correct
128 Correct 478 ms 110608 KB Output is correct
129 Correct 507 ms 111184 KB Output is correct