Submission #925303

# Submission time Handle Problem Language Result Execution time Memory
925303 2024-02-11T11:17:06 Z alexdd Sprinkler (JOI22_sprinkler) C++17
53 / 100
4000 ms 116368 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;

int n,phi;
long long MOD;

int aint[800005];
void build(int nod, int st, int dr)
{
    aint[nod]=1;
    if(st==dr)
        return;
    int mij=(st+dr)/2;
    build(nod*2,st,mij);
    build(nod*2+1,mij+1,dr);
}
void upd(int nod, int st, int dr, int le, int ri, int newv)
{
    if(le>ri)
        return;
    if(le==st && dr==ri)
    {
        aint[nod]=1LL*aint[nod]*newv%MOD;
        return;
    }
    int mij=(st+dr)/2;
    upd(nod*2,st,mij,le,min(mij,ri),newv);
    upd(nod*2+1,mij+1,dr,max(le,mij+1),ri,newv);
}
int qry(int nod, int st, int dr, int poz)
{
    if(st==dr)
        return aint[nod];
    int mij=(st+dr)/2;
    if(poz<=mij) return 1LL*qry(nod*2,st,mij,poz)*aint[nod]%MOD;
    else return 1LL*qry(nod*2+1,mij+1,dr,poz)*aint[nod]%MOD;
}

int aib0[200005];
int qry0(int poz)
{
    int aux=0;
    for(int i=poz;i>0;i-=(i&(-i)))
        aux+=aib0[i];
    return aux;
}
void upd0(int poz, int newv)
{
    for(int i=poz;i<=n;i+=(i&(-i)))
        aib0[i]+=newv;
}

int h[200005];
vector<int> con[200005];
int parent[200005];
int tole[200005][45];
int tori[200005][45];
int cntd[200005];
int cntd2[200005];
int depth[200005];
int pos[200005];
int care[200005];
void dfs(int nod)
{
    cntd[depth[nod]]++;
    for(auto adj:con[nod])
    {
        if(adj!=parent[nod])
        {
            depth[adj]=depth[nod]+1;
            parent[adj]=nod;
            dfs(adj);
        }
    }
}
void dfs2(int nod)
{
    cntd2[depth[nod]]++;
    pos[nod] = cntd[depth[nod]-1] + cntd2[depth[nod]];
    care[pos[nod]]=nod;
    //cout<<nod<<" "<<pos[nod]<<" pos\n";
    for(int d=0;d<=42;d++)
    {
        tole[nod][d] = n+5;
        tori[nod][d] = -1;
    }
    tole[nod][0] = tori[nod][0] = pos[nod];
    for(auto adj:con[nod])
    {
        if(adj!=parent[nod])
        {
            dfs2(adj);
            for(int d=1;d<=42;d++)
            {
                tole[nod][d] = min(tole[nod][d], tole[adj][d-1]);
                tori[nod][d] = max(tori[nod][d], tori[adj][d-1]);
            }
        }
    }
}
void faupd(int le, int ri, int newv)
{
    if(le>ri)
        return;
    if(newv!=0)
    {
        if(ri-le+1 <= 30)
        {
            for(int i=le;i<=ri;i++)
                h[care[i]] = (1LL*h[care[i]]*newv)%MOD;
        }
        else
            upd(1,1,n,le,ri,newv);
    }
    else
    {
        upd0(le,1);
        upd0(ri+1,-1);
    }
}
int calc_phi(int x)
{
    int d=2,prod=x;
    while(d*d<=x)
    {
        if(x%d==0)
        {
            while(x%d==0)
                x/=d;
            prod = prod / d * (d-1);
        }
        if(d==2)
            d--;
        d+=2;
    }
    if(x>1)
        prod = prod / x * (x-1);
    return prod;
}
int getrez(int poz)
{
    if(qry0(poz)>0)
        return 0;
    else
        return qry(1,1,n,poz);
}
int le[200005],ri[200005];
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>MOD;
    phi = calc_phi(MOD);
    int a,b;
    for(int i=1;i<n;i++)
    {
        cin>>a>>b;
        con[a].push_back(b);
        con[b].push_back(a);
    }
    depth[1]=1;
    dfs(1);
    for(int i=2;i<=n;i++)
        cntd[i]+=cntd[i-1];
    dfs2(1);
    for(int i=1;i<=n;i++)
    {
        cin>>h[i];
    }
    build(1,1,n);
    int cntq,tip,d;
    cin>>cntq;
    for(int pas=1;pas<=cntq;pas++)
    {
        cin>>tip;
        if(tip==1)
        {
            cin>>a>>d>>b;
            int cur=a;
            for(int i=0;i<=d;i++)
            {
                if(cur==0)
                    break;
                ///j > -1 + d - (i+1)


                int lim=0;
                if(parent[cur]!=0)
                    lim=max(lim,d-i-1);
                for(int j=lim;j<=d-i;j++)
                {
                    faupd(tole[cur][j],tori[cur][j],b);
                }
                cur = parent[cur];
            }
        }
        else
        {
            cin>>a;
            cout<<(1LL*h[a]*getrez(pos[a]))%MOD<<"\n";
            //cout<<h[a]<<" "<<qry_brut(pos[a])<<" zzz\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 4 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 3 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16984 KB Output is correct
17 Correct 4 ms 16732 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 4 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16984 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 4 ms 16728 KB Output is correct
28 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 331 ms 95304 KB Output is correct
3 Correct 336 ms 92032 KB Output is correct
4 Correct 372 ms 107764 KB Output is correct
5 Correct 321 ms 93624 KB Output is correct
6 Correct 359 ms 93932 KB Output is correct
7 Correct 303 ms 93700 KB Output is correct
8 Correct 281 ms 94400 KB Output is correct
9 Correct 365 ms 116368 KB Output is correct
10 Correct 371 ms 111620 KB Output is correct
11 Correct 308 ms 95312 KB Output is correct
12 Correct 319 ms 91984 KB Output is correct
13 Correct 295 ms 92608 KB Output is correct
14 Correct 308 ms 92620 KB Output is correct
15 Correct 297 ms 92532 KB Output is correct
16 Correct 286 ms 92744 KB Output is correct
17 Correct 293 ms 93060 KB Output is correct
18 Correct 3 ms 16728 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 3 ms 16728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 331 ms 95304 KB Output is correct
3 Correct 336 ms 92032 KB Output is correct
4 Correct 372 ms 107764 KB Output is correct
5 Correct 321 ms 93624 KB Output is correct
6 Correct 359 ms 93932 KB Output is correct
7 Correct 303 ms 93700 KB Output is correct
8 Correct 281 ms 94400 KB Output is correct
9 Correct 365 ms 116368 KB Output is correct
10 Correct 371 ms 111620 KB Output is correct
11 Correct 308 ms 95312 KB Output is correct
12 Correct 319 ms 91984 KB Output is correct
13 Correct 295 ms 92608 KB Output is correct
14 Correct 308 ms 92620 KB Output is correct
15 Correct 297 ms 92532 KB Output is correct
16 Correct 286 ms 92744 KB Output is correct
17 Correct 293 ms 93060 KB Output is correct
18 Correct 3 ms 16728 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 3 ms 16728 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 310 ms 95268 KB Output is correct
25 Correct 357 ms 91984 KB Output is correct
26 Correct 371 ms 113744 KB Output is correct
27 Correct 325 ms 93872 KB Output is correct
28 Correct 313 ms 93524 KB Output is correct
29 Correct 327 ms 93780 KB Output is correct
30 Correct 277 ms 94164 KB Output is correct
31 Correct 347 ms 110108 KB Output is correct
32 Correct 399 ms 111440 KB Output is correct
33 Correct 334 ms 95520 KB Output is correct
34 Correct 439 ms 92124 KB Output is correct
35 Correct 3 ms 16732 KB Output is correct
36 Correct 3 ms 16732 KB Output is correct
37 Correct 4 ms 16732 KB Output is correct
38 Correct 3 ms 16732 KB Output is correct
39 Correct 3 ms 16732 KB Output is correct
40 Correct 3 ms 16732 KB Output is correct
41 Correct 3 ms 16732 KB Output is correct
42 Correct 3 ms 16732 KB Output is correct
43 Correct 3 ms 16732 KB Output is correct
44 Correct 3 ms 16728 KB Output is correct
45 Correct 3 ms 16732 KB Output is correct
46 Correct 3 ms 16732 KB Output is correct
47 Correct 3 ms 16728 KB Output is correct
48 Correct 3 ms 16732 KB Output is correct
49 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 560 ms 113312 KB Output is correct
3 Correct 2898 ms 107692 KB Output is correct
4 Correct 1066 ms 108884 KB Output is correct
5 Correct 678 ms 91992 KB Output is correct
6 Correct 453 ms 92272 KB Output is correct
7 Correct 379 ms 92236 KB Output is correct
8 Correct 249 ms 92748 KB Output is correct
9 Correct 568 ms 107344 KB Output is correct
10 Correct 2861 ms 111076 KB Output is correct
11 Correct 380 ms 92400 KB Output is correct
12 Correct 1579 ms 92104 KB Output is correct
13 Correct 1268 ms 92716 KB Output is correct
14 Correct 1274 ms 93100 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16888 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 508 ms 109204 KB Output is correct
3 Correct 1628 ms 103520 KB Output is correct
4 Correct 756 ms 106580 KB Output is correct
5 Correct 812 ms 93820 KB Output is correct
6 Correct 855 ms 93720 KB Output is correct
7 Correct 755 ms 93896 KB Output is correct
8 Correct 317 ms 94040 KB Output is correct
9 Correct 528 ms 114624 KB Output is correct
10 Correct 1702 ms 111960 KB Output is correct
11 Correct 604 ms 95104 KB Output is correct
12 Correct 2308 ms 92228 KB Output is correct
13 Execution timed out 4038 ms 93436 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16732 KB Output is correct
2 Correct 2 ms 16732 KB Output is correct
3 Correct 2 ms 16732 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 4 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 16732 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 4 ms 16732 KB Output is correct
14 Correct 3 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16984 KB Output is correct
17 Correct 4 ms 16732 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 4 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 4 ms 16732 KB Output is correct
22 Correct 3 ms 16732 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16984 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 4 ms 16728 KB Output is correct
28 Correct 3 ms 16732 KB Output is correct
29 Correct 3 ms 16728 KB Output is correct
30 Correct 331 ms 95304 KB Output is correct
31 Correct 336 ms 92032 KB Output is correct
32 Correct 372 ms 107764 KB Output is correct
33 Correct 321 ms 93624 KB Output is correct
34 Correct 359 ms 93932 KB Output is correct
35 Correct 303 ms 93700 KB Output is correct
36 Correct 281 ms 94400 KB Output is correct
37 Correct 365 ms 116368 KB Output is correct
38 Correct 371 ms 111620 KB Output is correct
39 Correct 308 ms 95312 KB Output is correct
40 Correct 319 ms 91984 KB Output is correct
41 Correct 295 ms 92608 KB Output is correct
42 Correct 308 ms 92620 KB Output is correct
43 Correct 297 ms 92532 KB Output is correct
44 Correct 286 ms 92744 KB Output is correct
45 Correct 293 ms 93060 KB Output is correct
46 Correct 3 ms 16728 KB Output is correct
47 Correct 3 ms 16732 KB Output is correct
48 Correct 3 ms 16732 KB Output is correct
49 Correct 4 ms 16732 KB Output is correct
50 Correct 3 ms 16728 KB Output is correct
51 Correct 3 ms 16732 KB Output is correct
52 Correct 310 ms 95268 KB Output is correct
53 Correct 357 ms 91984 KB Output is correct
54 Correct 371 ms 113744 KB Output is correct
55 Correct 325 ms 93872 KB Output is correct
56 Correct 313 ms 93524 KB Output is correct
57 Correct 327 ms 93780 KB Output is correct
58 Correct 277 ms 94164 KB Output is correct
59 Correct 347 ms 110108 KB Output is correct
60 Correct 399 ms 111440 KB Output is correct
61 Correct 334 ms 95520 KB Output is correct
62 Correct 439 ms 92124 KB Output is correct
63 Correct 3 ms 16732 KB Output is correct
64 Correct 3 ms 16732 KB Output is correct
65 Correct 4 ms 16732 KB Output is correct
66 Correct 3 ms 16732 KB Output is correct
67 Correct 3 ms 16732 KB Output is correct
68 Correct 3 ms 16732 KB Output is correct
69 Correct 3 ms 16732 KB Output is correct
70 Correct 3 ms 16732 KB Output is correct
71 Correct 3 ms 16732 KB Output is correct
72 Correct 3 ms 16728 KB Output is correct
73 Correct 3 ms 16732 KB Output is correct
74 Correct 3 ms 16732 KB Output is correct
75 Correct 3 ms 16728 KB Output is correct
76 Correct 3 ms 16732 KB Output is correct
77 Correct 3 ms 16732 KB Output is correct
78 Correct 3 ms 16732 KB Output is correct
79 Correct 560 ms 113312 KB Output is correct
80 Correct 2898 ms 107692 KB Output is correct
81 Correct 1066 ms 108884 KB Output is correct
82 Correct 678 ms 91992 KB Output is correct
83 Correct 453 ms 92272 KB Output is correct
84 Correct 379 ms 92236 KB Output is correct
85 Correct 249 ms 92748 KB Output is correct
86 Correct 568 ms 107344 KB Output is correct
87 Correct 2861 ms 111076 KB Output is correct
88 Correct 380 ms 92400 KB Output is correct
89 Correct 1579 ms 92104 KB Output is correct
90 Correct 1268 ms 92716 KB Output is correct
91 Correct 1274 ms 93100 KB Output is correct
92 Correct 3 ms 16732 KB Output is correct
93 Correct 3 ms 16732 KB Output is correct
94 Correct 3 ms 16888 KB Output is correct
95 Correct 3 ms 16732 KB Output is correct
96 Correct 3 ms 16732 KB Output is correct
97 Correct 3 ms 16732 KB Output is correct
98 Correct 508 ms 109204 KB Output is correct
99 Correct 1628 ms 103520 KB Output is correct
100 Correct 756 ms 106580 KB Output is correct
101 Correct 812 ms 93820 KB Output is correct
102 Correct 855 ms 93720 KB Output is correct
103 Correct 755 ms 93896 KB Output is correct
104 Correct 317 ms 94040 KB Output is correct
105 Correct 528 ms 114624 KB Output is correct
106 Correct 1702 ms 111960 KB Output is correct
107 Correct 604 ms 95104 KB Output is correct
108 Correct 2308 ms 92228 KB Output is correct
109 Execution timed out 4038 ms 93436 KB Time limit exceeded
110 Halted 0 ms 0 KB -