This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Problem: #3 - Izbori
// Contest: DMOJ - COCI '21 Contest 4
// URL: https://dmoj.ca/problem/coci21c4p3
// Memory Limit: 512 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define lg long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mid (lr+hr)/2
const lg N = 2e5+5;
lg id;
map<lg, lg> ids;
vector<lg> o[N];
lg n, a[N], fr[2*N+2], seg[(2*N+2) << 2][4];
//sum, arithmetic decreasing, lazy, assign
void doLazy(lg node, lg lr, lg hr)
{
if(seg[node][3])
{
seg[node][0] = seg[node][1] = 0;
if(lr != hr)
{
for(int ch = node*2; ch <= node*2+1; ch++)
{
seg[ch][3] |= seg[node][3];
seg[ch][2] = 0;
}
}
seg[node][3] = 0;
}
if(seg[node][2])
{
seg[node][0] += seg[node][2]*(hr-lr+1);
lg x = (hr-lr+1)*(hr-lr+2)/2;
seg[node][1] += x*seg[node][2];
if(lr != hr)
{
for(int ch = node*2; ch <= node*2+1; ch++)
{
seg[ch][2] += seg[node][2];
}
}
seg[node][2] = 0;
}
return;
}
void update(lg node, lg lr, lg hr, lg lq, lg hq, lg val)
{
if(lq > hq) return;
doLazy(node, lr, hr);
if(lr > hq || lq > hr) return;
if(lq <= lr && hr <= hq)
{
seg[node][2] += val;
doLazy(node, lr, hr);
return ;
}
update(node << 1, lr, mid, lq, hq, val);
update(node << 1 | 1, mid+1, hr, lq, hq, val);
seg[node][0] = seg[node << 1][0]+seg[node << 1 | 1][0];
seg[node][1] = seg[node << 1][1]+seg[node << 1][0]*(hr-mid)+seg[node << 1 | 1][1];
return;
}
lg get(lg node, lg lr, lg hr, lg lq, lg hq, lg t)
{
if(lq > hq) return 0;
doLazy(node, lr, hr);
if(lq > hr || lr > hq) return 0;
if(lq <= lr && hr <= hq)
{
if(t)
{
return seg[node][0]*(hq-hr)+seg[node][1];
}
return seg[node][0];
}
lg l = get(node << 1, lr, mid, lq, hq, t);
lg r = get(node << 1 | 1, mid+1, hr, lq, hq, t);
return l+r;
}
int main()
{
fastio;
cin >> n;
for(int i = 1; i <= n; i++) o[i].push_back(0);
for(int i = 1; i <= n; i++)
{
cin >> a[i];
if(!ids.count(a[i]))
{
ids[a[i]] = ++id;
}
a[i] = ids[a[i]];
o[a[i]].push_back(i);
}
for(int i = 1; i <= n; i++) o[i].push_back(n+1);
lg ans = 0;
for(int i = 1; i <= n; i++)
{
if(o[i].size() <= 2) continue;
lg lstVal = 0;
update(1, 1, 2*n+1, n+2-o[i][1], n+1, 1);
for(int j = 1; j+1 < o[i].size(); j++)
{
lstVal -= (o[i][j]-o[i][j-1]-1);
lstVal++;
lg st = lstVal-(o[i][j+1]-o[i][j])+1;
ans += get(1, 1, 2*n+1, st+n+1, lstVal+n, 1);
if(st+n) ans += get(1, 1, 2*n+1, 1, st+n, 0)*(lstVal-st+1);
update(1, 1, 2*n+1, st+n+1, lstVal+n+1, 1);
}
seg[1][3] = true;
seg[1][2] = 0;
doLazy(1, 1, 2*n+1);
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:122:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(int j = 1; j+1 < o[i].size(); j++)
| ~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |