#include<bits/stdc++.h>
using namespace std;
#define INF 1234567890
#define ll long long
inline int64_t gilbertOrder(int x, int y, int pow, int rotate) {
if (pow == 0) {
return 0;
}
int hpow = 1 << (pow-1);
int seg = (x < hpow) ? (
(y < hpow) ? 0 : 3
) : (
(y < hpow) ? 1 : 2
);
seg = (seg + rotate) & 3;
const int rotateDelta[4] = {3, 0, 0, 1};
int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
int nrot = (rotate + rotateDelta[seg]) & 3;
int64_t subSquareSize = int64_t(1) << (2*pow - 2);
int64_t ans = seg * subSquareSize;
int64_t add = gilbertOrder(nx, ny, pow-1, nrot);
ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
return ans;
}
struct Node {
int l, r, i;
int64_t ord;
inline void calcOrder() {
ord = gilbertOrder(l, r, 17, 0);
}
};
inline bool operator<(const Node &a, const Node &b) {
return a.ord < b.ord;
}
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=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;
while(r < nr)
{
r++;
if (cnt[B[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]]--;
if (cnt[B[l]]) pq.push((ll)A[l]*cnt[B[l]]);
l++;
}
while(nl < l)
{
l--;
if (cnt[B[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]]--;
if (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:73:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0; i<q.size(); i++)
| ~^~~~~~~~~
historic.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf("%d %d", &N, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:53:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
53 | scanf("%d", &A[i]);
| ~~~~~^~~~~~~~~~~~~
historic.cpp:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
288 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
416 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 |
384 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 |
288 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
416 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 |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
15 ms |
896 KB |
Output is correct |
18 |
Correct |
71 ms |
1676 KB |
Output is correct |
19 |
Correct |
313 ms |
4804 KB |
Output is correct |
20 |
Correct |
1455 ms |
8512 KB |
Output is correct |
21 |
Correct |
3826 ms |
13132 KB |
Output is correct |
22 |
Correct |
3754 ms |
25564 KB |
Output is correct |
23 |
Correct |
3550 ms |
3936 KB |
Output is correct |
24 |
Correct |
3608 ms |
6976 KB |
Output is correct |
25 |
Correct |
3218 ms |
99516 KB |
Output is correct |
26 |
Execution timed out |
4054 ms |
50276 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
11 ms |
640 KB |
Output is correct |
5 |
Correct |
200 ms |
2548 KB |
Output is correct |
6 |
Correct |
103 ms |
1096 KB |
Output is correct |
7 |
Correct |
107 ms |
4280 KB |
Output is correct |
8 |
Correct |
24 ms |
2292 KB |
Output is correct |
9 |
Correct |
284 ms |
14112 KB |
Output is correct |
10 |
Correct |
2441 ms |
2936 KB |
Output is correct |
11 |
Execution timed out |
4027 ms |
8660 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
288 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
416 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 |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
15 ms |
896 KB |
Output is correct |
18 |
Correct |
71 ms |
1676 KB |
Output is correct |
19 |
Correct |
313 ms |
4804 KB |
Output is correct |
20 |
Correct |
1455 ms |
8512 KB |
Output is correct |
21 |
Correct |
3826 ms |
13132 KB |
Output is correct |
22 |
Correct |
3754 ms |
25564 KB |
Output is correct |
23 |
Correct |
3550 ms |
3936 KB |
Output is correct |
24 |
Correct |
3608 ms |
6976 KB |
Output is correct |
25 |
Correct |
3218 ms |
99516 KB |
Output is correct |
26 |
Execution timed out |
4054 ms |
50276 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |