// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
struct Node {
Node *l, *r;
ll val, sum, mset = INF, madd = 0;
int lo, hi;
Node(int L, int R) : lo(L), hi(R) {
if (hi - lo == 1)
val = sum = 0;
else {
int mid = (lo + hi) / 2;
l = new Node(lo, mid);
r = new Node(mid, hi);
update();
}
}
void update() {
int mid = (lo + hi) / 2;
sum = l->sum + r->sum;
val = l->val + r->val + l->sum * (hi - mid);
}
pll query(int L, int R) {
if (hi <= L || R <= lo) return {0, 0};
if (L <= lo && hi <= R) {
// cout << "L = " << lo << " R = " << hi << " cur_val = " << val << " cur_sum = " << sum << endl;
return {val + sum * (R - hi), sum};
}
push();
int mid = (lo + hi) / 2;
pll lpair = l->query(L, R), rpair = r->query(L, R);
ll right_len = max(0, R - mid);
ll cur_sum = lpair.second + rpair.second;
ll cur_val = lpair.first + rpair.first;
// cout << "L = " << lo << " R = " << hi << " cur_val = " << cur_val << " cur_sum = " << cur_sum << endl;
return {cur_val, cur_sum};
}
void set(int L, int R, ll x) {
if (hi <= L || R <= lo) return;
if (L <= lo && hi <= R) {
madd = 0;
mset = x;
sum = x * ll(hi - lo);
val = x * ll(hi - lo) * ll(hi - lo + 1) / 2;
return;
}
push();
l->set(L, R, x);
r->set(L, R, x);
update();
}
void add(int L, int R, ll x) {
if (hi <= L || R <= lo) return;
if (L <= lo && hi <= R) {
if (mset != INF)
mset += x;
else
madd += x;
// cout << "lo = " << lo << " hi = " << hi << endl;
sum += x * ll(hi - lo);
val += x * ll(hi - lo) * ll(hi - lo + 1) / 2;
return;
}
push();
l->add(L, R, x);
r->add(L, R, x);
update();
}
void push() {
if (mset != INF) {
if (hi - lo != 1) {
l->set(lo, hi, mset);
r->set(lo, hi, mset);
}
mset = INF;
} else if (madd) {
if (hi - lo != 1) {
l->add(lo, hi, madd);
r->add(lo, hi, madd);
}
madd = 0;
}
}
};
const ll seg_size = 500000;
int n, arr[200005];
umap<int, vi> dishes;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin >> n;
rep(i, 0, n) {
cin >> arr[i];
dishes[arr[i]].pb(i);
}
ll ans = 0;
Node segtree(0, seg_size);
trav(item, dishes) {
int dish;
vi people;
tie(dish, people) = item;
people.pb(n);
//debug(dish);
int prefix = seg_size / 2;
segtree.set(0, seg_size, 0);
segtree.add(prefix, prefix + (people[0]) + 1, 1);
rep(i, 0, sz(people) - 1) {
ll prev = ans;
++prefix;
int cur_pos = people[i], next_pos = people[i + 1], len = next_pos - cur_pos;
ll val = 0, sum = 0;
//cout << "i = " << i << " prefix = " << prefix << " len = " << len << endl;
tie(val, sum) = segtree.query(prefix - len, prefix);
//cout<<endl;
ans += val;
//cout << "val = " << val << endl;
tie(val, sum) = segtree.query(0, prefix - len);
//cout<<endl;
ans += sum * len;
//cout << "sum = " << sum << endl;
segtree.add(prefix - (len - 1), prefix + 1, 1);
//cout << endl<<endl;
prefix -= len - 1;
}
//cout << endl;
}
/*ll real_ans=0;
rep(i,0,n) {
umap<int,int>cnt;
int mx=0;
rep(j,i,n){
++cnt[arr[j]];
mx=max(mx,cnt[arr[j]]);
if(mx>(j-i+1)/2) ++real_ans;
}
}*/
cout << ans << endl;
return 0;
}
Compilation message
Main.cpp: In member function 'pll Node::query(int, int)':
Main.cpp:65:12: warning: unused variable 'right_len' [-Wunused-variable]
65 | ll right_len = max(0, R - mid);
| ^~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:8:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define rep(i, a, b) for (int i = (a); i < (b); ++i)
| ^
Main.cpp:158:9: note: in expansion of macro 'rep'
158 | rep(i, 0, sz(people) - 1) {
| ^~~
Main.cpp:159:16: warning: unused variable 'prev' [-Wunused-variable]
159 | ll prev = ans;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
62900 KB |
Output is correct |
2 |
Correct |
44 ms |
62820 KB |
Output is correct |
3 |
Correct |
44 ms |
62860 KB |
Output is correct |
4 |
Correct |
46 ms |
62888 KB |
Output is correct |
5 |
Correct |
44 ms |
62852 KB |
Output is correct |
6 |
Correct |
45 ms |
62832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
62900 KB |
Output is correct |
2 |
Correct |
44 ms |
62820 KB |
Output is correct |
3 |
Correct |
44 ms |
62860 KB |
Output is correct |
4 |
Correct |
46 ms |
62888 KB |
Output is correct |
5 |
Correct |
44 ms |
62852 KB |
Output is correct |
6 |
Correct |
45 ms |
62832 KB |
Output is correct |
7 |
Correct |
45 ms |
62908 KB |
Output is correct |
8 |
Correct |
45 ms |
62924 KB |
Output is correct |
9 |
Correct |
47 ms |
62924 KB |
Output is correct |
10 |
Correct |
51 ms |
62984 KB |
Output is correct |
11 |
Correct |
46 ms |
62952 KB |
Output is correct |
12 |
Correct |
46 ms |
63016 KB |
Output is correct |
13 |
Correct |
46 ms |
62960 KB |
Output is correct |
14 |
Correct |
45 ms |
62936 KB |
Output is correct |
15 |
Correct |
47 ms |
62888 KB |
Output is correct |
16 |
Correct |
46 ms |
62976 KB |
Output is correct |
17 |
Correct |
44 ms |
62984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
64660 KB |
Output is correct |
2 |
Correct |
120 ms |
65316 KB |
Output is correct |
3 |
Correct |
87 ms |
64352 KB |
Output is correct |
4 |
Correct |
122 ms |
65412 KB |
Output is correct |
5 |
Correct |
125 ms |
65504 KB |
Output is correct |
6 |
Correct |
130 ms |
65680 KB |
Output is correct |
7 |
Correct |
130 ms |
65700 KB |
Output is correct |
8 |
Correct |
127 ms |
65692 KB |
Output is correct |
9 |
Correct |
127 ms |
65684 KB |
Output is correct |
10 |
Correct |
128 ms |
65676 KB |
Output is correct |
11 |
Correct |
135 ms |
66460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
62900 KB |
Output is correct |
2 |
Correct |
44 ms |
62820 KB |
Output is correct |
3 |
Correct |
44 ms |
62860 KB |
Output is correct |
4 |
Correct |
46 ms |
62888 KB |
Output is correct |
5 |
Correct |
44 ms |
62852 KB |
Output is correct |
6 |
Correct |
45 ms |
62832 KB |
Output is correct |
7 |
Correct |
45 ms |
62908 KB |
Output is correct |
8 |
Correct |
45 ms |
62924 KB |
Output is correct |
9 |
Correct |
47 ms |
62924 KB |
Output is correct |
10 |
Correct |
51 ms |
62984 KB |
Output is correct |
11 |
Correct |
46 ms |
62952 KB |
Output is correct |
12 |
Correct |
46 ms |
63016 KB |
Output is correct |
13 |
Correct |
46 ms |
62960 KB |
Output is correct |
14 |
Correct |
45 ms |
62936 KB |
Output is correct |
15 |
Correct |
47 ms |
62888 KB |
Output is correct |
16 |
Correct |
46 ms |
62976 KB |
Output is correct |
17 |
Correct |
44 ms |
62984 KB |
Output is correct |
18 |
Correct |
105 ms |
64660 KB |
Output is correct |
19 |
Correct |
120 ms |
65316 KB |
Output is correct |
20 |
Correct |
87 ms |
64352 KB |
Output is correct |
21 |
Correct |
122 ms |
65412 KB |
Output is correct |
22 |
Correct |
125 ms |
65504 KB |
Output is correct |
23 |
Correct |
130 ms |
65680 KB |
Output is correct |
24 |
Correct |
130 ms |
65700 KB |
Output is correct |
25 |
Correct |
127 ms |
65692 KB |
Output is correct |
26 |
Correct |
127 ms |
65684 KB |
Output is correct |
27 |
Correct |
128 ms |
65676 KB |
Output is correct |
28 |
Correct |
135 ms |
66460 KB |
Output is correct |
29 |
Correct |
144 ms |
68036 KB |
Output is correct |
30 |
Correct |
109 ms |
66284 KB |
Output is correct |
31 |
Correct |
186 ms |
69376 KB |
Output is correct |
32 |
Correct |
404 ms |
75720 KB |
Output is correct |
33 |
Correct |
188 ms |
69596 KB |
Output is correct |
34 |
Correct |
205 ms |
69928 KB |
Output is correct |
35 |
Correct |
147 ms |
67980 KB |
Output is correct |
36 |
Correct |
102 ms |
65968 KB |
Output is correct |
37 |
Correct |
112 ms |
66532 KB |
Output is correct |
38 |
Correct |
157 ms |
65360 KB |
Output is correct |
39 |
Correct |
158 ms |
65248 KB |
Output is correct |
40 |
Correct |
163 ms |
65376 KB |
Output is correct |
41 |
Correct |
159 ms |
65208 KB |
Output is correct |
42 |
Correct |
155 ms |
65344 KB |
Output is correct |
43 |
Correct |
201 ms |
66924 KB |
Output is correct |
44 |
Correct |
193 ms |
66932 KB |
Output is correct |
45 |
Correct |
205 ms |
66932 KB |
Output is correct |
46 |
Correct |
200 ms |
66932 KB |
Output is correct |
47 |
Correct |
191 ms |
66932 KB |
Output is correct |
48 |
Correct |
261 ms |
65352 KB |
Output is correct |
49 |
Correct |
262 ms |
65352 KB |
Output is correct |
50 |
Correct |
237 ms |
65484 KB |
Output is correct |
51 |
Correct |
265 ms |
65696 KB |
Output is correct |
52 |
Correct |
219 ms |
65400 KB |
Output is correct |
53 |
Correct |
221 ms |
65252 KB |
Output is correct |
54 |
Correct |
218 ms |
65400 KB |
Output is correct |