QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#575547#9313. Make MaxHSJasdfghjklWA 367ms7296kbC++172.0kb2024-09-19 15:16:542024-09-19 15:16:54

Judging History

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

  • [2024-09-19 15:16:54]
  • 评测
  • 测评结果:WA
  • 用时:367ms
  • 内存:7296kb
  • [2024-09-19 15:16:54]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
using uInt = unsigned long long;
#define lowbit(x)  ((x) & - (x))
using ll = long long;
void update(vector<int>& tr, int x, ll v) {      // 单点修改(加)
	for (; x < tr.size(); x += lowbit(x))
		tr[x] += v;
}
ll qr(vector<int>& tr, int x) {                     //前缀查询
	ll sum = 0;
	for (x; x >= 1; x -= lowbit(x)) sum += tr[x];
	return sum;
}
ll query(vector<int>& tr, int l, int r) {            //区间查询
	return qr(tr, r) - qr(tr, l - 1);
}
bool cmp(pair<int, int>& p1,pair<int, int>& p2) {
	if (p1.first != p2.first)return p1.first > p2.first;
	return p1.second < p2.second;
}
void solve() {
	int n; cin >> n;
	vector<int>v(n + 1),cnt;
	cnt.push_back(0);
	for (int u = 1; u <= n; ++u)cin >> v[u], cnt.push_back(v[u]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end())-v.begin());	
	for (int u = 1; u <= n; ++u)
		cnt[u] = lower_bound(v.begin(), v.end(), cnt[u]) - v.begin();
	vector<pair<int, int>>vp(n + 1);
	for (int u = 1; u <= n; ++u)vp[u].first = cnt[u], vp[u].second = u;
	sort(vp.begin()+1, vp.end(),cmp);
	vector<int>is(n+1);
	Int ans = 0;
	for (int u = 1; u <= n; ++u) {
		int l =0, r = vp[u].second;
		while (l + 1 < r) {
			int mid = l + r >> 1;
			int sum = query(is, mid, vp[u].second);
			if (sum>1) {
				l = mid + 1;
			}
			else if(sum==0) {
				r = mid;
			}
			else {
				l = mid;
			}
		}
		ans += (vp[u].second - 1 - l);
		if (u==n|| vp[u].first != vp[u + 1].first) {
			if (query(is, vp[u].second, n) != 0) {
				int l = vp[u].second + 1, r = n + 1;
				while (l + 1 < r) {
					int mid = l + r >> 1;
					int sum = query(is, vp[u].second, mid);
					if (sum > 1) {
						r = mid;
					}
					else if (sum == 0) {
						l = mid + 1;
					}
					else {
						l = mid;
					}
				}
				ans += (l-1-vp[u].second);
			}
		}
		update(is, vp[u].second, 1);
	}
	cout << ans << endl;
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	for(int i=1,n=(cin>>n,n);i<=n;++i)
		solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3840kb

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Wrong Answer
time: 367ms
memory: 7296kb

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:

5884649
5635095

result:

wrong answer 1st numbers differ - expected: '4084978', found: '5884649'