답안 #787318

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
787318 2023-07-19T05:07:42 Z 반딧불(#10031) 즐거운 사진 수집 (JOI13_collecting) C++17
100 / 100
1399 ms 77360 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct segTree{
    ll cnt[21];
    ll tree[1<<21];
    bool add[1<<21];

    void init(int n){
        cnt[n] = 1;
        add[1] = 1;
    }

    void update(int i, int l, int r, int x, int depth){
        if(l==r){
            tree[i] = !tree[i];
            return;
        }
        if(add[i]){
            cnt[depth]--, cnt[depth-1] += 2;
            add[i] = 0, add[i*2] = add[i*2+1] = 1;
        }
        int m = (l+r)>>1;
        if(x<=m) update(i*2, l, m, x, depth-1);
        else update(i*2+1, m+1, r, x, depth-1);
        if(add[i*2] && add[i*2+1] && tree[i*2] == tree[i*2+1]){
            add[i] = 1, add[i*2] = add[i*2+1] = 0;
            cnt[depth]++, cnt[depth-1] -= 2;
        }
        tree[i] = tree[i*2] + tree[i*2+1];
    }
} treeA, treeB;

int n, q;

int main(){
    scanf("%d %d", &n, &q);
    treeA.init(n), treeB.init(n);
    while(q--){
        int t, x;
        scanf("%d %d", &t, &x);
        if(t == 0) treeA.update(1, 1, 1<<n, x, n);
        else treeB.update(1, 1, 1<<n, x, n);

        ll A[22] = {0}, B[22] = {0};
        for(int i=0; i<=n; i++) A[i] = treeA.cnt[i], B[i] = treeB.cnt[i];

        ll ans = 0;
        ll aSum = 1<<n, bSum = 1<<n;
        for(int i=0; i<=n; i++){
            ans += (aSum * B[i] + bSum * A[i]) >> i;
            ans -= A[i] * B[i];
            A[i+1] += A[i] >> 1;
            B[i+1] += B[i] >> 1;
        }
        printf("%lld\n", ans);
    }
}

Compilation message

collecting.cpp: In function 'int main()':
collecting.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
collecting.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         scanf("%d %d", &t, &x);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1364 ms 64700 KB Output is correct
2 Correct 1399 ms 64620 KB Output is correct
3 Correct 1187 ms 44504 KB Output is correct
4 Correct 1351 ms 77360 KB Output is correct
5 Correct 1350 ms 74120 KB Output is correct
6 Correct 1347 ms 66220 KB Output is correct
7 Correct 1379 ms 67752 KB Output is correct
8 Correct 1362 ms 67716 KB Output is correct
9 Correct 1158 ms 59384 KB Output is correct
10 Correct 1289 ms 57416 KB Output is correct
11 Correct 1328 ms 68348 KB Output is correct
12 Correct 1281 ms 63440 KB Output is correct
13 Correct 1147 ms 44888 KB Output is correct