#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
int m, n, a[4][200010];
vector<int> Ty[4*200010], tree[4*200010];
vector<pair<int, pair<int,int> > > Vq;
void Down(int h, int tl, int tr, int x, int y) {
if(tr < x || x < tl) return;
Ty[h].push_back(y);
if(tl == tr) return;
int mid = (tl + tr) / 2;
Down(h*2, tl, mid, x, y); Down(h*2+1, mid+1, tr, x, y);
}
void build(int h, int tl, int tr) {
sort(all(Ty[h]));
Ty[h].erase(unique(all(Ty[h])), Ty[h].end());
tree[h] = vector<int>(4*sz(Ty[h]));
if(tl == tr) return;
int mid = (tl + tr) / 2;
build(h*2, tl, mid); build(h*2+1, mid+1, tr);
}
void update_y(int h1, int h2, int tl, int tr, int y, int v) {
if(tr < y || y < tl) return;
if(tl == tr) { tree[h1][h2] = v; return; }
int mid = (tl + tr) / 2;
update_y(h1, h2*2, tl, mid, y, v);
update_y(h1, h2*2+1, mid+1, tr, y, v);
tree[h1][h2] = max(tree[h1][h2*2], tree[h1][h2*2+1]);
}
void update(int h, int tl, int tr, int x, int y, int v) {
if(tr < x || x < tl) return;
update_y(h,1,0,sz(Ty[h])-1,lower_bound(all(Ty[h]),y)-Ty[h].begin(),v);
if(tl == tr) return;
int mid = (tl + tr) / 2;
update(h*2, tl, mid, x,y,v);
update(h*2+1, mid+1, tr, x,y,v);
}
int query_y(int h1, int h2, int tl, int tr, int l, int r) {
if(l>r) return 0;
if(tr < l || r < tl) return 0;
if(l <= tl && tr <= r) return tree[h1][h2];
int mid = (tl + tr) / 2;
return max(query_y(h1,h2*2,tl,mid,l,r), query_y(h1,h2*2+1,mid+1,tr,l,r));
}
int query(int h, int tl, int tr, int x1, int y1, int x2, int y2) {
if(x1>x2||y1>y2) return 0;
if(tr < x1 || x2 < tl) return 0;
if(x1 <= tl && tr <= x2) return
query_y(h,1,0,sz(Ty[h])-1,lower_bound(all(Ty[h]),y1)-Ty[h].begin(),lower_bound(all(Ty[h]),y2+1)-Ty[h].begin()-1);
int mid = (tl + tr) / 2;
return max(query(h*2, tl, mid, x1,y1,x2,y2),
query(h*2+1, mid+1, tr, x1,y1,x2,y2));
}
int main() {
//clock_t start = clock();
scanf("%d%d", &m, &n);
for(int i=1; i<=m; i++) {
vector<int> Vc;
for(int j=1; j<=n; j++) {
scanf("%d", &a[i][j]);
Vc.push_back(a[i][j]);
}
sort(all(Vc));
for(int j=1; j<=n; j++) {
int c = lower_bound(all(Vc), a[i][j]) - Vc.begin();
a[i][j] = c;
}
}
for(int i=1; i<=n; i++) {
int x = a[2][i], y = a[m == 3 ? 3 : 1][i];
Vq.push_back({a[1][i], {x, y}});
Down(1,0,n-1,x,y);
}
build(1,0,n-1);
sort(all(Vq));
int res = 0;
for(auto& it : Vq) {
int x=it.second.first,y=it.second.second,q=query(1,0,n-1,0,0,x-1,y-1);
//printf("x:%d, y:%d, q:%d\n",x,y,q+1);
update(1,0,n-1,x,y,q+1);
res = max(res, q+1);
}
printf("%d", res);
//printf("\n%lu", clock() - start);
return 0;
}
Compilation message
matrix.cpp: In function 'int main()':
matrix.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &m, &n);
~~~~~^~~~~~~~~~~~~~~~
matrix.cpp:63:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
41976 KB |
Output is correct |
2 |
Correct |
93 ms |
42084 KB |
Output is correct |
3 |
Correct |
99 ms |
42160 KB |
Output is correct |
4 |
Correct |
89 ms |
42364 KB |
Output is correct |
5 |
Correct |
114 ms |
42364 KB |
Output is correct |
6 |
Correct |
88 ms |
42364 KB |
Output is correct |
7 |
Correct |
87 ms |
42396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
42396 KB |
Output is correct |
2 |
Correct |
89 ms |
42616 KB |
Output is correct |
3 |
Correct |
122 ms |
43052 KB |
Output is correct |
4 |
Correct |
138 ms |
43396 KB |
Output is correct |
5 |
Correct |
158 ms |
43708 KB |
Output is correct |
6 |
Correct |
114 ms |
43860 KB |
Output is correct |
7 |
Correct |
149 ms |
44248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
41976 KB |
Output is correct |
2 |
Correct |
93 ms |
42084 KB |
Output is correct |
3 |
Correct |
99 ms |
42160 KB |
Output is correct |
4 |
Correct |
89 ms |
42364 KB |
Output is correct |
5 |
Correct |
114 ms |
42364 KB |
Output is correct |
6 |
Correct |
88 ms |
42364 KB |
Output is correct |
7 |
Correct |
87 ms |
42396 KB |
Output is correct |
8 |
Correct |
3469 ms |
136012 KB |
Output is correct |
9 |
Correct |
2946 ms |
136012 KB |
Output is correct |
10 |
Correct |
2044 ms |
136012 KB |
Output is correct |
11 |
Correct |
1840 ms |
136088 KB |
Output is correct |
12 |
Correct |
2486 ms |
136128 KB |
Output is correct |
13 |
Correct |
1880 ms |
136128 KB |
Output is correct |
14 |
Correct |
2196 ms |
136128 KB |
Output is correct |
15 |
Correct |
1834 ms |
136128 KB |
Output is correct |
16 |
Correct |
1989 ms |
136128 KB |
Output is correct |
17 |
Correct |
2017 ms |
136128 KB |
Output is correct |
18 |
Correct |
1937 ms |
136128 KB |
Output is correct |
19 |
Correct |
2126 ms |
136128 KB |
Output is correct |
20 |
Correct |
3679 ms |
136128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
42396 KB |
Output is correct |
2 |
Correct |
89 ms |
42616 KB |
Output is correct |
3 |
Correct |
122 ms |
43052 KB |
Output is correct |
4 |
Correct |
138 ms |
43396 KB |
Output is correct |
5 |
Correct |
158 ms |
43708 KB |
Output is correct |
6 |
Correct |
114 ms |
43860 KB |
Output is correct |
7 |
Correct |
149 ms |
44248 KB |
Output is correct |
8 |
Correct |
2350 ms |
142468 KB |
Output is correct |
9 |
Correct |
3256 ms |
148344 KB |
Output is correct |
10 |
Correct |
2060 ms |
154140 KB |
Output is correct |
11 |
Correct |
2009 ms |
160136 KB |
Output is correct |
12 |
Correct |
3148 ms |
165804 KB |
Output is correct |
13 |
Correct |
2201 ms |
171676 KB |
Output is correct |
14 |
Correct |
3504 ms |
177296 KB |
Output is correct |
15 |
Correct |
2294 ms |
183104 KB |
Output is correct |
16 |
Correct |
2243 ms |
188784 KB |
Output is correct |
17 |
Correct |
3725 ms |
193992 KB |
Output is correct |
18 |
Execution timed out |
4019 ms |
199872 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |