Submission #696944

# Submission time Handle Problem Language Result Execution time Memory
696944 2023-02-07T16:37:42 Z tamthegod Izbori (COCI22_izbori) C++17
110 / 110
609 ms 15800 KB
// Make the best become better
// No room for laziness
#include<bits/stdc++.h>

#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 2e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, a[maxN];
vector<int> pos[maxN];
const int block_size = 1000;
int b[maxN];
int bit[maxN * 2];
void update(int pos)
{
    while(pos <= 2 * n)
    {
        bit[pos]++;
        pos += pos & -pos;
    }
}
int get(int pos)
{
    int res = 0;
    while(pos)
    {
        res += bit[pos];
        pos -= pos & -pos;
    }
    return res;
}
void ReadInput()
{
    cin >> n;
    vector<int> vc;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        vc.pb(a[i]);
    }
    sort(vc.begin(), vc.end());
    vc.erase(unique(vc.begin(), vc.end()), vc.end());
    for(int i=1; i<=n; i++)
        a[i] = upper_bound(vc.begin(), vc.end(), a[i]) - vc.begin();
    for(int i=1; i<=n; i++)
        pos[a[i]].pb(i);
}
int f(int l, int r)
{
    return (r + l) * (r - l + 1) / 2;
}
int res = 0;
void Cal1(int val)
{
    for(int i=0; i<pos[val].size(); i++)
    {
        for(int j=i; j>=0; j--)
        {
           // i = 4, j = 2;
            int r = pos[val][i], l = pos[val][j];
            int s = 2 * (i - j + 1) - (r - l + 1);
            if(s <= 0) continue;
            int last1 = j ? pos[val][j - 1] : 0;
            int last2 = i == pos[val].size() - 1 ? n + 1 : pos[val][i + 1];
            int left = s - 1;
            int t1 = l - last1 - 1, t2 = last2 - r - 1;
            t1 = min(t1, left);
            t2 = min(t2, left);
           // cout << t2;exit(0);
            /// cal 0 <= x1 <= t1, 0 <= x2 <= t2, x1 + x2 <= left
           // cout << s;exit(0);
            if(t1 + t2 <= left) res += (t1 + 1) * (t2 + 1);
            else
            {
                res += (t2 + 1) * (left - t2 + 1);
                int t = min(t1 - (left - t2 + 1), t2 - 1);
                res += f(t2 - t, t2);
            }
           //cout << res;exit(0);
        }
    }
}
void Cal2(int val)
{
    for(int i=1; i<=n; i++)
        b[i] = b[i - 1] + (a[i] == val ? 1 : -1);
    memset(bit, 0, sizeof bit);
    update(n);
    for(int i=1; i<=n; i++)
    {
        res += get(b[i] + n - 1);
        update(b[i] + n);
    }
}
void Solve()
{
    for(int i=1; i<=n; i++)
    {
        if(pos[i].empty()) continue;
        if(pos[i].size() <= block_size) Cal1(i);
        else Cal2(i);
    }
    cout << res;
}
int32_t main()
{
   // freopen("sol.inp", "r", stdin);
   // freopen("sol.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}

Compilation message

Main.cpp: In function 'void Cal1(long long int)':
Main.cpp:63: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]
   63 |     for(int i=0; i<pos[val].size(); i++)
      |                  ~^~~~~~~~~~~~~~~~
Main.cpp:72:27: 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]
   72 |             int last2 = i == pos[val].size() - 1 ? n + 1 : pos[val][i + 1];
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 6 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 12500 KB Output is correct
2 Correct 30 ms 13920 KB Output is correct
3 Correct 19 ms 11608 KB Output is correct
4 Correct 34 ms 13916 KB Output is correct
5 Correct 33 ms 13952 KB Output is correct
6 Correct 35 ms 14604 KB Output is correct
7 Correct 35 ms 14640 KB Output is correct
8 Correct 36 ms 14604 KB Output is correct
9 Correct 37 ms 14244 KB Output is correct
10 Correct 37 ms 14216 KB Output is correct
11 Correct 26 ms 13280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 6 ms 8148 KB Output is correct
18 Correct 36 ms 12500 KB Output is correct
19 Correct 30 ms 13920 KB Output is correct
20 Correct 19 ms 11608 KB Output is correct
21 Correct 34 ms 13916 KB Output is correct
22 Correct 33 ms 13952 KB Output is correct
23 Correct 35 ms 14604 KB Output is correct
24 Correct 35 ms 14640 KB Output is correct
25 Correct 36 ms 14604 KB Output is correct
26 Correct 37 ms 14244 KB Output is correct
27 Correct 37 ms 14216 KB Output is correct
28 Correct 26 ms 13280 KB Output is correct
29 Correct 36 ms 14928 KB Output is correct
30 Correct 18 ms 6916 KB Output is correct
31 Correct 30 ms 8764 KB Output is correct
32 Correct 77 ms 13172 KB Output is correct
33 Correct 35 ms 8832 KB Output is correct
34 Correct 38 ms 8992 KB Output is correct
35 Correct 24 ms 7728 KB Output is correct
36 Correct 14 ms 6740 KB Output is correct
37 Correct 16 ms 6960 KB Output is correct
38 Correct 55 ms 14680 KB Output is correct
39 Correct 54 ms 14116 KB Output is correct
40 Correct 53 ms 14448 KB Output is correct
41 Correct 57 ms 14424 KB Output is correct
42 Correct 59 ms 14172 KB Output is correct
43 Correct 101 ms 15796 KB Output is correct
44 Correct 107 ms 15800 KB Output is correct
45 Correct 106 ms 15644 KB Output is correct
46 Correct 105 ms 15708 KB Output is correct
47 Correct 102 ms 15772 KB Output is correct
48 Correct 128 ms 13964 KB Output is correct
49 Correct 137 ms 13964 KB Output is correct
50 Correct 589 ms 14480 KB Output is correct
51 Correct 609 ms 14460 KB Output is correct
52 Correct 72 ms 10572 KB Output is correct
53 Correct 76 ms 10412 KB Output is correct
54 Correct 115 ms 10660 KB Output is correct