/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI 3.14159265359
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define task "tnc"
typedef long long ll;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
int a[maxn];
vector<int>k[maxn];
stack<pair<int,int>>p;
int st[2*maxn];
int lazy[2*maxn];
int st1[2*maxn];
vector<int>vt;
int ans=0;
int getsum(int l,int r){
return (r-l+1)*(r+l)/2;
}
void push(int id,int l,int mid,int r){
st[id*2]+=lazy[id]*(mid-l+1);
st1[id*2]+=lazy[id]*getsum(1,mid-l+1);
st[id*2+1]+=lazy[id]*(r-mid);
st1[id*2+1]+=lazy[id]*getsum(1,r-mid);
lazy[id*2]+=lazy[id];
lazy[id*2+1]+=lazy[id];
lazy[id]=0;
}
void update(int id,int l,int r,int u,int v,int val){
if(r<u || v<l)return;
if(u<=l && r<=v){
st[id]+=val*(r-l+1);
st1[id]+=val*getsum(1,r-l+1);
lazy[id]+=val;
return;
}
int mid=(l+r)/2;
push(id,l,mid,r);
update(id*2,l,mid,u,v,val);
update(id*2+1,mid+1,r,u,v,val);
st[id]=st[id*2]+st[id*2+1];
st1[id]=st1[id*2]+st[id*2]*(r-mid)+st1[id*2+1];
}
int get(int id,int l,int r,int u,int v,int dis){
if(r<u || v<l)return 0;
if(u<=l && r<=v){
return st[id]*(dis);
}
int mid=(l+r)/2;
push(id,l,mid,r);
return get(id*2,l,mid,u,v,dis)+get(id*2+1,mid+1,r,u,v,dis);
}
int get1(int id,int l,int r,int u,int v){
if(r<u || v<l)return 0;
if(u<=l && r<=v ){
return st1[id]+st[id]*(v-r);
}
int mid=(l+r)/2;
push(id,l,mid,r);
return get1(id*2,l,mid,u,v)+get1(id*2+1,mid+1,r,u,v);
}
signed main()
{
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
//freopen(task".inp" , "r" , stdin);
//freopen(task".out" , "w" , stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
vt.pb(a[i]);
}
sort(vt.begin(),vt.end());
vt.resize(distance(vt.begin(),unique(all(vt))));
for(int i=1;i<=n;i++){
a[i]=lower_bound(vt.begin(),vt.end(),a[i])-vt.begin()+1;
k[a[i]].pb(i);
}
for(int i=1;i<=n;i++){
while(p.size()){
pair<int,int>d=p.top();
p.pop();
update(1,1,2*n,d.fi,d.se,-1);
}
int ht=0;
for(int j=0;j<k[i].size();j++){
if(j==0){
p.push({n-k[i][j]+1,n});
update(1,1,2*n,n-k[i][j]+1,n,1);
ht-=k[i][j];
ht++;
}
ht++;
int dis;
if(j==(int)k[i].size()-1){
dis=n-k[i][j]+1;
}
else{
dis=k[i][j+1]-k[i][j];
}
ans+=get(1,1,2*n,1,ht-dis+n,dis);
ans+=get1(1,1,2*n,ht-dis+n+1,ht-1+n);
update(1,1,2*n,ht-dis+1+n,ht+n,1);
p.push(mp(ht-dis+1+n,ht+n));
ht-=dis;
ht++;
}
}
cout<<ans;
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:109:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int j=0;j<k[i].size();j++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23844 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23976 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23844 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23976 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
18 ms |
23944 KB |
Output is correct |
10 |
Correct |
16 ms |
24032 KB |
Output is correct |
11 |
Correct |
15 ms |
24020 KB |
Output is correct |
12 |
Correct |
15 ms |
23924 KB |
Output is correct |
13 |
Correct |
14 ms |
24040 KB |
Output is correct |
14 |
Correct |
14 ms |
23984 KB |
Output is correct |
15 |
Correct |
18 ms |
24020 KB |
Output is correct |
16 |
Correct |
15 ms |
24020 KB |
Output is correct |
17 |
Correct |
15 ms |
24020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
28804 KB |
Output is correct |
2 |
Correct |
164 ms |
29788 KB |
Output is correct |
3 |
Correct |
101 ms |
27216 KB |
Output is correct |
4 |
Correct |
175 ms |
30208 KB |
Output is correct |
5 |
Correct |
180 ms |
30364 KB |
Output is correct |
6 |
Correct |
204 ms |
30480 KB |
Output is correct |
7 |
Correct |
190 ms |
30380 KB |
Output is correct |
8 |
Correct |
192 ms |
30600 KB |
Output is correct |
9 |
Correct |
189 ms |
30656 KB |
Output is correct |
10 |
Correct |
195 ms |
30632 KB |
Output is correct |
11 |
Correct |
193 ms |
44520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23844 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23976 KB |
Output is correct |
4 |
Correct |
12 ms |
23888 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
13 ms |
23892 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23764 KB |
Output is correct |
9 |
Correct |
18 ms |
23944 KB |
Output is correct |
10 |
Correct |
16 ms |
24032 KB |
Output is correct |
11 |
Correct |
15 ms |
24020 KB |
Output is correct |
12 |
Correct |
15 ms |
23924 KB |
Output is correct |
13 |
Correct |
14 ms |
24040 KB |
Output is correct |
14 |
Correct |
14 ms |
23984 KB |
Output is correct |
15 |
Correct |
18 ms |
24020 KB |
Output is correct |
16 |
Correct |
15 ms |
24020 KB |
Output is correct |
17 |
Correct |
15 ms |
24020 KB |
Output is correct |
18 |
Correct |
125 ms |
28804 KB |
Output is correct |
19 |
Correct |
164 ms |
29788 KB |
Output is correct |
20 |
Correct |
101 ms |
27216 KB |
Output is correct |
21 |
Correct |
175 ms |
30208 KB |
Output is correct |
22 |
Correct |
180 ms |
30364 KB |
Output is correct |
23 |
Correct |
204 ms |
30480 KB |
Output is correct |
24 |
Correct |
190 ms |
30380 KB |
Output is correct |
25 |
Correct |
192 ms |
30600 KB |
Output is correct |
26 |
Correct |
189 ms |
30656 KB |
Output is correct |
27 |
Correct |
195 ms |
30632 KB |
Output is correct |
28 |
Correct |
193 ms |
44520 KB |
Output is correct |
29 |
Correct |
198 ms |
44204 KB |
Output is correct |
30 |
Correct |
98 ms |
28628 KB |
Output is correct |
31 |
Correct |
195 ms |
33224 KB |
Output is correct |
32 |
Correct |
495 ms |
43332 KB |
Output is correct |
33 |
Correct |
208 ms |
33476 KB |
Output is correct |
34 |
Correct |
221 ms |
33644 KB |
Output is correct |
35 |
Correct |
146 ms |
29312 KB |
Output is correct |
36 |
Correct |
88 ms |
28464 KB |
Output is correct |
37 |
Correct |
115 ms |
28700 KB |
Output is correct |
38 |
Correct |
215 ms |
37712 KB |
Output is correct |
39 |
Correct |
213 ms |
37176 KB |
Output is correct |
40 |
Correct |
219 ms |
37320 KB |
Output is correct |
41 |
Correct |
217 ms |
37336 KB |
Output is correct |
42 |
Correct |
211 ms |
37116 KB |
Output is correct |
43 |
Correct |
243 ms |
39728 KB |
Output is correct |
44 |
Correct |
251 ms |
39900 KB |
Output is correct |
45 |
Correct |
240 ms |
39648 KB |
Output is correct |
46 |
Correct |
237 ms |
39672 KB |
Output is correct |
47 |
Correct |
237 ms |
39800 KB |
Output is correct |
48 |
Correct |
313 ms |
41284 KB |
Output is correct |
49 |
Correct |
321 ms |
41280 KB |
Output is correct |
50 |
Correct |
343 ms |
41692 KB |
Output is correct |
51 |
Correct |
288 ms |
41820 KB |
Output is correct |
52 |
Correct |
352 ms |
41088 KB |
Output is correct |
53 |
Correct |
333 ms |
40976 KB |
Output is correct |
54 |
Correct |
366 ms |
41052 KB |
Output is correct |