답안 #302118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
302118 2020-09-18T13:10:22 Z edenooo 역사적 조사 (JOI14_historical) C++17
0 / 100
1 ms 384 KB
#include<bits/stdc++.h>
using namespace std;
 
#define INF 1234567890
#define ll long long

const int SQ = 316;
struct Node {
	int l, r, i;
	bool operator<(Node &n)
	{
		if (l/SQ != n.l/SQ) return l/SQ < n.l/SQ;
		return r < n.r;
	}
};

int N, Q;
int A[100101], B[100101], cnt[100101];
ll res[100101];
vector<Node> q;
priority_queue<ll> pq, del;
vector<int> com;

int main()
{
	scanf("%d %d", &N, &Q);
	for(int i=1; i<=N; i++)
	{
		scanf("%d", &A[i]);
		com.push_back(A[i]);
	}
	// 좌표 압축
	sort(com.begin(), com.end());
	com.erase(unique(com.begin(), com.end()), com.end());
	for(int i=1; i<=N; i++)
		B[i] = lower_bound(com.begin(), com.end(), A[i]) - com.begin();
	for(int i=0; i<com.size(); i++)
		pq.push(0);

	// 오프라인 쿼리
	for(int i=1; i<=Q; i++)
	{
		int l, r;
		scanf("%d %d", &l, &r);
		q.push_back({l, r, i});
	}
	sort(q.begin(), q.end());

	// MO's
	int l = 1, r = 0; // [l, r]
	for(int i=0; i<q.size(); i++)
	{
		int nl = q[i].l, nr = q[i].r, idx = q[i].i;
		printf("%d %d\n", nl, nr);
		while(r < nr)
		{
			r++;
			del.push((ll)A[r]*cnt[B[r]]);
			cnt[B[r]]++;
			pq.push((ll)A[r]*cnt[B[r]]);
		}
		while(l < nl)
		{
			del.push((ll)A[l]*cnt[B[l]]);
			cnt[B[l]]--;
			pq.push((ll)A[l]*cnt[B[l]]);
			l++;
		}
		while(nl < l)
		{
			l--;
			del.push((ll)A[l]*cnt[B[l]]);
			cnt[B[l]]++;
			pq.push((ll)A[l]*cnt[B[l]]);
		}
		while(nr < r)
		{
			del.push((ll)A[r]*cnt[B[r]]);
			cnt[B[r]]--;
			pq.push((ll)A[r]*cnt[B[r]]);
			r--;
		}

		while(!pq.empty() && !del.empty() && pq.top() == del.top())
			pq.pop(), del.pop();
		res[idx] = pq.top();
	}

	for(int i=1; i<=Q; i++)
		printf("%lld\n", res[i]);
	return 0;
}

Compilation message

historic.cpp: In function 'int main()':
historic.cpp:37:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |  for(int i=0; i<com.size(); i++)
      |               ~^~~~~~~~~~~
historic.cpp:51:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |  for(int i=0; i<q.size(); i++)
      |               ~^~~~~~~~~
historic.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   26 |  scanf("%d %d", &N, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |   scanf("%d", &A[i]);
      |   ~~~~~^~~~~~~~~~~~~
historic.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   44 |   scanf("%d %d", &l, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -