Submission #617455

# Submission time Handle Problem Language Result Execution time Memory
617455 2022-08-01T11:29:33 Z patrikpavic2 Sprinkler (JOI22_sprinkler) C++17
83 / 100
4000 ms 684628 KB
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef vector < int > vi;
typedef long long ll;

const int N = 2e5 + 500;
const int LOG = 20;
const int INF = 0x3f3f3f3f;

int MOD;

inline int mul(int A, int B){
	return (ll)A * B % MOD;
}

struct tournament{
	int siz, off;
	vi T;
	void init(int _siz){
		siz = _siz; off = 1;
		while(off < siz) off <<= 1;
		T.resize(2 * off);
		for(int &x : T) x = 1;
	}
	int get(int x){
		int ret = 1;
		for(x = (x + off); x ; x /= 2)
			ret = mul(ret, T[x]);
		return ret;
	}
	inline void multiply(int l, int r, int x){
		if(r < l) return;
		r++;
		for (l += off, r += off; l < r; l >>= 1, r >>= 1) {
			if(l&1) T[l] = mul(T[l], x), l++;
			if(r&1) r--, T[r] = mul(T[r], x);
		}
	}
};

tournament T[N][41];
int cen[N], siz[N], par[N], dep[N][LOG], gore[N], tko[N][41];
vector < int > pos;
int L[N][41], R[N][41], gdje[N][LOG], osb[N], gdje2[N][LOG];
int n, q;
vector < int > v[N];

void dfs(int x, int lst){
	siz[x] = 1; pos.PB(x);
	for(int y : v[x]){
		if(cen[y] != INF || y == lst) continue;
		dfs(y, x); siz[x] += siz[y];
	}
}

int TATA, RAZ;

void dfs2(int x, int lst, int dis){
	gdje2[x][RAZ] = dis;
	if(dis <= 40) {
		gdje[x][RAZ] = tko[TATA][dis];
		tko[TATA][dis]++;
	}
	for(int y : v[x]){
		if(y == lst || cen[y] <= RAZ) continue;
		dfs2(y, x, dis + 1);
	}
}

int nadi_centroid(int x){
	pos.clear(); dfs(x, x); 
	int bst = x;
	for(int y : pos)
		if(2 * siz[y] >= (int)pos.size() && siz[y] < siz[bst])
			bst = y;
	return bst;
}


int centroidna(int x, int dub){
	x = nadi_centroid(x);
	cen[x] = dub;
	for(int y : v[x]){
		if(cen[y] != INF) continue;
		int nov = centroidna(y, dub + 1);
		for(int i = 1;i <= 40;i++)
			L[nov][i] = tko[x][i];
		TATA = x; RAZ = dub;
		dfs2(y, x, 1);
		for(int i = 1;i <= 40;i++)
			R[nov][i] = tko[x][i] - 1;
		par[nov] = x; 
	}
	return x;
}

void obradi(int x, int d, int k){
	osb[x] = mul(osb[x], k);
	for(int i = 1;i <= d;i++) 
		T[x][i].multiply(0, T[x][i].siz - 1, k);
	int lst = x, poc = x; 
	x = par[x]; 
	while(x){
		if(gdje2[poc][cen[x]] <= d)
			osb[x] = mul(osb[x], k);		
		for(int i = 1;i <= d - gdje2[poc][cen[x]];i++){
			T[x][i].multiply(0, L[lst][i] - 1, k);
			T[x][i].multiply(R[lst][i] + 1, T[x][i].siz - 1, k);
		}
		lst = x; x = par[x];	
	}
}

int get(int x){
	int ret = osb[x];
	int cur = x;
	while(cur){
		if(gdje[x][cen[cur]] != -1){
			ret = mul(ret, T[cur][gdje2[x][cen[cur]]].get(gdje[x][cen[cur]]));
		}
		cur = par[cur];
	}
	return ret;
	
}

int main(){
	memset(cen, INF, sizeof(cen));
	memset(gdje, -1, sizeof(gdje));
	scanf("%d%d", &n, &MOD);
	for(int i = 1;i < n;i++){
		int x, y; scanf("%d%d", &x, &y);
		v[x].PB(y), v[y].PB(x);
	}
	for(int i = 1;i <= n;i++)
		scanf("%d", osb + i);
	centroidna(1, 1);
	for(int i = 1;i <= n;i++)
		for(int j = 0;j <= 40;j++)
			T[i][j].init(tko[i][j]);
	scanf("%d", &q);
	for(;q--;){
		int ty; scanf("%d", &ty);
		if(ty == 2){
			int x; scanf("%d", &x);
			printf("%d\n", get(x));
		}
		else{
			int x, d, k; scanf("%d%d%d", &x, &d, &k);
			obradi(x, d, k);
		}
	}
	return 0;
}





Compilation message

sprinkler.cpp: In function 'int main()':
sprinkler.cpp:139:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |  scanf("%d%d", &n, &MOD);
      |  ~~~~~^~~~~~~~~~~~~~~~~~
sprinkler.cpp:141:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |   int x, y; scanf("%d%d", &x, &y);
      |             ~~~~~^~~~~~~~~~~~~~~~
sprinkler.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |   scanf("%d", osb + i);
      |   ~~~~~^~~~~~~~~~~~~~~
sprinkler.cpp:150:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  150 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
sprinkler.cpp:152:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |   int ty; scanf("%d", &ty);
      |           ~~~~~^~~~~~~~~~~
sprinkler.cpp:154:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |    int x; scanf("%d", &x);
      |           ~~~~~^~~~~~~~~~
sprinkler.cpp:158:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |    int x, d, k; scanf("%d%d%d", &x, &d, &k);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 134 ms 278804 KB Output is correct
2 Correct 125 ms 278920 KB Output is correct
3 Correct 127 ms 278916 KB Output is correct
4 Correct 137 ms 280832 KB Output is correct
5 Correct 147 ms 280852 KB Output is correct
6 Correct 149 ms 280792 KB Output is correct
7 Correct 132 ms 280732 KB Output is correct
8 Correct 126 ms 280560 KB Output is correct
9 Correct 124 ms 278992 KB Output is correct
10 Correct 127 ms 278860 KB Output is correct
11 Correct 125 ms 278924 KB Output is correct
12 Correct 130 ms 278888 KB Output is correct
13 Correct 133 ms 278860 KB Output is correct
14 Correct 126 ms 278916 KB Output is correct
15 Correct 129 ms 278860 KB Output is correct
16 Correct 126 ms 278892 KB Output is correct
17 Correct 124 ms 278976 KB Output is correct
18 Correct 134 ms 278868 KB Output is correct
19 Correct 131 ms 278916 KB Output is correct
20 Correct 129 ms 278824 KB Output is correct
21 Correct 124 ms 278928 KB Output is correct
22 Correct 122 ms 278852 KB Output is correct
23 Correct 129 ms 278936 KB Output is correct
24 Correct 137 ms 278836 KB Output is correct
25 Correct 125 ms 278888 KB Output is correct
26 Correct 121 ms 278964 KB Output is correct
27 Correct 130 ms 278928 KB Output is correct
28 Correct 121 ms 278940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 278868 KB Output is correct
2 Correct 1861 ms 671632 KB Output is correct
3 Correct 1423 ms 668504 KB Output is correct
4 Correct 1767 ms 669752 KB Output is correct
5 Correct 1666 ms 669956 KB Output is correct
6 Correct 1602 ms 676028 KB Output is correct
7 Correct 1667 ms 674396 KB Output is correct
8 Correct 954 ms 630448 KB Output is correct
9 Correct 1795 ms 676432 KB Output is correct
10 Correct 1741 ms 671964 KB Output is correct
11 Correct 1741 ms 671608 KB Output is correct
12 Correct 1472 ms 668592 KB Output is correct
13 Correct 1088 ms 658456 KB Output is correct
14 Correct 1129 ms 658836 KB Output is correct
15 Correct 1269 ms 662004 KB Output is correct
16 Correct 1231 ms 662468 KB Output is correct
17 Correct 1260 ms 666820 KB Output is correct
18 Correct 129 ms 278852 KB Output is correct
19 Correct 129 ms 278900 KB Output is correct
20 Correct 143 ms 278932 KB Output is correct
21 Correct 135 ms 278880 KB Output is correct
22 Correct 145 ms 278924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 278868 KB Output is correct
2 Correct 1861 ms 671632 KB Output is correct
3 Correct 1423 ms 668504 KB Output is correct
4 Correct 1767 ms 669752 KB Output is correct
5 Correct 1666 ms 669956 KB Output is correct
6 Correct 1602 ms 676028 KB Output is correct
7 Correct 1667 ms 674396 KB Output is correct
8 Correct 954 ms 630448 KB Output is correct
9 Correct 1795 ms 676432 KB Output is correct
10 Correct 1741 ms 671964 KB Output is correct
11 Correct 1741 ms 671608 KB Output is correct
12 Correct 1472 ms 668592 KB Output is correct
13 Correct 1088 ms 658456 KB Output is correct
14 Correct 1129 ms 658836 KB Output is correct
15 Correct 1269 ms 662004 KB Output is correct
16 Correct 1231 ms 662468 KB Output is correct
17 Correct 1260 ms 666820 KB Output is correct
18 Correct 129 ms 278852 KB Output is correct
19 Correct 129 ms 278900 KB Output is correct
20 Correct 143 ms 278932 KB Output is correct
21 Correct 135 ms 278880 KB Output is correct
22 Correct 145 ms 278924 KB Output is correct
23 Correct 139 ms 278880 KB Output is correct
24 Correct 1765 ms 672160 KB Output is correct
25 Correct 1491 ms 668848 KB Output is correct
26 Correct 1720 ms 674544 KB Output is correct
27 Correct 1586 ms 670560 KB Output is correct
28 Correct 1717 ms 677548 KB Output is correct
29 Correct 1518 ms 673968 KB Output is correct
30 Correct 997 ms 630820 KB Output is correct
31 Correct 1859 ms 672268 KB Output is correct
32 Correct 1813 ms 672524 KB Output is correct
33 Correct 1782 ms 672036 KB Output is correct
34 Correct 1524 ms 668676 KB Output is correct
35 Correct 131 ms 278916 KB Output is correct
36 Correct 133 ms 278900 KB Output is correct
37 Correct 150 ms 278864 KB Output is correct
38 Correct 133 ms 278868 KB Output is correct
39 Correct 142 ms 278900 KB Output is correct
40 Correct 130 ms 278840 KB Output is correct
41 Correct 134 ms 278896 KB Output is correct
42 Correct 149 ms 278924 KB Output is correct
43 Correct 156 ms 278904 KB Output is correct
44 Correct 129 ms 279052 KB Output is correct
45 Correct 130 ms 278884 KB Output is correct
46 Correct 137 ms 278820 KB Output is correct
47 Correct 130 ms 278932 KB Output is correct
48 Correct 135 ms 278924 KB Output is correct
49 Correct 139 ms 278864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 278848 KB Output is correct
2 Correct 1999 ms 674008 KB Output is correct
3 Correct 3324 ms 669692 KB Output is correct
4 Correct 2382 ms 677992 KB Output is correct
5 Correct 2404 ms 676580 KB Output is correct
6 Correct 3165 ms 681748 KB Output is correct
7 Correct 3310 ms 680076 KB Output is correct
8 Correct 1191 ms 636848 KB Output is correct
9 Correct 2208 ms 676440 KB Output is correct
10 Correct 3317 ms 680064 KB Output is correct
11 Correct 2133 ms 676080 KB Output is correct
12 Correct 3856 ms 676828 KB Output is correct
13 Correct 3359 ms 650916 KB Output is correct
14 Correct 3657 ms 657816 KB Output is correct
15 Correct 142 ms 278844 KB Output is correct
16 Correct 143 ms 278920 KB Output is correct
17 Correct 138 ms 278876 KB Output is correct
18 Correct 137 ms 278880 KB Output is correct
19 Correct 141 ms 278836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 278888 KB Output is correct
2 Correct 2067 ms 671888 KB Output is correct
3 Correct 3085 ms 666556 KB Output is correct
4 Correct 2209 ms 669644 KB Output is correct
5 Correct 2519 ms 670772 KB Output is correct
6 Correct 3671 ms 678860 KB Output is correct
7 Correct 3194 ms 681784 KB Output is correct
8 Correct 1083 ms 637904 KB Output is correct
9 Correct 1937 ms 682556 KB Output is correct
10 Correct 3090 ms 681084 KB Output is correct
11 Correct 2122 ms 678856 KB Output is correct
12 Correct 3822 ms 677352 KB Output is correct
13 Correct 3383 ms 650936 KB Output is correct
14 Correct 3527 ms 658052 KB Output is correct
15 Correct 138 ms 278904 KB Output is correct
16 Correct 139 ms 278920 KB Output is correct
17 Correct 140 ms 278880 KB Output is correct
18 Correct 152 ms 278860 KB Output is correct
19 Correct 139 ms 278900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 134 ms 278804 KB Output is correct
2 Correct 125 ms 278920 KB Output is correct
3 Correct 127 ms 278916 KB Output is correct
4 Correct 137 ms 280832 KB Output is correct
5 Correct 147 ms 280852 KB Output is correct
6 Correct 149 ms 280792 KB Output is correct
7 Correct 132 ms 280732 KB Output is correct
8 Correct 126 ms 280560 KB Output is correct
9 Correct 124 ms 278992 KB Output is correct
10 Correct 127 ms 278860 KB Output is correct
11 Correct 125 ms 278924 KB Output is correct
12 Correct 130 ms 278888 KB Output is correct
13 Correct 133 ms 278860 KB Output is correct
14 Correct 126 ms 278916 KB Output is correct
15 Correct 129 ms 278860 KB Output is correct
16 Correct 126 ms 278892 KB Output is correct
17 Correct 124 ms 278976 KB Output is correct
18 Correct 134 ms 278868 KB Output is correct
19 Correct 131 ms 278916 KB Output is correct
20 Correct 129 ms 278824 KB Output is correct
21 Correct 124 ms 278928 KB Output is correct
22 Correct 122 ms 278852 KB Output is correct
23 Correct 129 ms 278936 KB Output is correct
24 Correct 137 ms 278836 KB Output is correct
25 Correct 125 ms 278888 KB Output is correct
26 Correct 121 ms 278964 KB Output is correct
27 Correct 130 ms 278928 KB Output is correct
28 Correct 121 ms 278940 KB Output is correct
29 Correct 123 ms 278868 KB Output is correct
30 Correct 1861 ms 671632 KB Output is correct
31 Correct 1423 ms 668504 KB Output is correct
32 Correct 1767 ms 669752 KB Output is correct
33 Correct 1666 ms 669956 KB Output is correct
34 Correct 1602 ms 676028 KB Output is correct
35 Correct 1667 ms 674396 KB Output is correct
36 Correct 954 ms 630448 KB Output is correct
37 Correct 1795 ms 676432 KB Output is correct
38 Correct 1741 ms 671964 KB Output is correct
39 Correct 1741 ms 671608 KB Output is correct
40 Correct 1472 ms 668592 KB Output is correct
41 Correct 1088 ms 658456 KB Output is correct
42 Correct 1129 ms 658836 KB Output is correct
43 Correct 1269 ms 662004 KB Output is correct
44 Correct 1231 ms 662468 KB Output is correct
45 Correct 1260 ms 666820 KB Output is correct
46 Correct 129 ms 278852 KB Output is correct
47 Correct 129 ms 278900 KB Output is correct
48 Correct 143 ms 278932 KB Output is correct
49 Correct 135 ms 278880 KB Output is correct
50 Correct 145 ms 278924 KB Output is correct
51 Correct 139 ms 278880 KB Output is correct
52 Correct 1765 ms 672160 KB Output is correct
53 Correct 1491 ms 668848 KB Output is correct
54 Correct 1720 ms 674544 KB Output is correct
55 Correct 1586 ms 670560 KB Output is correct
56 Correct 1717 ms 677548 KB Output is correct
57 Correct 1518 ms 673968 KB Output is correct
58 Correct 997 ms 630820 KB Output is correct
59 Correct 1859 ms 672268 KB Output is correct
60 Correct 1813 ms 672524 KB Output is correct
61 Correct 1782 ms 672036 KB Output is correct
62 Correct 1524 ms 668676 KB Output is correct
63 Correct 131 ms 278916 KB Output is correct
64 Correct 133 ms 278900 KB Output is correct
65 Correct 150 ms 278864 KB Output is correct
66 Correct 133 ms 278868 KB Output is correct
67 Correct 142 ms 278900 KB Output is correct
68 Correct 130 ms 278840 KB Output is correct
69 Correct 134 ms 278896 KB Output is correct
70 Correct 149 ms 278924 KB Output is correct
71 Correct 156 ms 278904 KB Output is correct
72 Correct 129 ms 279052 KB Output is correct
73 Correct 130 ms 278884 KB Output is correct
74 Correct 137 ms 278820 KB Output is correct
75 Correct 130 ms 278932 KB Output is correct
76 Correct 135 ms 278924 KB Output is correct
77 Correct 139 ms 278864 KB Output is correct
78 Correct 138 ms 278848 KB Output is correct
79 Correct 1999 ms 674008 KB Output is correct
80 Correct 3324 ms 669692 KB Output is correct
81 Correct 2382 ms 677992 KB Output is correct
82 Correct 2404 ms 676580 KB Output is correct
83 Correct 3165 ms 681748 KB Output is correct
84 Correct 3310 ms 680076 KB Output is correct
85 Correct 1191 ms 636848 KB Output is correct
86 Correct 2208 ms 676440 KB Output is correct
87 Correct 3317 ms 680064 KB Output is correct
88 Correct 2133 ms 676080 KB Output is correct
89 Correct 3856 ms 676828 KB Output is correct
90 Correct 3359 ms 650916 KB Output is correct
91 Correct 3657 ms 657816 KB Output is correct
92 Correct 142 ms 278844 KB Output is correct
93 Correct 143 ms 278920 KB Output is correct
94 Correct 138 ms 278876 KB Output is correct
95 Correct 137 ms 278880 KB Output is correct
96 Correct 141 ms 278836 KB Output is correct
97 Correct 147 ms 278888 KB Output is correct
98 Correct 2067 ms 671888 KB Output is correct
99 Correct 3085 ms 666556 KB Output is correct
100 Correct 2209 ms 669644 KB Output is correct
101 Correct 2519 ms 670772 KB Output is correct
102 Correct 3671 ms 678860 KB Output is correct
103 Correct 3194 ms 681784 KB Output is correct
104 Correct 1083 ms 637904 KB Output is correct
105 Correct 1937 ms 682556 KB Output is correct
106 Correct 3090 ms 681084 KB Output is correct
107 Correct 2122 ms 678856 KB Output is correct
108 Correct 3822 ms 677352 KB Output is correct
109 Correct 3383 ms 650936 KB Output is correct
110 Correct 3527 ms 658052 KB Output is correct
111 Correct 138 ms 278904 KB Output is correct
112 Correct 139 ms 278920 KB Output is correct
113 Correct 140 ms 278880 KB Output is correct
114 Correct 152 ms 278860 KB Output is correct
115 Correct 139 ms 278900 KB Output is correct
116 Correct 2143 ms 677132 KB Output is correct
117 Correct 3817 ms 680056 KB Output is correct
118 Correct 2241 ms 683312 KB Output is correct
119 Correct 2326 ms 679512 KB Output is correct
120 Correct 3199 ms 684148 KB Output is correct
121 Correct 3011 ms 684628 KB Output is correct
122 Correct 1118 ms 639976 KB Output is correct
123 Correct 2011 ms 680788 KB Output is correct
124 Correct 3097 ms 678272 KB Output is correct
125 Correct 2155 ms 678380 KB Output is correct
126 Execution timed out 4034 ms 679628 KB Time limit exceeded
127 Halted 0 ms 0 KB -