답안 #898127

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898127 2024-01-04T10:45:12 Z Litusiano Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
30 / 100
1408 ms 207064 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'

struct MST{
	int n;
	vector<vector<int>> seg;
	void init(int n1){
		n = 1;
		while(n < n1) n*=2;
		seg.assign(2*n, vector<int>());
	}
 
	void merge(int i, int j, int k){
		int l = 0;
		int r = 0;
		while(l < seg[j].size() && r < seg[k].size()){
			if(seg[j][l] < seg[k][r]){
				seg[i].push_back(seg[j][l]); l++;
			}
			else{
				seg[i].push_back(seg[k][r]); r++;
			}
		}
		while(l < seg[j].size()){
			seg[i].push_back(seg[j][l]); l++;
		}
		while(r < seg[k].size()){
			seg[i].push_back(seg[k][r]); r++;
		}
	}
 
	void build(vector<int> v){
		for(int i = n; i < 2*n; i++){
			if(i-n >= v.size()) seg[i].push_back(0);
			else seg[i].push_back(v[i-n]);
		}
		for(int i = n-1; i>=0; i--){
			merge(i,2*i, 2*i+1);
		}
	}
 
	int query(int ql, int qr, int v, int k, int l, int r){
		if(r < ql || l > qr) return 0;
		if(ql <= l && r<= qr){
			auto it = lower_bound(seg[k].begin(),seg[k].end(), v);
			if(it == seg[k].begin()) return 0;
			return *(it-1);
		}
		int m = (l+r) / 2;
		int x = query(ql, qr, v, 2*k, l ,m);
		int y = query(ql, qr, v, 2*k+1, m+1 ,r);
		return max(x,y);
	}
 
	int query(int ql, int qr, int v){
		return query(ql, qr, v, 1, 0, n-1);
	}
};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
  int n,q; cin>>n>>q;
  vector<int> v(n); for(int& i : v) cin>>i;
  vector<int> pre(n+1);
  vector<int> inv;
 	for(int i = 1; i<n; i++){
  	pre[i+1] = pre[i] + (v[i] < v[i-1]); 
  	if(v[i] < v[i-1]) inv.push_back(i-1);
  }
  MST seg; seg.init(n); seg.build(v);
  while(q--){
  	int l,r,k; cin>>l>>r>>k;
  	if(n > 5000){
  		if(pre[r] - pre[l] == 0) cout<<1<<endl;
  		else cout<<0<<endl;
  	}
  	else{
  		l--; r--;
  		int mx = 0;
  		int idx = lower_bound(inv.begin(),inv.end(),l) - inv.begin();
  		// cerr<<"INV ";
	  	for(int x = idx; x < inv.size(); x++){
	  		// cerr<<inv[x]<<" "; 
	  		if(inv[x] >= r) break;
	  		int i = inv[x];
	  		// cerr<<i+1<<" "<<r<<endl;
	  		// cerr<<"QUERY: "<<i<<" "<< seg.query(i+1,r,v[i])<<endl;
	  		int y = seg.query(i+1,r,v[i]);
	  		if(!y) continue;
	  		mx = max(mx, v[i] + y);
	  	}
	  	// cerr<<endl;
	  	if(mx > k) cout<<0<<endl;
	  	else cout<<1<<endl;
  	}
  	
  	
  }
}

Compilation message

sortbooks.cpp: In member function 'void MST::merge(int, int, int)':
sortbooks.cpp:20:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |   while(l < seg[j].size() && r < seg[k].size()){
      |         ~~^~~~~~~~~~~~~~~
sortbooks.cpp:20:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |   while(l < seg[j].size() && r < seg[k].size()){
      |                              ~~^~~~~~~~~~~~~~~
sortbooks.cpp:28:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |   while(l < seg[j].size()){
      |         ~~^~~~~~~~~~~~~~~
sortbooks.cpp:31:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |   while(r < seg[k].size()){
      |         ~~^~~~~~~~~~~~~~~
sortbooks.cpp: In member function 'void MST::build(std::vector<int>)':
sortbooks.cpp:38:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |    if(i-n >= v.size()) seg[i].push_back(0);
      |       ~~~~^~~~~~~~~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:87:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     for(int x = idx; x < inv.size(); x++){
      |                      ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 496 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 7 ms 348 KB Output is correct
7 Correct 7 ms 348 KB Output is correct
8 Correct 7 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 496 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 7 ms 348 KB Output is correct
7 Correct 7 ms 348 KB Output is correct
8 Correct 7 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 164 ms 852 KB Output is correct
12 Correct 722 ms 1876 KB Output is correct
13 Correct 811 ms 1880 KB Output is correct
14 Correct 1324 ms 1800 KB Output is correct
15 Correct 1408 ms 2136 KB Output is correct
16 Correct 944 ms 2056 KB Output is correct
17 Correct 3 ms 1116 KB Output is correct
18 Correct 5 ms 1816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 657 ms 207064 KB Output is correct
2 Correct 650 ms 206864 KB Output is correct
3 Correct 662 ms 207048 KB Output is correct
4 Correct 655 ms 206808 KB Output is correct
5 Correct 550 ms 204684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 62 ms 24012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 496 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 7 ms 348 KB Output is correct
7 Correct 7 ms 348 KB Output is correct
8 Correct 7 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 164 ms 852 KB Output is correct
12 Correct 722 ms 1876 KB Output is correct
13 Correct 811 ms 1880 KB Output is correct
14 Correct 1324 ms 1800 KB Output is correct
15 Correct 1408 ms 2136 KB Output is correct
16 Correct 944 ms 2056 KB Output is correct
17 Correct 3 ms 1116 KB Output is correct
18 Correct 5 ms 1816 KB Output is correct
19 Incorrect 141 ms 49152 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 496 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 7 ms 348 KB Output is correct
7 Correct 7 ms 348 KB Output is correct
8 Correct 7 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 164 ms 852 KB Output is correct
12 Correct 722 ms 1876 KB Output is correct
13 Correct 811 ms 1880 KB Output is correct
14 Correct 1324 ms 1800 KB Output is correct
15 Correct 1408 ms 2136 KB Output is correct
16 Correct 944 ms 2056 KB Output is correct
17 Correct 3 ms 1116 KB Output is correct
18 Correct 5 ms 1816 KB Output is correct
19 Correct 657 ms 207064 KB Output is correct
20 Correct 650 ms 206864 KB Output is correct
21 Correct 662 ms 207048 KB Output is correct
22 Correct 655 ms 206808 KB Output is correct
23 Correct 550 ms 204684 KB Output is correct
24 Incorrect 62 ms 24012 KB Output isn't correct
25 Halted 0 ms 0 KB -