-
Notifications
You must be signed in to change notification settings - Fork 67
/
ADANUM-TLE.cpp
189 lines (170 loc) · 4.96 KB
/
ADANUM-TLE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// main.cpp
// practice
//
// Created by Mahmud on 12/18/17.
// Copyright © 2017 Mahmud. All rights reserved.
//
/*
O((N + Q) * sqrt(N) * log(N)) solution using MO's algoritm
Gets time limit exceeded verdict...
MAX test may run in ~5 seconds. Probably, this solution can not be fastened further.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX = 200005;
const int MAGIC = 444;
struct query{
int id;
int l;
int r;
query() {
}
query(int _id, int _l, int _r):
id(_id), l(_l), r(_r) {
}
bool operator < (const query other) const{
if (l / MAGIC != other.l / MAGIC) return l / MAGIC < other.l / MAGIC;
return r < other.r;
}
};
int N, Q;
int data[MAX];
query queries[MAX];
int activeElements;
long long answer = 0;
int elementCount[MAX];
long long result[MAX];
vector<int> activeCounts;
void add(int position) {
if (position < 1 || position > N) return;
//cout << "adding: " << position << endl;
elementCount[data[position]] ++;
//cout << "element count is --> " << elementCount[data[position]] << endl;
if (elementCount[data[position]] == 1) {
//activeCounts.push_back(1);
activeCounts[activeElements] = 1;
activeElements ++;
answer += activeElements;
}
else {
auto it = lower_bound(activeCounts.begin(), activeCounts.end(), elementCount[data[position]] - 1, greater<int>());
(*it) ++;
answer += (int)(it - activeCounts.begin() + 1);
}
//cout << "after adding: "; for (int j : activeCounts) cout << j << ", "; cout << " ---> " << answer << endl;
}
void remove(int position) {
if (position < 1 || position > N) return;
//cout << "removing: " << position << endl;
elementCount[data[position]] --;
if (!elementCount[data[position]]) {
activeCounts[activeElements - 1] = 0;
//activeCounts.pop_back();
answer -= activeElements;
activeElements --;
}
else {
auto it = --lower_bound(activeCounts.begin(), activeCounts.end(), elementCount[data[position]], greater<int>());
(*it) --;
answer -= (int)(it - activeCounts.begin() + 1);
}
//cout << "after removing: "; for (int j : activeCounts) cout << j << ", "; cout << " ---> " << answer << endl;
}
template <class T> void fastInput(T &n){
char ch;
int sign = 1;
while(ch = getchar_unlocked(), isspace(ch)) {
};
n = 0;
if(ch == '-')
sign = -1;
else n = ch - '0';
while(ch = getchar_unlocked(), isdigit(ch))
n = (n << 3) + (n << 1) + ch - '0';
n *= sign;
}
template<class T> void fastPrint(T n){
if(n == 0){
puts("0");
return;
}
char buffer[256];
int ptr = 0, sign = 1;
if(n < 0){
sign = -1;
n *= -1;
}
while(n > 0){
buffer[ptr ++] = (char)(n % 10 + '0');
n /= 10;
}
if(sign == -1)
putchar_unlocked('-');
for(int i = ptr - 1; i >= 0; i --)
putchar_unlocked(buffer[i]);
putchar_unlocked('\n');
}
void test() {
vector<int> v = {5, 2, 1, 1};
auto it = lower_bound(v.begin(), v.end(), 1, greater<int>());
(*it) ++;
cout << *it << " vs " << (int)(it - v.begin()) << endl;
cout << "now vector is: ";
for (int i : v) cout << i << ", "; cout << endl;
}
int main() {
//test();
//return 0;
//scanf("%d%d", &N, &Q);
fastInput(N);
fastInput(Q);
for (int i = 1; i <= N; i ++) {
//scanf("%d", &data[i]);
fastInput(data[i]);
}
vector<int> keys = vector<int>(data + 1, data + N + 1);
sort(keys.begin(), keys.end());
keys.erase(unique(keys.begin(), keys.end()), keys.end());
for (int i = 1; i <= N; i ++) {
data[i] = (int)(lower_bound(keys.begin(), keys.end(), data[i]) - keys.begin()) + 1;
}
int l = (int)keys.size();
//for (int i = 1; i <= N; i ++) cout << data[i] << ", ";
//cout << endl;
//cout << endl;
for (int i = 0; i < Q; i ++) {
int l, r;
//scanf("%d%d", &l, &r);
fastInput(l);
fastInput(r);
queries[i] = query(i, l, r);
}
if (l == N) {
for (int i = 0; i < Q; i ++) {
int m = queries[i].r - queries[i].l + 1;
printf("%lld\n", 1LL * m * (m + 1) / 2);
}
return 0;
}
sort(queries, queries + Q);
activeCounts.resize(N, 0);
int L = 1, R = 0;
for (int i = 0; i < Q; i ++) {
//cout << "now query comes: " << queries[i].l << " vs " << queries[i].r << endl;
while (L > queries[i].l) add(--L);
while (R < queries[i].r) add(++R);
while (R > queries[i].r) remove(R --);
while (L < queries[i].l) remove(L ++);
result[queries[i].id] = answer;
//for (int j : activeCounts) cout << j << ", "; cout << endl;
}
for (int i = 0; i < Q; i ++) {
//printf("%d\n", result[i]);
fastPrint(result[i]);
}
return 0;
}