답안 #136199

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
136199 2019-07-25T01:04:12 Z gs14004 Queue (CEOI06_queue) C++17
100 / 100
390 ms 16624 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <deque>
#include <bitset>
#include <iostream>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int inf = 1e9;
 
map<int, int> nxt, prv, low;
 
int getprv(int x){
	if(prv.find(x) != prv.end()) return prv[x];
	return x - 1;
}
 
int getnxt(int x){
	if(nxt.find(x) != nxt.end()) return nxt[x];
	return x + 1;
}
 
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		int a, b;
		scanf("%d %d",&a,&b);
		int pa = getprv(a);
		int na = getnxt(a);
		nxt[pa] = na;
		prv[na] = pa;
		int pb = getprv(b);
		nxt[pb] = a;
		prv[a] = pb;
		nxt[a] = b;
		prv[b] = a;
	}
	vector<pi> interval;
	vector<int> length;
	int p = 0;
	while(1){
		auto t = nxt.lower_bound(p);
		if(t == nxt.end()){
			interval.emplace_back(p, inf);
			break;
		}
		interval.emplace_back(p, t->first);
		p = t->second;
	}
	int cnt = 0;
	for(auto &i : interval){
		low[i.first] = cnt;
		cnt += i.second - i.first + 1;
		length.push_back(cnt);
	}
	int q;
	scanf("%d",&q);
	while(q--){
		char buf[5];
		int x;
		scanf("%s %d",buf,&x);
		if(*buf == 'L'){
			x++;
			int p = lower_bound(length.begin(), length.end(), x) - length.begin();
			if(p) x -= length[p-1];
			printf("%d\n",interval[p].first + x - 1);
		}
		else{
			int cnt = 0;
			auto p = --low.upper_bound(x);
			printf("%d\n", p->second + x - p->first);
		}
	}
}

Compilation message

queue.cpp: In function 'int main()':
queue.cpp:83:8: warning: unused variable 'cnt' [-Wunused-variable]
    int cnt = 0;
        ^~~
queue.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
queue.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a,&b);
   ~~~~~^~~~~~~~~~~~~~~
queue.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
queue.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s %d",buf,&x);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 30 ms 1272 KB Output is correct
6 Correct 41 ms 2168 KB Output is correct
7 Correct 59 ms 3064 KB Output is correct
8 Correct 69 ms 4724 KB Output is correct
9 Correct 84 ms 5064 KB Output is correct
10 Correct 86 ms 5496 KB Output is correct
11 Correct 228 ms 11724 KB Output is correct
12 Correct 206 ms 9716 KB Output is correct
13 Correct 242 ms 11968 KB Output is correct
14 Correct 225 ms 11752 KB Output is correct
15 Correct 232 ms 11956 KB Output is correct
16 Correct 232 ms 11848 KB Output is correct
17 Correct 30 ms 504 KB Output is correct
18 Correct 54 ms 1288 KB Output is correct
19 Correct 88 ms 1624 KB Output is correct
20 Correct 139 ms 2308 KB Output is correct
21 Correct 210 ms 11504 KB Output is correct
22 Correct 294 ms 13776 KB Output is correct
23 Correct 390 ms 16436 KB Output is correct
24 Correct 325 ms 16624 KB Output is correct
25 Correct 271 ms 13072 KB Output is correct