답안 #259653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
259653 2020-08-08T06:39:47 Z 임성재(#5054) 역사적 조사 (JOI14_historical) C++17
40 / 100
4000 ms 17364 KB
#include<bits/stdc++.h>  
using namespace std;  
  
#define fast ios::sync_with_stdio(false);cin.tie(NULL)  
#define fi first  
#define se second  
#define all(v) (v).begin(),(v).end()  
#define pb push_back  
#define eb emplace_back
#define em emplace
#define pre(a) cout<<fixed; cout.precision(a)
#define mp make_pair
  
typedef long long ll;  
typedef pair<int,int> pii;  
typedef pair<ll,ll> pll;  
const ll INF = 1e18;
const int inf = 1e9;
const ll sz = 317;

ll n, q;
ll x[100010];
ll cnt[100010];
ll ans[100010];
vector<ll> v;
vector<pair<pll, ll>> qr;
priority_queue<ll> pQ, rQ;

void cl() {
	while(pQ.size() && rQ.size() && pQ.top() == rQ.top()) pQ.pop(), rQ.pop();
}

void add(int k) {
	rQ.em(cnt[k] * v[k]);
	cl();
	cnt[k]++;
	pQ.em(cnt[k] * v[k]);
}

void erase(int k) {
	rQ.em(cnt[k] * v[k]);
	cl();
	cnt[k]--;
	pQ.em(cnt[k] * v[k]);
}

ll cal() {
	cl();
	return pQ.top();
}

int main() {
	fast;

	cin >> n >> q;

	for(int i=0; i<n; i++) {
		cin >> x[i];
		v.eb(x[i]);
	}

	sort(all(v));
	v.erase(unique(all(v)), v.end());

	for(int i=0; i<n; i++) {
		x[i] = lower_bound(all(v), x[i]) - v.begin();
	}

	for(int i=1; i<=q; i++) {
		ll a, b;
		cin >> a >> b;
		a--, b--;

		qr.eb(mp(a, b), i);
	}

	sort(all(qr), [](pair<pll, ll> i, pair<pll, ll> j) {
		if(i.fi.fi / sz == j.fi.fi / sz) return i.fi.se < j.fi.se;
		return i.fi.fi < j.fi.fi;
	});

	int s = qr[0].fi.fi;
	int e = qr[0].fi.se;

	for(int i=s; i<=e; i++) {
		add(x[i]);
	}

	ans[qr[0].se] = cal();

	for(int i=1; i<q; i++) {
		while(s < qr[i].fi.fi) erase(x[s++]);
		while(s > qr[i].fi.fi) add(x[--s]);
		while(e < qr[i].fi.se) add(x[++e]);
		while(e > qr[i].fi.se) erase(x[e--]);

		ans[qr[i].se] = cal();
	}

	for(int i=1; i<=q; i++) {
		cout << ans[i] << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 7 ms 896 KB Output is correct
18 Correct 19 ms 1320 KB Output is correct
19 Correct 38 ms 1484 KB Output is correct
20 Correct 91 ms 1828 KB Output is correct
21 Correct 154 ms 1860 KB Output is correct
22 Correct 152 ms 2396 KB Output is correct
23 Correct 148 ms 1800 KB Output is correct
24 Correct 148 ms 1860 KB Output is correct
25 Correct 137 ms 2372 KB Output is correct
26 Correct 153 ms 2744 KB Output is correct
27 Correct 147 ms 2500 KB Output is correct
28 Correct 148 ms 2500 KB Output is correct
29 Correct 157 ms 2500 KB Output is correct
30 Correct 152 ms 2612 KB Output is correct
31 Correct 153 ms 1860 KB Output is correct
32 Correct 118 ms 1356 KB Output is correct
33 Correct 151 ms 2500 KB Output is correct
34 Correct 138 ms 2500 KB Output is correct
35 Correct 135 ms 2740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 10 ms 1024 KB Output is correct
9 Correct 19 ms 1664 KB Output is correct
10 Correct 23 ms 1660 KB Output is correct
11 Correct 108 ms 7392 KB Output is correct
12 Correct 69 ms 4976 KB Output is correct
13 Correct 88 ms 6804 KB Output is correct
14 Correct 106 ms 8940 KB Output is correct
15 Correct 98 ms 8800 KB Output is correct
16 Correct 73 ms 10088 KB Output is correct
17 Correct 37 ms 5780 KB Output is correct
18 Correct 66 ms 11112 KB Output is correct
19 Correct 70 ms 12384 KB Output is correct
20 Correct 38 ms 6444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 7 ms 896 KB Output is correct
18 Correct 19 ms 1320 KB Output is correct
19 Correct 38 ms 1484 KB Output is correct
20 Correct 91 ms 1828 KB Output is correct
21 Correct 154 ms 1860 KB Output is correct
22 Correct 152 ms 2396 KB Output is correct
23 Correct 148 ms 1800 KB Output is correct
24 Correct 148 ms 1860 KB Output is correct
25 Correct 137 ms 2372 KB Output is correct
26 Correct 153 ms 2744 KB Output is correct
27 Correct 147 ms 2500 KB Output is correct
28 Correct 148 ms 2500 KB Output is correct
29 Correct 157 ms 2500 KB Output is correct
30 Correct 152 ms 2612 KB Output is correct
31 Correct 153 ms 1860 KB Output is correct
32 Correct 118 ms 1356 KB Output is correct
33 Correct 151 ms 2500 KB Output is correct
34 Correct 138 ms 2500 KB Output is correct
35 Correct 135 ms 2740 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 0 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 2 ms 640 KB Output is correct
41 Correct 3 ms 512 KB Output is correct
42 Correct 6 ms 640 KB Output is correct
43 Correct 10 ms 1024 KB Output is correct
44 Correct 19 ms 1664 KB Output is correct
45 Correct 23 ms 1660 KB Output is correct
46 Correct 108 ms 7392 KB Output is correct
47 Correct 69 ms 4976 KB Output is correct
48 Correct 88 ms 6804 KB Output is correct
49 Correct 106 ms 8940 KB Output is correct
50 Correct 98 ms 8800 KB Output is correct
51 Correct 73 ms 10088 KB Output is correct
52 Correct 37 ms 5780 KB Output is correct
53 Correct 66 ms 11112 KB Output is correct
54 Correct 70 ms 12384 KB Output is correct
55 Correct 38 ms 6444 KB Output is correct
56 Correct 355 ms 2988 KB Output is correct
57 Correct 930 ms 5612 KB Output is correct
58 Correct 1640 ms 6904 KB Output is correct
59 Correct 2611 ms 12432 KB Output is correct
60 Correct 3788 ms 17364 KB Output is correct
61 Execution timed out 4082 ms 12760 KB Time limit exceeded
62 Halted 0 ms 0 KB -