#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int M = 1e5 + 100;
const int N = M;
const int SZ = 800;
int t[N], x[N], l[N], r[N], n, m, change[M], L[M], P[M], sz[N], y[N], p[N], C[N], ans[N];
vector <pair <int, int>> save[M];
int cov(int a, int b) {
return (a - 1) * m + b;
}
int get(int a) {
while (p[a])
a = p[a];
return a;
}
int cc[N];
map <int, int> mp[N];
void unite(int a, int b) {
a = get(a), b = get(b);
if (a == b)
return;
if (sz[a] < sz[b])
swap(a, b);
sz[a] += sz[b];
p[b] = a;
if (cc[a] < cc[b]) {
swap(cc[a], cc[b]);
swap(mp[a], mp[b]);
}
for (auto &x : mp[b]) {
int &t = mp[a][x.f];
if (t == 0 && x.s)
cc[a]++;
t += x.s;
}
}
vector <vector <int>> action;
void upd(int c, int x) {
c = get(c);
int &t = mp[c][x];
if (t == 0)
cc[c]++;
action.pb({c, x, t});
t++;
}
void rollback() {
while (sz(action)) {
auto t = action.back();
action.pop_back();
if (t[2] == 0) {
cc[t[0]]--;
}
mp[t[0]][t[1]] = t[2];
}
}
ll solve() {
mem(L, 0x3f);
mem(P, 0x3f);
int q;
cin >> n >> m >> q;
int nm = n * m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> L[cov(i, j)];
vector <vector <int>> edge;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i < n)
edge.pb({max(L[cov(i + 1, j)], L[cov(i, j)]), cov(i + 1, j), cov(i, j)});
if (j < m)
edge.pb({max(L[cov(i, j + 1)], L[cov(i, j)]), cov(i, j + 1), cov(i, j)});
}
}
sort(all(edge), [&](vector <int> a, vector <int> b) { return a[0] < b[0]; });
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> P[cov(i, j)];
for (int i = 1; i <= q; i++) {
cin >> t[i] >> y[i] >> x[i] >> r[i];
}
for (int i = 1; i <= q; i += SZ) {
int X = i, Y = min(q, i + SZ - 1);
vector <int> ask, up;
for (int j = X; j <= Y; j++) {
if (t[j] == 2) {
ask.pb(j);
} else {
int id = cov(x[j], y[j]);
if (change[id] == 0) {
up.pb(id);
}
change[id] = 1;
}
}
for (int j = X; j <= Y; j++) {
if (t[j] == 1) {
P[cov(x[j], y[j])] = r[j];
} else {
save[j - X].clear();
for (auto t : up) {
save[j - X].pb({t, P[t]});
}
}
}
sort(all(ask), [&](int a, int b) { return r[a] < r[b]; });
for (int j = 1; j <= nm; j++) {
mp[j].clear();
if (change[j] == 0) {
cc[j] = 1;
mp[j][P[j]]++;
sz[j] = 1;
} else {
sz[j] = 0;
cc[j] = 0;
}
p[j] = 0;
}
int j = 0;
for (auto id : ask) {
while (j < sz(edge) && r[id] >= edge[j][0]) {
unite(edge[j][1], edge[j][2]);
j++;
}
for (auto t : save[id - X]) {
upd(t.f, t.s);
}
if (r[id] >= L[cov(x[id], y[id])])
ans[id] = cc[get(cov(x[id], y[id]))];
rollback();
}
for (auto id : up)
change[id] = 0;
}
for (int i = 1; i <= q; i++) {
if (t[i] == 2) {
cout << ans[i] << "\n";
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
//cout << solve() << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
16708 KB |
Output is correct |
2 |
Correct |
65 ms |
17736 KB |
Output is correct |
3 |
Correct |
70 ms |
17352 KB |
Output is correct |
4 |
Correct |
56 ms |
17152 KB |
Output is correct |
5 |
Correct |
64 ms |
17672 KB |
Output is correct |
6 |
Correct |
61 ms |
16448 KB |
Output is correct |
7 |
Correct |
56 ms |
17664 KB |
Output is correct |
8 |
Correct |
62 ms |
17672 KB |
Output is correct |
9 |
Correct |
57 ms |
17668 KB |
Output is correct |
10 |
Correct |
74 ms |
17920 KB |
Output is correct |
11 |
Correct |
68 ms |
17668 KB |
Output is correct |
12 |
Correct |
87 ms |
18432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
15872 KB |
Output is correct |
2 |
Correct |
58 ms |
16180 KB |
Output is correct |
3 |
Correct |
59 ms |
16388 KB |
Output is correct |
4 |
Correct |
62 ms |
18428 KB |
Output is correct |
5 |
Correct |
65 ms |
17664 KB |
Output is correct |
6 |
Correct |
67 ms |
17920 KB |
Output is correct |
7 |
Correct |
100 ms |
18688 KB |
Output is correct |
8 |
Correct |
95 ms |
20480 KB |
Output is correct |
9 |
Correct |
89 ms |
18820 KB |
Output is correct |
10 |
Correct |
114 ms |
22008 KB |
Output is correct |
11 |
Correct |
99 ms |
20736 KB |
Output is correct |
12 |
Correct |
122 ms |
23216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
650 ms |
18112 KB |
Output is correct |
2 |
Correct |
1475 ms |
20736 KB |
Output is correct |
3 |
Correct |
1058 ms |
23812 KB |
Output is correct |
4 |
Correct |
1081 ms |
25680 KB |
Output is correct |
5 |
Correct |
1568 ms |
21828 KB |
Output is correct |
6 |
Correct |
675 ms |
20740 KB |
Output is correct |
7 |
Correct |
2197 ms |
23552 KB |
Output is correct |
8 |
Correct |
2200 ms |
23320 KB |
Output is correct |
9 |
Correct |
2224 ms |
23556 KB |
Output is correct |
10 |
Correct |
2294 ms |
23844 KB |
Output is correct |
11 |
Correct |
2164 ms |
23576 KB |
Output is correct |
12 |
Correct |
2194 ms |
23296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
16708 KB |
Output is correct |
2 |
Correct |
65 ms |
17736 KB |
Output is correct |
3 |
Correct |
70 ms |
17352 KB |
Output is correct |
4 |
Correct |
56 ms |
17152 KB |
Output is correct |
5 |
Correct |
64 ms |
17672 KB |
Output is correct |
6 |
Correct |
61 ms |
16448 KB |
Output is correct |
7 |
Correct |
56 ms |
17664 KB |
Output is correct |
8 |
Correct |
62 ms |
17672 KB |
Output is correct |
9 |
Correct |
57 ms |
17668 KB |
Output is correct |
10 |
Correct |
74 ms |
17920 KB |
Output is correct |
11 |
Correct |
68 ms |
17668 KB |
Output is correct |
12 |
Correct |
87 ms |
18432 KB |
Output is correct |
13 |
Correct |
656 ms |
18144 KB |
Output is correct |
14 |
Correct |
2157 ms |
20488 KB |
Output is correct |
15 |
Correct |
3123 ms |
23868 KB |
Output is correct |
16 |
Correct |
1116 ms |
23624 KB |
Output is correct |
17 |
Correct |
2095 ms |
20420 KB |
Output is correct |
18 |
Correct |
1427 ms |
18732 KB |
Output is correct |
19 |
Correct |
1221 ms |
20488 KB |
Output is correct |
20 |
Correct |
1939 ms |
20548 KB |
Output is correct |
21 |
Correct |
1409 ms |
20744 KB |
Output is correct |
22 |
Correct |
3790 ms |
20664 KB |
Output is correct |
23 |
Correct |
2355 ms |
20424 KB |
Output is correct |
24 |
Correct |
4843 ms |
21584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
16708 KB |
Output is correct |
2 |
Correct |
65 ms |
17736 KB |
Output is correct |
3 |
Correct |
70 ms |
17352 KB |
Output is correct |
4 |
Correct |
56 ms |
17152 KB |
Output is correct |
5 |
Correct |
64 ms |
17672 KB |
Output is correct |
6 |
Correct |
61 ms |
16448 KB |
Output is correct |
7 |
Correct |
56 ms |
17664 KB |
Output is correct |
8 |
Correct |
62 ms |
17672 KB |
Output is correct |
9 |
Correct |
57 ms |
17668 KB |
Output is correct |
10 |
Correct |
74 ms |
17920 KB |
Output is correct |
11 |
Correct |
68 ms |
17668 KB |
Output is correct |
12 |
Correct |
87 ms |
18432 KB |
Output is correct |
13 |
Correct |
55 ms |
15872 KB |
Output is correct |
14 |
Correct |
58 ms |
16180 KB |
Output is correct |
15 |
Correct |
59 ms |
16388 KB |
Output is correct |
16 |
Correct |
62 ms |
18428 KB |
Output is correct |
17 |
Correct |
65 ms |
17664 KB |
Output is correct |
18 |
Correct |
67 ms |
17920 KB |
Output is correct |
19 |
Correct |
100 ms |
18688 KB |
Output is correct |
20 |
Correct |
95 ms |
20480 KB |
Output is correct |
21 |
Correct |
89 ms |
18820 KB |
Output is correct |
22 |
Correct |
114 ms |
22008 KB |
Output is correct |
23 |
Correct |
99 ms |
20736 KB |
Output is correct |
24 |
Correct |
122 ms |
23216 KB |
Output is correct |
25 |
Correct |
650 ms |
18112 KB |
Output is correct |
26 |
Correct |
1475 ms |
20736 KB |
Output is correct |
27 |
Correct |
1058 ms |
23812 KB |
Output is correct |
28 |
Correct |
1081 ms |
25680 KB |
Output is correct |
29 |
Correct |
1568 ms |
21828 KB |
Output is correct |
30 |
Correct |
675 ms |
20740 KB |
Output is correct |
31 |
Correct |
2197 ms |
23552 KB |
Output is correct |
32 |
Correct |
2200 ms |
23320 KB |
Output is correct |
33 |
Correct |
2224 ms |
23556 KB |
Output is correct |
34 |
Correct |
2294 ms |
23844 KB |
Output is correct |
35 |
Correct |
2164 ms |
23576 KB |
Output is correct |
36 |
Correct |
2194 ms |
23296 KB |
Output is correct |
37 |
Correct |
656 ms |
18144 KB |
Output is correct |
38 |
Correct |
2157 ms |
20488 KB |
Output is correct |
39 |
Correct |
3123 ms |
23868 KB |
Output is correct |
40 |
Correct |
1116 ms |
23624 KB |
Output is correct |
41 |
Correct |
2095 ms |
20420 KB |
Output is correct |
42 |
Correct |
1427 ms |
18732 KB |
Output is correct |
43 |
Correct |
1221 ms |
20488 KB |
Output is correct |
44 |
Correct |
1939 ms |
20548 KB |
Output is correct |
45 |
Correct |
1409 ms |
20744 KB |
Output is correct |
46 |
Correct |
3790 ms |
20664 KB |
Output is correct |
47 |
Correct |
2355 ms |
20424 KB |
Output is correct |
48 |
Correct |
4843 ms |
21584 KB |
Output is correct |
49 |
Correct |
723 ms |
18176 KB |
Output is correct |
50 |
Correct |
2210 ms |
20684 KB |
Output is correct |
51 |
Correct |
3155 ms |
24376 KB |
Output is correct |
52 |
Correct |
1216 ms |
25612 KB |
Output is correct |
53 |
Correct |
2246 ms |
22008 KB |
Output is correct |
54 |
Correct |
1549 ms |
19784 KB |
Output is correct |
55 |
Correct |
1923 ms |
23228 KB |
Output is correct |
56 |
Correct |
3818 ms |
24780 KB |
Output is correct |
57 |
Correct |
2214 ms |
23288 KB |
Output is correct |
58 |
Execution timed out |
5024 ms |
26616 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |