QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#751991#9584. 顾影自怜masttfWA 236ms96984kbC++233.9kb2024-11-15 21:30:292024-11-15 21:30:31

Judging History

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

  • [2024-11-15 21:30:31]
  • 评测
  • 测评结果:WA
  • 用时:236ms
  • 内存:96984kb
  • [2024-11-15 21:30:29]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
#define dbg(x...) \
do { \
    cout << #x << " -> "; \
    err(x); \
} while (0)

void err() {
    cout << endl << endl;
}
 
template<class T, class... Ts>
void err(T arg, Ts ... args) {
    cout << fixed << setprecision(10) << arg << ' ';
    err(args...);
}
template<class Info>
struct SegmentTree{
	int n;
	vector<Info> info;
	SegmentTree() : n(0) {}
	SegmentTree(int n_, Info v_ = Info()){
		init(n_, v_);
	}
	template<class T>
	SegmentTree(vector<T> init_){ //注意下标从1开始也就是[0] 是空
		init(init_);
	}
	void init(int n_, Info v_ = Info()){
		init(vector(n_ + 1, v_));
	}
	template<class T>
	void init(vector<T> init_){
		n = init_.size() - 1;
		info.assign(n * 4, Info());
		auto bulid = [&](auto self, int l, int r, int p) -> void{
			if(l == r){
				info[p] = init_[l];
				return ;
			}
			int mid = (l + r) >> 1;
			self(self, l, mid, p << 1);
			self(self, mid + 1, r, p << 1 | 1);
			up(p);
		};
		bulid(bulid, 1, n, 1);
	}
	void up (int p){
		info[p] = info[p << 1] + info[p << 1 | 1];
	}
	void update(int l, int r, int x, const Info &v, int p){
		if(l == r){
			info[p] = v;
			return ;
		}
		int mid = (l + r) >> 1;
		if(x <= mid)update(l, mid, x, v, p << 1);
		else update(mid + 1, r, x, v, p << 1 | 1);
		up(p);
	}
	void update(int x, const Info &v){
		update(1, n, x, v, 1);
	}
	Info rangeQuery(int l, int r, int x, int y, int p){
		if(x <= l && r <= y)return info[p];
		Info res = Info();
		int mid = (l + r) >> 1;
		if(x <= mid)res = res + rangeQuery(l, mid, x, y, p << 1);
		if(y > mid)res = res + rangeQuery(mid + 1, r, x, y, p << 1 | 1);
		return res;
	}
	Info rangeQuery(int l, int r){
		return rangeQuery(1, n, l, r, 1);
	}
	template<class F>
	int findFirst(int l, int r, int x, int y, int p, F &&check){
		if(x <= l && r <= y){
			if(!check(info[p]))return -1;
			if(l == r)return l;
		}
		int mid = (l + r) >> 1;
		if(x <= mid){
			int res = findFirst(l, mid, x, y, p << 1, check);
			if(res != -1)return res;
		}
		if(y > mid)return findFirst(mid + 1, r, x, y, p << 1 | 1, check);
		return -1;
	}
	template<class F>
	int findFirst(int l, int r, F &&check){
		return findFirst(1, n, l, r, 1, check);
	}
	template<class F>
	int findLast(int l, int r, int x, int y, int p, F &&check){
		if(x <= l && r <= y){
			if(!check(info[p]))return -1;
			if(l == r)return l;
		}
		int mid = (l + r) >> 1;
		if(y > mid){
			int res = findLast(mid + 1, r, x, y, p << 1 | 1, check);
			if(res != -1)return res;
		}
		if(x <= mid)return findLast(l, mid, x, y, p << 1, check);
		return -1;
	}
	template<class F>
	int findLast(int l, int r, F &&check){
		return findLast(1, n, l, r, 1, check);
	}
};
struct Info{
	int mx = 0, cnt = 0;	
};
Info operator+(const Info &a, const Info &b){
	Info res = Info();
	res.mx = max(a.mx, b.mx);
	if(a.mx == res.mx)res.cnt += a.cnt;
	if(b.mx == res.mx)res.cnt += b.cnt;
	return res;
}
void solve(){
    int n, k; cin >> n >> k;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i++){
    	cin >> a[i];
    }
    vector<int> suf(n + 5);
    for(int i = n; i >= 1; i--){
    	suf[i] = max(suf[i + 1], a[i]);
    }
    SegmentTree<Info> tr(n);
    for(int i = 1; i <= n; i++){
    	tr.update(i, {a[i], 1});
    }
    int ans = 0;
    for(int i = n; i >= 1; i--){
    	if(tr.rangeQuery(i, n).cnt < k)continue;
    	int sum = 0;
    	int pos = tr.findFirst(i, n, [&](const Info &t) -> bool{
    		if(t.mx == suf[i]){
    			if(sum + t.cnt < k){
    				sum += t.cnt;
    				return false;
    			}else{
    				return true;
    			}
    		}else{
    			return sum >= k;
    		}
    	});
    	assert(pos != -1);
    	ans += n - pos + 1;
    }
    cout << ans << '\n';

    return ;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int 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: 0ms
memory: 3632kb

input:

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

output:

7
0

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 236ms
memory: 96984kb

input:

1
1000000 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

500000500000

result:

ok single line: '500000500000'

Test #3:

score: -100
Wrong Answer
time: 81ms
memory: 3804kb

input:

158921
1 1
1
2 1
1 1
2 2
1 1
2 1
1 2
2 2
1 2
2 1
2 1
2 2
2 1
2 1
2 2
2 2
2 2
3 1
1 1 1
3 2
1 1 1
3 3
1 1 1
3 1
1 1 2
3 2
1 1 2
3 3
1 1 2
3 1
1 1 3
3 2
1 1 3
3 3
1 1 3
3 1
1 2 1
3 2
1 2 1
3 3
1 2 1
3 1
1 2 2
3 2
1 2 2
3 3
1 2 2
3 1
1 2 3
3 2
1 2 3
3 3
1 2 3
3 1
1 3 1
3 2
1 3 1
3 3
1 3 1
3 1
1 3 2
3 2...

output:

1
3
1
2
0
3
0
3
1
6
3
1
3
0
0
3
0
0
5
0
0
5
2
0
3
0
0
5
0
0
5
0
0
5
2
0
6
1
0
5
1
0
3
0
0
6
2
0
6
3
1
3
0
0
5
0
0
5
0
0
5
2
0
6
1
0
5
0
0
5
1
0
6
0
0
6
1
0
5
1
0
6
2
0
6
2
0
6
3
1
10
6
3
1
4
0
0
0
4
0
0
0
4
0
0
0
7
0
0
0
7
3
0
0
4
0
0
0
4
0
0
0
7
0
0
0
7
0
0
0
7
3
0
0
4
0
0
0
7
0
0
0
7
0
0
0
7
0
0
0...

result:

wrong answer 4th lines differ - expected: '3', found: '2'