Submission #675802

# Submission time Handle Problem Language Result Execution time Memory
675802 2022-12-28T00:59:31 Z rafatoa Izbori (COCI22_izbori) C++17
110 / 110
579 ms 16504 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
 
#define F first
#define S second
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define vb vector<bool>
#define vvb vector<vb>
#define pb push_back
#define ppb pop_back
#define read(a) for(auto &x:a) cin >> x;
#define print(a) for(auto x:a) cout << x << " "; cout << "\n";
#define vc vector<char>
#define vvc vector<vc>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
 
#define int long long
#define ld long double
const int INF = 1e18;
const int inf = 1e9;

struct FT{
    int n;
    vi tree;
    FT(int sz){
        n = sz;
        tree = vi(n+1);
    }

    void update(int k, int val){
        k++;
        while(k <= n){
            tree[k] += val;
            k += k&-k;
        }
    }

    int query(int l, int r){
        r++;
        int ans = 0;
        while(r >= 1){
            ans += tree[r];
            r -= r&-r;
        }
        while(l >= 1){
            ans -= tree[l];
            l -= l&-l;
        }
        return ans;
    }
};

void solve(){
    int n; cin >> n;
    vi a(n); read(a);

    map<int, vi> mp;
    for(int i=0; i<n; i++)
        mp[a[i]].pb(i);

    //Número de subarrays de longitud <= len en [l, r]
    auto f = [&](int l, int r, int len){
        if(l > r) return 0LL;
        int s = (r-l+1)*(r-l+2)/2;
        if(len < r-l+1) s -= (r-l+1-len)*(r-l+2-len)/2;
        return s;
    };

    int ans = 0;
    for(auto &[el, pos]:mp){
        if(pos.size() <= 1000){
            for(int i=0; i<pos.size(); i++)
            for(int j=i; j<pos.size(); j++){
                int mxlen = 2*(j-i)+1;
                if(pos[j]-pos[i]+1 > mxlen) continue;

                int l = (i ? pos[i-1]+1 : 0);
                int r = (j+1 < pos.size() ? pos[j+1]-1 : n-1);

                ans += f(l, r, mxlen)-f(pos[i]+1, r, mxlen)-f(l, pos[j]-1, mxlen)+
                    f(pos[i]+1, pos[j]-1, mxlen);
            }
        } else {
            FT ft(2*n+1);
            int curr = 0;
            ft.update(n, 1);
            for(int i=0; i<n; i++){
                curr += (a[i] == el ? 1 : -1);
                if(curr+n-1 >= 0) ans += ft.query(0, curr+n-1);
                ft.update(curr+n, 1);
            }
        }
    }
    cout << ans << "\n";
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    // #ifndef ONLINE_JUDGE
    //     freopen("in.txt", "r", stdin);
    //     freopen("out.txt", "w", stdout);
    // #endif

    int tt = 1;
    // cin >> tt;
    while(tt--)
        solve();
    return 0;
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:77: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]
   77 |             for(int i=0; i<pos.size(); i++)
      |                          ~^~~~~~~~~~~
Main.cpp:78: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]
   78 |             for(int j=i; j<pos.size(); j++){
      |                          ~^~~~~~~~~~~
Main.cpp:83:30: 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]
   83 |                 int r = (j+1 < pos.size() ? pos[j+1]-1 : n-1);
      |                          ~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 4940 KB Output is correct
2 Correct 23 ms 6216 KB Output is correct
3 Correct 12 ms 3792 KB Output is correct
4 Correct 23 ms 6592 KB Output is correct
5 Correct 27 ms 6664 KB Output is correct
6 Correct 30 ms 7104 KB Output is correct
7 Correct 31 ms 7104 KB Output is correct
8 Correct 27 ms 7140 KB Output is correct
9 Correct 29 ms 7072 KB Output is correct
10 Correct 26 ms 7072 KB Output is correct
11 Correct 18 ms 7108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 19 ms 4940 KB Output is correct
19 Correct 23 ms 6216 KB Output is correct
20 Correct 12 ms 3792 KB Output is correct
21 Correct 23 ms 6592 KB Output is correct
22 Correct 27 ms 6664 KB Output is correct
23 Correct 30 ms 7104 KB Output is correct
24 Correct 31 ms 7104 KB Output is correct
25 Correct 27 ms 7140 KB Output is correct
26 Correct 29 ms 7072 KB Output is correct
27 Correct 26 ms 7072 KB Output is correct
28 Correct 18 ms 7108 KB Output is correct
29 Correct 27 ms 8680 KB Output is correct
30 Correct 17 ms 4564 KB Output is correct
31 Correct 43 ms 8272 KB Output is correct
32 Correct 147 ms 16504 KB Output is correct
33 Correct 46 ms 8760 KB Output is correct
34 Correct 43 ms 9088 KB Output is correct
35 Correct 25 ms 6304 KB Output is correct
36 Correct 14 ms 4052 KB Output is correct
37 Correct 19 ms 4820 KB Output is correct
38 Correct 45 ms 7092 KB Output is correct
39 Correct 44 ms 7088 KB Output is correct
40 Correct 44 ms 7036 KB Output is correct
41 Correct 44 ms 7080 KB Output is correct
42 Correct 43 ms 7028 KB Output is correct
43 Correct 91 ms 9068 KB Output is correct
44 Correct 88 ms 9016 KB Output is correct
45 Correct 88 ms 8832 KB Output is correct
46 Correct 90 ms 8904 KB Output is correct
47 Correct 100 ms 9008 KB Output is correct
48 Correct 121 ms 7748 KB Output is correct
49 Correct 143 ms 7756 KB Output is correct
50 Correct 535 ms 8448 KB Output is correct
51 Correct 579 ms 8424 KB Output is correct
52 Correct 70 ms 4452 KB Output is correct
53 Correct 70 ms 4304 KB Output is correct
54 Correct 127 ms 4436 KB Output is correct