답안 #535889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535889 2022-03-11T17:21:22 Z phathnv Izbori (COCI22_izbori) C++11
15 / 110
401 ms 32132 KB
#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)
#define taskname "MAJORITY"
using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int N = 500010;

struct SegTree{
    #define mid ((l + r) >> 1)
    ll d[N * 4], lazy[N * 4], start[N * 4];
    ll calc(int l, int r){
        ll len = r - l + 1;
        return (len + 1) * len / 2;
    }
    void doLazy(int id, int l, int r){
        if (lazy[id] == 0 && start == 0)
            return;
        d[id] += lazy[id] * calc(l, r) + start[id] * (r - l + 1);
        if (l != r){
            lazy[id << 1] += lazy[id];
            lazy[id << 1 | 1] += lazy[id];
            start[id << 1] += start[id];
            start[id << 1 | 1] += start[id] + (mid + 1 - l) * lazy[id];
        }
        lazy[id] = 0;
        start[id] = 0;
    }
    void update(int id, int l, int r, int u, int v, int st, int val){
        doLazy(id, l, r);
        if (v < l || r < u)
            return;
        if (u <= l && r <= v){
            lazy[id] += val;
            start[id] += st + val * (l - u);
            doLazy(id, l, r);
            return;
        }
        update(id << 1, l, mid, u, v, st, val);
        update(id << 1 | 1, mid + 1, r, u, v, st, val);
        d[id] = d[id << 1] + d[id << 1 | 1];
    }
    ll get(int id, int l, int r, int u, int v){
        doLazy(id, l, r);
        if (v < l || r < u)
            return 0;
        if (u <= l && r <= v)
            return d[id];
        return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
    }
    #undef mid
} ST;

int n, a[N];
vector <int> pos[N];

void readInput(){
    cin >> n;
    FOR(i, 1, n)
        pos[i].push_back(0);
    FOR(i, 1, n){
        int x;
        cin >> x;
        pos[x].push_back(i);
    }
    FOR(i, 1, n)
        pos[i].push_back(n + 1);

}

void solve(){
    ll ans = 0;
    FOR(i, 1, n){
        FOR(j, 0, pos[i].size() - 2){
            int r = j * 2 - pos[i][j];
            int l = r - (pos[i][j + 1] - pos[i][j] - 1);
            ans += ST.get(1, 0, 2 * n, l - 1 + n, r - 1 + n);
            ST.update(1, 0, 2 * n, l + n, r + n, 0, 1);
            ST.update(1, 0, 2 * n, r + n + 1, n * 2, (r - l + 1), 0);
        }
        FOR(j, 0, pos[i].size() - 2){
            int r = j * 2 - pos[i][j];
            int l = r - (pos[i][j + 1] - pos[i][j] - 1);
            ST.update(1, 0, 2 * n, l + n, r + n, 0, -1);
            ST.update(1, 0, 2 * n, r + n + 1, n * 2, -(r - l + 1), 0);
        }
    }
    cout << ans;
}

int main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    readInput();
    solve();
    return 0;
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:6:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define FOR(i, a, b) for(int i = a; i <= b; i++)
......
   81 |         FOR(j, 0, pos[i].size() - 2){
      |             ~~~~~~~~~~~~~~~~~~~~~~~    
Main.cpp:81:9: note: in expansion of macro 'FOR'
   81 |         FOR(j, 0, pos[i].size() - 2){
      |         ^~~
Main.cpp:6:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define FOR(i, a, b) for(int i = a; i <= b; i++)
......
   88 |         FOR(j, 0, pos[i].size() - 2){
      |             ~~~~~~~~~~~~~~~~~~~~~~~    
Main.cpp:88:9: note: in expansion of macro 'FOR'
   88 |         FOR(j, 0, pos[i].size() - 2){
      |         ^~~
Main.cpp: In function 'int main()':
Main.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 17 ms 24236 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 17 ms 24236 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 262 ms 17420 KB Output is correct
2 Correct 331 ms 18824 KB Output is correct
3 Correct 194 ms 16248 KB Output is correct
4 Correct 359 ms 19180 KB Output is correct
5 Correct 359 ms 19692 KB Output is correct
6 Correct 389 ms 19868 KB Output is correct
7 Correct 394 ms 19996 KB Output is correct
8 Correct 383 ms 19960 KB Output is correct
9 Correct 389 ms 19916 KB Output is correct
10 Correct 386 ms 19808 KB Output is correct
11 Correct 401 ms 32132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 17 ms 24236 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -