// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int inf=1e9;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
namespace Segtree{
int tree[8*maxn],lazy[8*maxn],num[8*maxn];
void getnew(int l,int r,int id,int val,int add){
tree[id]+=val*(r-l+1)+add*(r-l)*(r-l+1)/2;
lazy[id]+=val;num[id]+=add;
}
void pushdown(int l,int r,int id){
if(lazy[id]==0 && num[id]==0) return;
int mid=(l+r)>>1;
getnew(l,mid,id<<1,lazy[id],num[id]);
getnew(mid+1,r,id<<1|1,lazy[id]+num[id]*(mid-l+1),num[id]);
lazy[id]=num[id]=0;
}
void update(int l,int r,int id,int tl,int tr,int val,int add){
if(r<tl || tr<l) return;
if(tl<=l && r<=tr){
getnew(l,r,id,val+add*(l-tl),add);
return;
}
pushdown(l,r,id);
int mid=(l+r)>>1;
update(l,mid,id<<1,tl,tr,val,add);update(mid+1,r,id<<1|1,tl,tr,val,add);
tree[id]=tree[id<<1]+tree[id<<1|1];
}
int query(int l,int r,int id,int tl,int tr){
if(r<tl || tr<l) return 0;
if(tl<=l && r<=tr) return tree[id];
pushdown(l,r,id);
int mid=(l+r)>>1;
return query(l,mid,id<<1,tl,tr)+query(mid+1,r,id<<1|1,tl,tr);
}
}
int n,ans=0;
vector<int> pos[maxn];
vector<pii> com;
void solve(int x){
int cur=n-pos[x][0]+1;pos[x].push_back(n+1);
Segtree::update(1,2*n,1,cur,n,1,1);
Segtree::update(1,2*n,1,n+1,2*n,n-cur+1,0);
//cout << x << '\n';
for(int i=0;i<(int)pos[x].size()-1;i++){
ans+=Segtree::query(1,2*n,1,cur-(pos[x][i+1]-pos[x][i])+1,cur);
//cout << ans << '\n';
Segtree::update(1,2*n,1,cur-(pos[x][i+1]-pos[x][i])+2,cur+1,1,1);
if(cur+2<=2*n) Segtree::update(1,2*n,1,cur+2,2*n,pos[x][i+1]-pos[x][i],0);
cur+=2-(pos[x][i+1]-pos[x][i]);
}
cur=n-pos[x][0]+1;
Segtree::update(1,2*n,1,cur,n,-1,-1);
Segtree::update(1,2*n,1,n+1,2*n,-(n-cur+1),0);
for(int i=0;i<(int)pos[x].size()-1;i++){
Segtree::update(1,2*n,1,cur-(pos[x][i+1]-pos[x][i])+2,cur+1,-1,-1);
if(cur+2<=2*n) Segtree::update(1,2*n,1,cur+2,2*n,-pos[x][i+1]+pos[x][i],0);
cur+=2-(pos[x][i+1]-pos[x][i]);
}
}
void solve(){
cin >> n;
for(int i=1;i<=n;i++){
int a;cin >> a;
com.push_back({a,i});
}
sort(com.begin(),com.end());
int v=1;
for(int i=0;i<n;i++){
if(i>=1 && com[i].first!=com[i-1].first) v++;
pos[v].push_back(com[i].second);
}
for(int i=1;i<=n;i++){
if(!pos[i].empty()) solve(i);
}
cout << ans << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
5 ms |
5204 KB |
Output is correct |
11 |
Correct |
6 ms |
5204 KB |
Output is correct |
12 |
Correct |
6 ms |
5204 KB |
Output is correct |
13 |
Correct |
5 ms |
5204 KB |
Output is correct |
14 |
Correct |
5 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5168 KB |
Output is correct |
16 |
Correct |
5 ms |
5204 KB |
Output is correct |
17 |
Correct |
5 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
9696 KB |
Output is correct |
2 |
Correct |
226 ms |
10840 KB |
Output is correct |
3 |
Correct |
124 ms |
8268 KB |
Output is correct |
4 |
Correct |
237 ms |
10908 KB |
Output is correct |
5 |
Correct |
252 ms |
11108 KB |
Output is correct |
6 |
Correct |
259 ms |
11468 KB |
Output is correct |
7 |
Correct |
255 ms |
11344 KB |
Output is correct |
8 |
Correct |
267 ms |
11392 KB |
Output is correct |
9 |
Correct |
271 ms |
11328 KB |
Output is correct |
10 |
Correct |
255 ms |
11336 KB |
Output is correct |
11 |
Correct |
281 ms |
24568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
5 ms |
5204 KB |
Output is correct |
11 |
Correct |
6 ms |
5204 KB |
Output is correct |
12 |
Correct |
6 ms |
5204 KB |
Output is correct |
13 |
Correct |
5 ms |
5204 KB |
Output is correct |
14 |
Correct |
5 ms |
5204 KB |
Output is correct |
15 |
Correct |
5 ms |
5168 KB |
Output is correct |
16 |
Correct |
5 ms |
5204 KB |
Output is correct |
17 |
Correct |
5 ms |
5204 KB |
Output is correct |
18 |
Correct |
168 ms |
9696 KB |
Output is correct |
19 |
Correct |
226 ms |
10840 KB |
Output is correct |
20 |
Correct |
124 ms |
8268 KB |
Output is correct |
21 |
Correct |
237 ms |
10908 KB |
Output is correct |
22 |
Correct |
252 ms |
11108 KB |
Output is correct |
23 |
Correct |
259 ms |
11468 KB |
Output is correct |
24 |
Correct |
255 ms |
11344 KB |
Output is correct |
25 |
Correct |
267 ms |
11392 KB |
Output is correct |
26 |
Correct |
271 ms |
11328 KB |
Output is correct |
27 |
Correct |
255 ms |
11336 KB |
Output is correct |
28 |
Correct |
281 ms |
24568 KB |
Output is correct |
29 |
Correct |
273 ms |
26340 KB |
Output is correct |
30 |
Correct |
97 ms |
10272 KB |
Output is correct |
31 |
Correct |
197 ms |
15352 KB |
Output is correct |
32 |
Correct |
551 ms |
26812 KB |
Output is correct |
33 |
Correct |
215 ms |
15564 KB |
Output is correct |
34 |
Correct |
221 ms |
15848 KB |
Output is correct |
35 |
Correct |
134 ms |
11140 KB |
Output is correct |
36 |
Correct |
80 ms |
9956 KB |
Output is correct |
37 |
Correct |
97 ms |
10376 KB |
Output is correct |
38 |
Correct |
295 ms |
18200 KB |
Output is correct |
39 |
Correct |
310 ms |
18244 KB |
Output is correct |
40 |
Correct |
292 ms |
18112 KB |
Output is correct |
41 |
Correct |
292 ms |
18168 KB |
Output is correct |
42 |
Correct |
298 ms |
18328 KB |
Output is correct |
43 |
Correct |
325 ms |
22264 KB |
Output is correct |
44 |
Correct |
347 ms |
22172 KB |
Output is correct |
45 |
Correct |
323 ms |
22172 KB |
Output is correct |
46 |
Correct |
319 ms |
22204 KB |
Output is correct |
47 |
Correct |
321 ms |
22164 KB |
Output is correct |
48 |
Correct |
432 ms |
22968 KB |
Output is correct |
49 |
Correct |
406 ms |
22932 KB |
Output is correct |
50 |
Correct |
383 ms |
23040 KB |
Output is correct |
51 |
Correct |
413 ms |
22956 KB |
Output is correct |
52 |
Correct |
415 ms |
22980 KB |
Output is correct |
53 |
Correct |
421 ms |
22968 KB |
Output is correct |
54 |
Correct |
407 ms |
23008 KB |
Output is correct |