답안 #893339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
893339 2023-12-27T02:33:51 Z vjudge1 Izbori (COCI22_izbori) C++17
40 / 110
3000 ms 9280 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <functional>
#include <complex>
#include <climits>
#include <random>
using namespace std;
   
#define ll long long
#define pb push_back
#define ull unsigned long long
#define F first
#define S second
#define all(v) v.begin(), v.end()

int n;
int a[200005];

struct segTree{
    int t[400010 * 4];
    void upd(int idx, int v = 1, int l = 0, int r = 2 * n){
        if(l == r){
            t[v]++;
            return;
        }
        int m = (l + r) / 2;
        if(idx <= m) upd(idx, v * 2, l, m);
        else upd(idx, v * 2 + 1, m + 1, r);
        t[v] = t[v * 2] + t[v * 2 + 1];
    }
    int get(int l, int r, int v = 1, int tl = 0, int tr = 2 * n){
        if(tr < l || r < tl) return 0;
        if(l <= tl && tr <= r) return t[v];
        int m = (tl + tr) / 2;
        return get(l, r, v * 2, tl, m) + get(l, r, v * 2 + 1, m + 1, tr);
    }
} ver[2];

void sub3(){
    int pref[2][n + 1];
    for(int i = 0; i < 2; i++){
        for(int j = 1; j <= n; j++) pref[i][j] = 0;
    }
    for(int i = 1; i <= n; i++){
        if(a[i] == 2) a[i] = 1;
        else a[i] = 0;
    }
    for(int i = 1; i <= n; i++){
        pref[0][i] = pref[0][i - 1] + (a[i] == 0 ? 1 : -1);
        pref[1][i] = pref[1][i - 1] + (a[i] == 1 ? 1 : -1);
    }
    ll ans = 0;
    for(int i = 0; i < 2; i++){
        ver[i].upd(0 + n);
        for(int j = 1; j <= n; j++){
            int x = pref[i][j];
            //cout << x << "\n";
            //cout << x + n << " " << x - 1 + n << "\n";
            ver[i].upd(x + n);
            ans += ver[i].get(0, x - 1 + n);
            //cout << j << " " << ans << "\n";
        }
    }
    cout << ans;
}

void sub12(){
    ll ans = 0;
    for(int l = 1; l <= n; l++){
        ans += 1;
        bool f = 1;
        int sz = 1, d = a[l];
        map<int, int> cnt;
        ++cnt[d];
        for(int r = l + 1; r <= n; r++){
            ++cnt[a[r]];
            if(a[r] == d) sz++;
            else if(cnt[a[r]] > sz) sz = cnt[a[r]], d = a[r];
            if(sz > (r - l + 1) / 2) f = 1;
            else f = 0;
            ans += f;
        }
    }
    cout << ans;
}

void solve(){
    cin >> n;
    bool s3 = 1;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        s3 &= (a[i] <= 2);
    }
    if(s3) sub3();
    else sub12();
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    int xach = 1; 
    //cin >> xach;
    while(xach--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 408 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 408 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 57 ms 512 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 41 ms 348 KB Output is correct
10 Correct 42 ms 348 KB Output is correct
11 Correct 41 ms 472 KB Output is correct
12 Correct 44 ms 596 KB Output is correct
13 Correct 42 ms 348 KB Output is correct
14 Correct 42 ms 348 KB Output is correct
15 Correct 42 ms 348 KB Output is correct
16 Correct 41 ms 344 KB Output is correct
17 Correct 6 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 4444 KB Output is correct
2 Correct 50 ms 4968 KB Output is correct
3 Correct 29 ms 8024 KB Output is correct
4 Correct 48 ms 4956 KB Output is correct
5 Correct 48 ms 5140 KB Output is correct
6 Correct 51 ms 5212 KB Output is correct
7 Correct 51 ms 5316 KB Output is correct
8 Correct 52 ms 5200 KB Output is correct
9 Correct 51 ms 5304 KB Output is correct
10 Correct 51 ms 5212 KB Output is correct
11 Correct 71 ms 9280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 408 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 57 ms 512 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 41 ms 348 KB Output is correct
10 Correct 42 ms 348 KB Output is correct
11 Correct 41 ms 472 KB Output is correct
12 Correct 44 ms 596 KB Output is correct
13 Correct 42 ms 348 KB Output is correct
14 Correct 42 ms 348 KB Output is correct
15 Correct 42 ms 348 KB Output is correct
16 Correct 41 ms 344 KB Output is correct
17 Correct 6 ms 348 KB Output is correct
18 Correct 34 ms 4444 KB Output is correct
19 Correct 50 ms 4968 KB Output is correct
20 Correct 29 ms 8024 KB Output is correct
21 Correct 48 ms 4956 KB Output is correct
22 Correct 48 ms 5140 KB Output is correct
23 Correct 51 ms 5212 KB Output is correct
24 Correct 51 ms 5316 KB Output is correct
25 Correct 52 ms 5200 KB Output is correct
26 Correct 51 ms 5304 KB Output is correct
27 Correct 51 ms 5212 KB Output is correct
28 Correct 71 ms 9280 KB Output is correct
29 Execution timed out 3064 ms 2396 KB Time limit exceeded
30 Halted 0 ms 0 KB -