結果
| 問題 | No.1332 Range Nearest Query | 
| コンテスト | |
| ユーザー |  沙耶花 | 
| 提出日時 | 2021-01-08 22:07:14 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,146 bytes | 
| コンパイル時間 | 2,171 ms | 
| コンパイル使用メモリ | 211,044 KB | 
| 最終ジャッジ日時 | 2025-01-17 12:14:58 | 
| ジャッジサーバーID (参考情報) | judge2 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 6 WA * 10 TLE * 32 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:10:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |         scanf("%d",&N);
      |         ~~~~~^~~~~~~~~
main.cpp:14:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |                 scanf("%lld",&X[i]);
      |                 ~~~~~^~~~~~~~~~~~~~
main.cpp:18:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |         scanf("%d",&Q);
      |         ~~~~~^~~~~~~~~
main.cpp:28:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |                 scanf("%d %d %lld",&l[i],&r[i],&x[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            
            ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000
int main(){
	int N;
	scanf("%d",&N);
	
	vector<long long> X(N);
	rep(i,N){
		scanf("%lld",&X[i]);
	}
	
	int Q;
	scanf("%d",&Q);
	
	vector<int> l(Q),r(Q);
	vector<long long> x(Q);
	
	int sz = 550;
	
	vector q(sz,vector<pair<int,int>>());
	
	rep(i,Q){
		scanf("%d %d %lld",&l[i],&r[i],&x[i]);
		l[i]--;
		r[i]--;
		
		q[l[i]/sz].emplace_back(r[i],i);
	}
	
	vector<int> ans(Q,1000000005);
	
	rep(i,sz){
		if(q[i].size()==0)continue;
		sort(q[i].begin(),q[i].end());
		
		int ll = 0,rr = 0;
		set<int> S;
		
		rep(j,q[i].size()){
			int ind = q[i][j].second;
			int R = q[i][j].first;
			int L = l[ind];
			
			while(rr<=R){
				S.insert(X[rr]);
				rr++;
			}
			
			while(L>ll){
				S.erase(X[ll]);
				ll++;
			}
			
			while(L<ll){
				ll--;
				S.insert(X[ll]);
			}
			
			long long xx = x[ind];
			auto it = S.upper_bound(xx);
			if(it!=S.end())ans[ind] = abs((*it)-xx);
			if(it!=S.begin()){
				it--;
				ans[ind] = abs((*it)-xx);
			}
		}
	}
	
	rep(i,Q){
		printf("%d\n",ans[i]);
	}
	
    return 0;
}
            
            
            
        