#include <bits/stdc++.h>
#define fi first
#define se second
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int MAXN = 5e4 + 11, MAXQ = 1e5 + 11;
const int SQ = 800;
int l[MAXN], p[MAXN], tp[MAXN];
int ans[MAXQ];
struct DSU{
int dsu[MAXN], ans[MAXN];
unordered_map<int, int> info[MAXN];
void init(int n){
for(int i = 0; i < n; i++) dsu[i] = i;
for(int i = 0; i < n; i++) info[i].clear(), info[i][p[i]]++, ans[i] = 1;
}
int f(int x){
return dsu[x] == x ? x : dsu[x] = f(dsu[x]);
}
void g(int x, int y){
x = f(x), y = f(y);
if(x == y) return;
if(info[x].size() < info[y].size()) swap(x, y);
for(auto i : info[y]) ans[x] += info[x][i.fi] == 0 && i.se != 0, info[x][i.fi] += i.se;
info[y].clear();
dsu[y] = x;
}
} dsu;
struct qry{
int t, x, y, v, i;
};
struct edge{
int u, v, w;
bool operator< (const edge& o) const {
return w < o.w;
}
};
int n, m, q;
pair<int, int> change[MAXQ];
vector<edge> E; int eid = 0;
void solve(vector<qry> Q){
eid = 0;
int _l = Q[0].i;
dsu.init(n * m);
for(int i = 0; i < n * m; i++) tp[i] = p[i];
for(auto q : Q){
if(q.t == 1){
change[q.i] = {tp[q.x * m + q.y], q.v};
tp[q.x * m + q.y] = q.v;
}
}
vector<pair<int, int>> Q2;
for(auto q : Q){
if(q.t == 2){
Q2.push_back({q.v, q.i});
}
}
sort(Q2.begin(), Q2.end());
for(int j = 0; j < Q2.size(); j++){
while(eid < E.size() && E[eid].w <= Q2[j].fi) dsu.g(E[eid].u, E[eid].v), eid++;
int i = Q2[j].se, ri = i - _l;
int pos = dsu.f(Q[ri].x * m + Q[ri].y);
for(int k = 0; k < ri; k++){
if(Q[k].t == 1){
if(dsu.f(Q[k].x * m + Q[k].y) != pos) continue;
dsu.info[pos][change[_l + k].fi]--;
if(dsu.info[pos][change[_l + k].fi] == 0) dsu.ans[pos]--;
dsu.info[pos][change[_l + k].se]++;
if(dsu.info[pos][change[_l + k].se] == 1) dsu.ans[pos]++;
}
}
if(l[Q[ri].x * m + Q[ri].y] <= Q[ri].v){
ans[i] = dsu.ans[pos];
}
for(int k = 0; k < ri; k++){
if(Q[k].t == 1){
if(dsu.f(Q[k].x * m + Q[k].y) != pos) continue;
dsu.info[pos][change[_l + k].se]--;
if(dsu.info[pos][change[_l + k].se] == 0) dsu.ans[pos]--;
dsu.info[pos][change[_l + k].fi]++;
if(dsu.info[pos][change[_l + k].fi] == 1) dsu.ans[pos]++;
}
}
}
}
int32_t main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
cin >> n >> m >> q;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> l[i * m + j];
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m - 1; j++){
int u = i * m + j, v = i * m + j + 1;
E.push_back({u, v, max(l[u], l[v])});
}
}
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < m; j++){
int u = i * m + j, v = (i + 1) * m + j;
E.push_back({u, v, max(l[u], l[v])});
}
}
sort(E.begin(), E.end());
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> p[i * m + j];
}
}
for(int i = 0; i < q; i += SQ){
vector<qry> Q;
for(int j = i; j < min(i + SQ, q); j++){
int t, x, y, v;
cin >> t >> y >> x >> v;
x--; y--;
Q.push_back({t, x, y, v, j});
}
solve(Q);
for(auto q : Q){
if(q.t == 1) p[q.x * m + q.y] = q.v;
}
for(int j = i; j < min(i + SQ, q); j++){
if(Q[j - i].t == 2){
cout << ans[j] << endl;
}
}
}
}
Compilation message
pokemonmaster.cpp: In function 'void solve(std::vector<qry>)':
pokemonmaster.cpp:64:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int j = 0; j < Q2.size(); j++){
| ~~^~~~~~~~~~~
pokemonmaster.cpp:65:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | while(eid < E.size() && E[eid].w <= Q2[j].fi) dsu.g(E[eid].u, E[eid].v), eid++;
| ~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11724 KB |
Output is correct |
2 |
Correct |
38 ms |
11800 KB |
Output is correct |
3 |
Correct |
39 ms |
11848 KB |
Output is correct |
4 |
Correct |
29 ms |
11752 KB |
Output is correct |
5 |
Correct |
36 ms |
11724 KB |
Output is correct |
6 |
Correct |
38 ms |
11848 KB |
Output is correct |
7 |
Correct |
40 ms |
11736 KB |
Output is correct |
8 |
Correct |
35 ms |
11720 KB |
Output is correct |
9 |
Correct |
33 ms |
11724 KB |
Output is correct |
10 |
Correct |
43 ms |
11912 KB |
Output is correct |
11 |
Correct |
35 ms |
11760 KB |
Output is correct |
12 |
Correct |
45 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11596 KB |
Output is correct |
2 |
Correct |
31 ms |
11596 KB |
Output is correct |
3 |
Correct |
36 ms |
11832 KB |
Output is correct |
4 |
Correct |
33 ms |
11936 KB |
Output is correct |
5 |
Correct |
30 ms |
11976 KB |
Output is correct |
6 |
Correct |
30 ms |
11884 KB |
Output is correct |
7 |
Correct |
39 ms |
12260 KB |
Output is correct |
8 |
Correct |
42 ms |
12404 KB |
Output is correct |
9 |
Correct |
36 ms |
12232 KB |
Output is correct |
10 |
Correct |
55 ms |
13116 KB |
Output is correct |
11 |
Correct |
48 ms |
12588 KB |
Output is correct |
12 |
Correct |
56 ms |
13408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1957 ms |
12964 KB |
Output is correct |
2 |
Correct |
2145 ms |
12988 KB |
Output is correct |
3 |
Correct |
1807 ms |
12984 KB |
Output is correct |
4 |
Correct |
1791 ms |
13224 KB |
Output is correct |
5 |
Correct |
2329 ms |
13184 KB |
Output is correct |
6 |
Correct |
1881 ms |
13140 KB |
Output is correct |
7 |
Correct |
3099 ms |
13656 KB |
Output is correct |
8 |
Correct |
2969 ms |
13464 KB |
Output is correct |
9 |
Correct |
3111 ms |
13620 KB |
Output is correct |
10 |
Correct |
3230 ms |
13468 KB |
Output is correct |
11 |
Correct |
3071 ms |
13672 KB |
Output is correct |
12 |
Correct |
2902 ms |
13628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11724 KB |
Output is correct |
2 |
Correct |
38 ms |
11800 KB |
Output is correct |
3 |
Correct |
39 ms |
11848 KB |
Output is correct |
4 |
Correct |
29 ms |
11752 KB |
Output is correct |
5 |
Correct |
36 ms |
11724 KB |
Output is correct |
6 |
Correct |
38 ms |
11848 KB |
Output is correct |
7 |
Correct |
40 ms |
11736 KB |
Output is correct |
8 |
Correct |
35 ms |
11720 KB |
Output is correct |
9 |
Correct |
33 ms |
11724 KB |
Output is correct |
10 |
Correct |
43 ms |
11912 KB |
Output is correct |
11 |
Correct |
35 ms |
11760 KB |
Output is correct |
12 |
Correct |
45 ms |
11980 KB |
Output is correct |
13 |
Correct |
1468 ms |
13148 KB |
Output is correct |
14 |
Correct |
2017 ms |
13004 KB |
Output is correct |
15 |
Correct |
2086 ms |
13004 KB |
Output is correct |
16 |
Correct |
1522 ms |
12920 KB |
Output is correct |
17 |
Correct |
2033 ms |
13176 KB |
Output is correct |
18 |
Correct |
2160 ms |
13556 KB |
Output is correct |
19 |
Correct |
1685 ms |
12896 KB |
Output is correct |
20 |
Correct |
2021 ms |
12996 KB |
Output is correct |
21 |
Correct |
1710 ms |
13052 KB |
Output is correct |
22 |
Correct |
3000 ms |
13208 KB |
Output is correct |
23 |
Correct |
2072 ms |
13112 KB |
Output is correct |
24 |
Correct |
2813 ms |
13384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
11724 KB |
Output is correct |
2 |
Correct |
38 ms |
11800 KB |
Output is correct |
3 |
Correct |
39 ms |
11848 KB |
Output is correct |
4 |
Correct |
29 ms |
11752 KB |
Output is correct |
5 |
Correct |
36 ms |
11724 KB |
Output is correct |
6 |
Correct |
38 ms |
11848 KB |
Output is correct |
7 |
Correct |
40 ms |
11736 KB |
Output is correct |
8 |
Correct |
35 ms |
11720 KB |
Output is correct |
9 |
Correct |
33 ms |
11724 KB |
Output is correct |
10 |
Correct |
43 ms |
11912 KB |
Output is correct |
11 |
Correct |
35 ms |
11760 KB |
Output is correct |
12 |
Correct |
45 ms |
11980 KB |
Output is correct |
13 |
Correct |
26 ms |
11596 KB |
Output is correct |
14 |
Correct |
31 ms |
11596 KB |
Output is correct |
15 |
Correct |
36 ms |
11832 KB |
Output is correct |
16 |
Correct |
33 ms |
11936 KB |
Output is correct |
17 |
Correct |
30 ms |
11976 KB |
Output is correct |
18 |
Correct |
30 ms |
11884 KB |
Output is correct |
19 |
Correct |
39 ms |
12260 KB |
Output is correct |
20 |
Correct |
42 ms |
12404 KB |
Output is correct |
21 |
Correct |
36 ms |
12232 KB |
Output is correct |
22 |
Correct |
55 ms |
13116 KB |
Output is correct |
23 |
Correct |
48 ms |
12588 KB |
Output is correct |
24 |
Correct |
56 ms |
13408 KB |
Output is correct |
25 |
Correct |
1957 ms |
12964 KB |
Output is correct |
26 |
Correct |
2145 ms |
12988 KB |
Output is correct |
27 |
Correct |
1807 ms |
12984 KB |
Output is correct |
28 |
Correct |
1791 ms |
13224 KB |
Output is correct |
29 |
Correct |
2329 ms |
13184 KB |
Output is correct |
30 |
Correct |
1881 ms |
13140 KB |
Output is correct |
31 |
Correct |
3099 ms |
13656 KB |
Output is correct |
32 |
Correct |
2969 ms |
13464 KB |
Output is correct |
33 |
Correct |
3111 ms |
13620 KB |
Output is correct |
34 |
Correct |
3230 ms |
13468 KB |
Output is correct |
35 |
Correct |
3071 ms |
13672 KB |
Output is correct |
36 |
Correct |
2902 ms |
13628 KB |
Output is correct |
37 |
Correct |
1468 ms |
13148 KB |
Output is correct |
38 |
Correct |
2017 ms |
13004 KB |
Output is correct |
39 |
Correct |
2086 ms |
13004 KB |
Output is correct |
40 |
Correct |
1522 ms |
12920 KB |
Output is correct |
41 |
Correct |
2033 ms |
13176 KB |
Output is correct |
42 |
Correct |
2160 ms |
13556 KB |
Output is correct |
43 |
Correct |
1685 ms |
12896 KB |
Output is correct |
44 |
Correct |
2021 ms |
12996 KB |
Output is correct |
45 |
Correct |
1710 ms |
13052 KB |
Output is correct |
46 |
Correct |
3000 ms |
13208 KB |
Output is correct |
47 |
Correct |
2072 ms |
13112 KB |
Output is correct |
48 |
Correct |
2813 ms |
13384 KB |
Output is correct |
49 |
Correct |
1958 ms |
13000 KB |
Output is correct |
50 |
Correct |
2106 ms |
12992 KB |
Output is correct |
51 |
Correct |
2242 ms |
13064 KB |
Output is correct |
52 |
Correct |
2040 ms |
13164 KB |
Output is correct |
53 |
Correct |
2656 ms |
13368 KB |
Output is correct |
54 |
Correct |
2068 ms |
13596 KB |
Output is correct |
55 |
Correct |
3366 ms |
13664 KB |
Output is correct |
56 |
Correct |
3494 ms |
13916 KB |
Output is correct |
57 |
Correct |
3028 ms |
13560 KB |
Output is correct |
58 |
Correct |
4114 ms |
14348 KB |
Output is correct |
59 |
Correct |
3690 ms |
14168 KB |
Output is correct |
60 |
Correct |
4562 ms |
14724 KB |
Output is correct |