답안 #1013541

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1013541 2024-07-03T15:52:22 Z 8pete8 Sprinkler (JOI22_sprinkler) C++17
100 / 100
2957 ms 202948 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m;
vector<int>adj[mxn+10];
void setIO(string name){		
	ios_base::sync_with_stdio(0); cin.tie(0);		
	freopen((name+".in").c_str(),"r",stdin);		
	freopen((name+".out").c_str(),"w",stdout);	
}
int h[mxn+10],dist[mxn+10],sz[mxn+10],del[mxn+10],id[mxn+10];
struct qry{
    int time,node,D,w,type;
    bool operator<(qry a)const{return D-dist[node]<=a.D-dist[a.node];};
};
vector<qry>have[mxn+10],event;
struct seg{
    int v[2*mxn+10];
    void init(int n){for(int i=0;i<=2*n;i++)v[i]=1;}
    void update(int pos,int val,int n){
        pos+=n;
        v[pos]=(v[pos]*val)%k;
        for(int i=pos;i>0;i>>=1)v[i>>1]=(v[i]*v[i^1])%k;
    }
    int qry(int l,int r,int n){
        int ans=1;
        for(l+=n,r+=n;l<=r;l>>=1,r>>=1){
            if(l&1)ans=(ans*v[l++])%k;
            if(!(r&1))ans=(ans*v[r--])%k;
        }
        return ans;
    }
}t2[45];
vector<qry>bro[mxn+10];
void getsz(int cur,int p){
    sz[cur]=1,dist[cur]=0;
    for(auto i:adj[cur])if(i!=p&&!del[i])getsz(i,cur),sz[cur]+=sz[i];
}
int getcen(int cur,int p,int need){
    for(auto i:adj[cur])if(i!=p&&sz[i]>=need&&!del[i])return getcen(i,cur,need);
    return cur;
}
int in[mxn+10],where[mxn+10],ans[mxn+10];
void dfs(int cur,int p,int deg){
    in[cur]=deg;
    for(auto i:have[cur]){
        if(i.type==1){
            if(i.D-dist[cur]>=0)event.pb(i);
        }
        else event.pb(i);
    }
    for(auto i:adj[cur])if(i!=p&&!del[i]){
        dist[i]=dist[cur]+1;
        dfs(i,cur,deg);
    }
}
bool cmp(qry a,qry b){return a.time<b.time;}
void solve(int cur){
    getsz(cur,-1);
    int node=getcen(cur,-1,sz[cur]/2);
    for(auto i:have[node]){
        if(i.type==1){
            if(i.D-dist[node]>=0)event.pb(i);
        }
        else event.pb(i);
    }
    for(int i=0;i<adj[node].size();i++)if(!del[adj[node][i]])dist[adj[node][i]]=1,dfs(adj[node][i],node,i);
    sort(all(event),cmp);
    for(int i=0;i<=40;i++)t2[i].init(adj[node].size()+1);
    vector<int>ex(41,1);
    for(auto i:event){
        if(i.type==1&&id[i.time]!=-1){
            if(i.node==node)ex[i.D-dist[i.node]]=(ex[i.D-dist[i.node]]*i.w)%k;
            else t2[(i.D-dist[i.node])].update(in[i.node],i.w,adj[node].size());
        }
        else if(i.type==2){
            for(int K=dist[i.node];K<=40;K++){
                ans[i.time]=(ans[i.time]*ex[K])%k;
                if(i.node==node)ans[i.time]=(ans[i.time]*t2[K].qry(0,adj[node].size()-1,adj[node].size()))%k;
                else{
                    if(in[i.node])ans[i.time]=(ans[i.time]*(t2[K].qry(0,in[i.node]-1,adj[node].size())))%k;
                    if(in[i.node]+1<adj[node].size())ans[i.time]=(ans[i.time]*(t2[K].qry(in[i.node]+1,adj[node].size()-1,adj[node].size())))%k;
                }
            }

        }

    }
    event.clear();
    del[node]=1;
    for(auto i:adj[node])if(!del[i])solve(i);
}
int32_t main(){
    fastio
    //d is less than 40 wtf '0'
    cin>>n>>k;
    for(int i=0;i<n-1;i++){
        int a,b;cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    for(int i=1;i<=n;i++)cin>>h[i];
    int q;cin>>q;
    vector<int>what;
    for(int i=0;i<q;i++){
        int a;cin>>a;
        qry x;
        x.type=a;
        x.time=i;
        if(a==1){
            cin>>x.node>>x.D>>x.w;
            x.w%=k;
            have[x.node].pb(x);
        }
        else{
            cin>>x.node;
            have[x.node].pb(x);
            what.pb(i);
            ans[i]=h[x.node];
        }
    }
    solve(1);
    for(auto i:what){
        cout<<ans[i]<<"\n";
        if(ans[i]<0)assert(0);
    }
}
/*
4 7
1 2
2 3
3 4
1
1
1
1
11
1 2 1 2
1 1 0 2
2 1
2 2
2 3
2 4
1 4 10 2
2 1
2 2
2 3
2 4

*/

Compilation message

sprinkler.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
   32 | #pragma GCC optimize ("03,unroll-lopps")
      |                                        ^
sprinkler.cpp:39:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   39 | void setIO(string name){
      |                       ^
sprinkler.cpp:47:26: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   47 |     bool operator<(qry a)const{return D-dist[node]<=a.D-dist[a.node];};
      |                          ^~~~~
sprinkler.cpp:52:20: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   52 |     void init(int n){for(int i=0;i<=2*n;i++)v[i]=1;}
      |                    ^
sprinkler.cpp:53:38: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   53 |     void update(int pos,int val,int n){
      |                                      ^
sprinkler.cpp:58:30: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   58 |     int qry(int l,int r,int n){
      |                              ^
sprinkler.cpp:68:25: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   68 | void getsz(int cur,int p){
      |                         ^
sprinkler.cpp:72:34: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   72 | int getcen(int cur,int p,int need){
      |                                  ^
sprinkler.cpp:77:31: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   77 | void dfs(int cur,int p,int deg){
      |                               ^
sprinkler.cpp:90:21: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   90 | bool cmp(qry a,qry b){return a.time<b.time;}
      |                     ^
sprinkler.cpp:91:19: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   91 | void solve(int cur){
      |                   ^
sprinkler.cpp: In function 'void solve(long long int)':
sprinkler.cpp:100:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for(int i=0;i<adj[node].size();i++)if(!del[adj[node][i]])dist[adj[node][i]]=1,dfs(adj[node][i],node,i);
      |                 ~^~~~~~~~~~~~~~~~~
sprinkler.cpp:115:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |                     if(in[i.node]+1<adj[node].size())ans[i.time]=(ans[i.time]*(t2[K].qry(in[i.node]+1,adj[node].size()-1,adj[node].size())))%k;
      |                        ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
sprinkler.cpp: At global scope:
sprinkler.cpp:126:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
  126 | int32_t main(){
      |              ^
sprinkler.cpp: In function 'void setIO(std::string)':
sprinkler.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sprinkler.cpp:42:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14684 KB Output is correct
2 Correct 5 ms 14908 KB Output is correct
3 Correct 5 ms 14684 KB Output is correct
4 Correct 8 ms 15196 KB Output is correct
5 Correct 8 ms 14940 KB Output is correct
6 Correct 8 ms 14940 KB Output is correct
7 Correct 8 ms 14940 KB Output is correct
8 Correct 8 ms 15692 KB Output is correct
9 Correct 6 ms 14940 KB Output is correct
10 Correct 6 ms 15008 KB Output is correct
11 Correct 6 ms 14940 KB Output is correct
12 Correct 6 ms 14940 KB Output is correct
13 Correct 7 ms 14940 KB Output is correct
14 Correct 6 ms 14940 KB Output is correct
15 Correct 6 ms 14940 KB Output is correct
16 Correct 6 ms 15092 KB Output is correct
17 Correct 7 ms 14868 KB Output is correct
18 Correct 7 ms 14940 KB Output is correct
19 Correct 6 ms 14940 KB Output is correct
20 Correct 8 ms 15008 KB Output is correct
21 Correct 7 ms 14940 KB Output is correct
22 Correct 9 ms 15076 KB Output is correct
23 Correct 6 ms 14940 KB Output is correct
24 Correct 6 ms 14892 KB Output is correct
25 Correct 6 ms 14940 KB Output is correct
26 Correct 6 ms 15020 KB Output is correct
27 Correct 7 ms 14940 KB Output is correct
28 Correct 10 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14684 KB Output is correct
2 Correct 1902 ms 78252 KB Output is correct
3 Correct 688 ms 56872 KB Output is correct
4 Correct 1425 ms 78240 KB Output is correct
5 Correct 1308 ms 66816 KB Output is correct
6 Correct 1469 ms 66660 KB Output is correct
7 Correct 1500 ms 67236 KB Output is correct
8 Correct 2785 ms 199200 KB Output is correct
9 Correct 1903 ms 89768 KB Output is correct
10 Correct 776 ms 68252 KB Output is correct
11 Correct 1813 ms 78228 KB Output is correct
12 Correct 624 ms 56832 KB Output is correct
13 Correct 842 ms 181248 KB Output is correct
14 Correct 802 ms 103916 KB Output is correct
15 Correct 787 ms 70900 KB Output is correct
16 Correct 739 ms 59312 KB Output is correct
17 Correct 719 ms 55392 KB Output is correct
18 Correct 6 ms 14936 KB Output is correct
19 Correct 7 ms 14940 KB Output is correct
20 Correct 7 ms 14940 KB Output is correct
21 Correct 7 ms 14940 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14684 KB Output is correct
2 Correct 1902 ms 78252 KB Output is correct
3 Correct 688 ms 56872 KB Output is correct
4 Correct 1425 ms 78240 KB Output is correct
5 Correct 1308 ms 66816 KB Output is correct
6 Correct 1469 ms 66660 KB Output is correct
7 Correct 1500 ms 67236 KB Output is correct
8 Correct 2785 ms 199200 KB Output is correct
9 Correct 1903 ms 89768 KB Output is correct
10 Correct 776 ms 68252 KB Output is correct
11 Correct 1813 ms 78228 KB Output is correct
12 Correct 624 ms 56832 KB Output is correct
13 Correct 842 ms 181248 KB Output is correct
14 Correct 802 ms 103916 KB Output is correct
15 Correct 787 ms 70900 KB Output is correct
16 Correct 739 ms 59312 KB Output is correct
17 Correct 719 ms 55392 KB Output is correct
18 Correct 6 ms 14936 KB Output is correct
19 Correct 7 ms 14940 KB Output is correct
20 Correct 7 ms 14940 KB Output is correct
21 Correct 7 ms 14940 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
23 Correct 5 ms 14684 KB Output is correct
24 Correct 1839 ms 78408 KB Output is correct
25 Correct 753 ms 56832 KB Output is correct
26 Correct 1457 ms 78292 KB Output is correct
27 Correct 1294 ms 66856 KB Output is correct
28 Correct 1562 ms 66996 KB Output is correct
29 Correct 1475 ms 67032 KB Output is correct
30 Correct 2952 ms 200588 KB Output is correct
31 Correct 2041 ms 89508 KB Output is correct
32 Correct 865 ms 68096 KB Output is correct
33 Correct 1950 ms 78248 KB Output is correct
34 Correct 679 ms 56884 KB Output is correct
35 Correct 6 ms 14936 KB Output is correct
36 Correct 6 ms 14940 KB Output is correct
37 Correct 6 ms 14940 KB Output is correct
38 Correct 7 ms 14940 KB Output is correct
39 Correct 7 ms 14940 KB Output is correct
40 Correct 7 ms 14940 KB Output is correct
41 Correct 6 ms 14936 KB Output is correct
42 Correct 7 ms 14940 KB Output is correct
43 Correct 8 ms 14936 KB Output is correct
44 Correct 7 ms 14936 KB Output is correct
45 Correct 6 ms 14940 KB Output is correct
46 Correct 6 ms 15092 KB Output is correct
47 Correct 7 ms 14940 KB Output is correct
48 Correct 7 ms 15000 KB Output is correct
49 Correct 7 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14684 KB Output is correct
2 Correct 2016 ms 89576 KB Output is correct
3 Correct 935 ms 67880 KB Output is correct
4 Correct 1484 ms 76720 KB Output is correct
5 Correct 1319 ms 65400 KB Output is correct
6 Correct 1642 ms 75884 KB Output is correct
7 Correct 1632 ms 76116 KB Output is correct
8 Correct 2899 ms 201544 KB Output is correct
9 Correct 1923 ms 89712 KB Output is correct
10 Correct 882 ms 67952 KB Output is correct
11 Correct 1873 ms 78304 KB Output is correct
12 Correct 870 ms 56536 KB Output is correct
13 Correct 878 ms 57712 KB Output is correct
14 Correct 792 ms 58040 KB Output is correct
15 Correct 6 ms 14940 KB Output is correct
16 Correct 6 ms 15000 KB Output is correct
17 Correct 10 ms 15192 KB Output is correct
18 Correct 7 ms 14940 KB Output is correct
19 Correct 7 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14680 KB Output is correct
2 Correct 1980 ms 89724 KB Output is correct
3 Correct 926 ms 68348 KB Output is correct
4 Correct 1526 ms 78240 KB Output is correct
5 Correct 1368 ms 66868 KB Output is correct
6 Correct 1768 ms 75696 KB Output is correct
7 Correct 1698 ms 75948 KB Output is correct
8 Correct 2885 ms 202928 KB Output is correct
9 Correct 1876 ms 89796 KB Output is correct
10 Correct 971 ms 68444 KB Output is correct
11 Correct 1828 ms 78256 KB Output is correct
12 Correct 814 ms 57088 KB Output is correct
13 Correct 821 ms 57684 KB Output is correct
14 Correct 831 ms 58204 KB Output is correct
15 Correct 8 ms 14936 KB Output is correct
16 Correct 9 ms 14940 KB Output is correct
17 Correct 6 ms 14840 KB Output is correct
18 Correct 6 ms 14852 KB Output is correct
19 Correct 6 ms 15064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14684 KB Output is correct
2 Correct 5 ms 14908 KB Output is correct
3 Correct 5 ms 14684 KB Output is correct
4 Correct 8 ms 15196 KB Output is correct
5 Correct 8 ms 14940 KB Output is correct
6 Correct 8 ms 14940 KB Output is correct
7 Correct 8 ms 14940 KB Output is correct
8 Correct 8 ms 15692 KB Output is correct
9 Correct 6 ms 14940 KB Output is correct
10 Correct 6 ms 15008 KB Output is correct
11 Correct 6 ms 14940 KB Output is correct
12 Correct 6 ms 14940 KB Output is correct
13 Correct 7 ms 14940 KB Output is correct
14 Correct 6 ms 14940 KB Output is correct
15 Correct 6 ms 14940 KB Output is correct
16 Correct 6 ms 15092 KB Output is correct
17 Correct 7 ms 14868 KB Output is correct
18 Correct 7 ms 14940 KB Output is correct
19 Correct 6 ms 14940 KB Output is correct
20 Correct 8 ms 15008 KB Output is correct
21 Correct 7 ms 14940 KB Output is correct
22 Correct 9 ms 15076 KB Output is correct
23 Correct 6 ms 14940 KB Output is correct
24 Correct 6 ms 14892 KB Output is correct
25 Correct 6 ms 14940 KB Output is correct
26 Correct 6 ms 15020 KB Output is correct
27 Correct 7 ms 14940 KB Output is correct
28 Correct 10 ms 14940 KB Output is correct
29 Correct 6 ms 14684 KB Output is correct
30 Correct 1902 ms 78252 KB Output is correct
31 Correct 688 ms 56872 KB Output is correct
32 Correct 1425 ms 78240 KB Output is correct
33 Correct 1308 ms 66816 KB Output is correct
34 Correct 1469 ms 66660 KB Output is correct
35 Correct 1500 ms 67236 KB Output is correct
36 Correct 2785 ms 199200 KB Output is correct
37 Correct 1903 ms 89768 KB Output is correct
38 Correct 776 ms 68252 KB Output is correct
39 Correct 1813 ms 78228 KB Output is correct
40 Correct 624 ms 56832 KB Output is correct
41 Correct 842 ms 181248 KB Output is correct
42 Correct 802 ms 103916 KB Output is correct
43 Correct 787 ms 70900 KB Output is correct
44 Correct 739 ms 59312 KB Output is correct
45 Correct 719 ms 55392 KB Output is correct
46 Correct 6 ms 14936 KB Output is correct
47 Correct 7 ms 14940 KB Output is correct
48 Correct 7 ms 14940 KB Output is correct
49 Correct 7 ms 14940 KB Output is correct
50 Correct 6 ms 14940 KB Output is correct
51 Correct 5 ms 14684 KB Output is correct
52 Correct 1839 ms 78408 KB Output is correct
53 Correct 753 ms 56832 KB Output is correct
54 Correct 1457 ms 78292 KB Output is correct
55 Correct 1294 ms 66856 KB Output is correct
56 Correct 1562 ms 66996 KB Output is correct
57 Correct 1475 ms 67032 KB Output is correct
58 Correct 2952 ms 200588 KB Output is correct
59 Correct 2041 ms 89508 KB Output is correct
60 Correct 865 ms 68096 KB Output is correct
61 Correct 1950 ms 78248 KB Output is correct
62 Correct 679 ms 56884 KB Output is correct
63 Correct 6 ms 14936 KB Output is correct
64 Correct 6 ms 14940 KB Output is correct
65 Correct 6 ms 14940 KB Output is correct
66 Correct 7 ms 14940 KB Output is correct
67 Correct 7 ms 14940 KB Output is correct
68 Correct 7 ms 14940 KB Output is correct
69 Correct 6 ms 14936 KB Output is correct
70 Correct 7 ms 14940 KB Output is correct
71 Correct 8 ms 14936 KB Output is correct
72 Correct 7 ms 14936 KB Output is correct
73 Correct 6 ms 14940 KB Output is correct
74 Correct 6 ms 15092 KB Output is correct
75 Correct 7 ms 14940 KB Output is correct
76 Correct 7 ms 15000 KB Output is correct
77 Correct 7 ms 14940 KB Output is correct
78 Correct 5 ms 14684 KB Output is correct
79 Correct 2016 ms 89576 KB Output is correct
80 Correct 935 ms 67880 KB Output is correct
81 Correct 1484 ms 76720 KB Output is correct
82 Correct 1319 ms 65400 KB Output is correct
83 Correct 1642 ms 75884 KB Output is correct
84 Correct 1632 ms 76116 KB Output is correct
85 Correct 2899 ms 201544 KB Output is correct
86 Correct 1923 ms 89712 KB Output is correct
87 Correct 882 ms 67952 KB Output is correct
88 Correct 1873 ms 78304 KB Output is correct
89 Correct 870 ms 56536 KB Output is correct
90 Correct 878 ms 57712 KB Output is correct
91 Correct 792 ms 58040 KB Output is correct
92 Correct 6 ms 14940 KB Output is correct
93 Correct 6 ms 15000 KB Output is correct
94 Correct 10 ms 15192 KB Output is correct
95 Correct 7 ms 14940 KB Output is correct
96 Correct 7 ms 14940 KB Output is correct
97 Correct 6 ms 14680 KB Output is correct
98 Correct 1980 ms 89724 KB Output is correct
99 Correct 926 ms 68348 KB Output is correct
100 Correct 1526 ms 78240 KB Output is correct
101 Correct 1368 ms 66868 KB Output is correct
102 Correct 1768 ms 75696 KB Output is correct
103 Correct 1698 ms 75948 KB Output is correct
104 Correct 2885 ms 202928 KB Output is correct
105 Correct 1876 ms 89796 KB Output is correct
106 Correct 971 ms 68444 KB Output is correct
107 Correct 1828 ms 78256 KB Output is correct
108 Correct 814 ms 57088 KB Output is correct
109 Correct 821 ms 57684 KB Output is correct
110 Correct 831 ms 58204 KB Output is correct
111 Correct 8 ms 14936 KB Output is correct
112 Correct 9 ms 14940 KB Output is correct
113 Correct 6 ms 14840 KB Output is correct
114 Correct 6 ms 14852 KB Output is correct
115 Correct 6 ms 15064 KB Output is correct
116 Correct 1843 ms 78236 KB Output is correct
117 Correct 858 ms 56688 KB Output is correct
118 Correct 1396 ms 78244 KB Output is correct
119 Correct 1295 ms 66892 KB Output is correct
120 Correct 1519 ms 75948 KB Output is correct
121 Correct 1663 ms 75956 KB Output is correct
122 Correct 2957 ms 202948 KB Output is correct
123 Correct 1920 ms 89512 KB Output is correct
124 Correct 888 ms 68284 KB Output is correct
125 Correct 1830 ms 78252 KB Output is correct
126 Correct 845 ms 56832 KB Output is correct
127 Correct 755 ms 55620 KB Output is correct
128 Correct 805 ms 57696 KB Output is correct
129 Correct 894 ms 58108 KB Output is correct