QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#383097#7685. Barkley IILeonard#WA 272ms7096kbC++202.9kb2024-04-08 21:52:202024-04-08 21:52:20

Judging History

你现在查看的是最新测评结果

  • [2024-04-08 21:52:20]
  • 评测
  • 测评结果:WA
  • 用时:272ms
  • 内存:7096kb
  • [2024-04-08 21:52:20]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int N = 5e5 + 10;
struct node {
	int l, r, v;
#define ls u << 1
#define rs u << 1 | 1
} tr[N << 2];
void pushup(int u) {
	tr[u].v = tr[ls].v + tr[rs].v;
}
void build(int u, int l, int r) {
	tr[u] = {l, r};
	if(l == r) {
		return ;
	}
	int mid = l + r >> 1;
	build(ls, l, mid), build(rs, mid + 1, r);
	pushup(u);
}
int query(int u, int l, int r) {
	if(tr[u].l >= l && tr[u].r <= r) return tr[u].v;
	int mid = tr[u].l + tr[u].r >> 1;
	int v = 0;
	if(l <= mid) v = v + query(ls, l, r);
	if(r > mid) v = v + query(rs, l, r);
	return v;
}
void modify(int u, int x, int v) {
	if(tr[u].l == x && tr[u].r == x) tr[u].v = v;
	else {
		int mid = tr[u].l + tr[u].r >> 1;
		if(mid >= x) modify(ls, x, v);
		else modify(rs, x, v);
		pushup(u);
	}
}
int n, m, a[N];
void solve() {
	cin >> n >> m;
    map<int, vector<int>> g;
	//vector<vector<int>> g(m + 2);
	for(int i = 1; i <= m + 1; i++) {
		g[i].push_back(0);
	}
    vector<vector<array<int, 2>>> q(n + 2);
    map<int, set<int>> s;
	//vector<set<int>> s(m + 2);
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		g[a[i]].push_back(i);
		s[a[i]].insert(i);
	}
	for(int i = 1; i <= m + 1; i++) {
        if(!s.count(i)) {
            q[1].push_back({n, i});
            break;
        }
    }
	build(1, 1, n);
    for(auto v : s) {
        auto it = *(v.second).begin();
        modify(1, it, 1);
        (v.second).erase(it);
    }
	// for(int i = 1; i <= m; i++) {
	// 	if(s[i].empty()) continue;
	// 	auto it = *s[i].begin();
	// 	modify(1, it, 1);
		
	// 	s[i].erase(it);
	// }
	for(int i = 1; i <= m + 1; i++) {
		g[i].push_back(n + 1);
	}
    for(auto [u, v] : g) {
        v.push_back(n + 1);
        int lst = 0;
        for(int j = 0; j + 1 < v.size(); j++) {
            q[lst + 1].push_back({v[j] - 1, u});
            lst = v[j];
        }
    }
	
	// for(int i = 1; i <= m + 1; i++) {
	// 	//cout << i << ": ";
	// 	//for(int j : g[i]) cout << j << ' ';
	// 	//cout << '\n';
	// 	for(int j = 0; j + 1 < g[i].size(); j++) {
	// 		//cout << "id = " << j << " " << g[i][j] + 1 << ' ' << g[i][j + 1] - 1 << "  ";
	// 		q[g[i][j] + 1].push_back({g[i][j + 1] - 1, i});
	// 	}
	// }
	int ans = -1e9;
    bool nice = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 0; j < q[i].size(); j++) {
			int l = i, r = q[i][j][0], y = q[i][j][1];
            
			//cout << l << ' ' << r << ' ' << y << '\n';
			if(l > r) continue;
			int cnt = query(1, l, r);
			ans = max(cnt - y, ans);
			//cout << l << ' ' << r << ' ' << y << ' ' << cnt - y << '\n';
			
		}
		if(!s[a[i]].empty()) {
			auto it = *s[a[i]].begin();
			modify(1, it, 1);
			//cout << it << '\n';
			s[a[i]].erase(it);
		}
	}
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t;
	t = 1;
	cin >> t;
	while(t--) {
		solve();
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 7096kb

input:

2
5 4
1 2 2 3 4
5 10000
5 2 3 4 1

output:

2
3

result:

ok 2 number(s): "2 3"

Test #2:

score: -100
Wrong Answer
time: 272ms
memory: 5628kb

input:

50000
10 19
12 6 1 12 11 15 4 1 13 18
10 8
8 7 6 7 6 2 2 3 4 8
10 6
3 2 6 6 5 2 3 4 5 6
10 11
6 3 7 9 2 1 2 10 10 4
10 6
6 1 2 6 1 1 3 4 2 1
10 9
8 5 3 9 1 7 5 5 1 1
10 5
1 4 3 2 5 4 5 3 5 2
10 14
3 8 12 10 4 2 3 13 7 3
10 14
5 5 12 2 8 1 13 9 8 5
10 7
5 5 6 6 1 5 3 7 3 4
10 7
5 1 4 6 1 6 4 3 7 5
10...

output:

6
5
4
4
1
4
3
7
4
4
4
5
2
3
6
6
7
5
7
6
5
5
6
1
6
8
7
2
5
5
5
2
2
3
4
5
3
3
7
3
2
5
6
1
2
5
3
2
3
8
6
6
5
7
4
4
5
4
6
6
6
3
7
3
6
1
3
7
6
6
6
7
4
3
3
3
4
6
2
4
6
6
4
5
5
9
4
4
7
5
3
5
2
1
5
6
6
8
4
3
4
5
5
5
7
7
3
2
6
5
3
5
4
4
5
6
6
5
6
7
7
4
5
7
4
7
3
7
6
6
6
5
4
2
5
4
2
3
6
5
1
6
5
5
4
3
5
6
6
6
...

result:

wrong answer 5th numbers differ - expected: '2', found: '1'