답안 #579523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579523 2022-06-19T10:04:48 Z josanneo22 가로등 (APIO19_street_lamps) C++17
60 / 100
355 ms 26604 KB
#include <bits/stdc++.h>
using namespace std;
 
struct Req {
    bool isQ;
    int deb, fin, pos;
};
 
struct Node {
    int deb, fin, maxi;
};
 
const int M = 1 << 19, N = 2*M, L = 142, INF = 1e9 + 42;
 
bool b[L][L];
Node node[N];
vector<Req> req;
int n, q, val[N], last[N], t[N];
 
void setMax(int i, int newMax) {
    if(i == 0)
        return;
    if(i >= M) {
        node[i].maxi = newMax;
        //cout << node[i].deb << ' ' << node[i].fin << ' ' << newMax << '\n';
    } else {
        node[i].maxi = max(node[i*2].maxi, node[i*2+1].maxi);
    }
    setMax(i/2, newMax);
}
 
int getMax(int i, int deb, int fin) {
    if(fin <= node[i].deb || node[i].fin <= deb)
        return -INF;
    if(deb <= node[i].deb && node[i].fin <= fin) {
        //cout << node[i].deb << ' ' << node[i].fin << ' ' << node[i].maxi << '\n';
        return node[i].maxi;
    }
    int v = max(getMax(i*2, deb, fin), getMax(i*2+1, deb, fin));
    //cout << node[i].deb << ' ' << node[i].fin << ' ' << v << '\n';
    return v;
}
 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    cin >> n >> q;
    for(int i = 0; i < n; i++) {
        char c;
        cin >> c;
        if(c == '1')
            val[i] = 1;
    }
    for(int i = 0; i < q; i++) {
        string str;
        cin >> str;
        if(str[0] == 'q') {
            int deb, fin;
            cin >> deb >> fin;
            req.push_back({true, deb-1, fin-1, -1});
        } else {
            int pos;
            cin >> pos;
            req.push_back({false, -1, -1, pos-1});
        }
    }
    if(n <= 100 && q <= 100) {
        for(int i = 0; i < n; i++)
            b[0][i] = (val[i] == 1);
        for(int i = 0; i < q; i++) {
            for(int j = 0; j < n; j++)
                b[i+1][j] = b[i][j];
            if(req[i].isQ) {
                int nb = 0;
                for(int j = 0; j <= i; j++) {
                    bool possible = true;
                    for(int k = req[i].deb; k < req[i].fin; k++)
                        if(!b[j][k])
                            possible = false;
                    if(possible)
                        nb++;
                }
                cout << nb << '\n';
            } else {
                b[i+1][req[i].pos] = !b[i+1][req[i].pos];
            }
        }
        return 0;
    }
    bool one = true;
    for(int i = 0; i < q; i++)
        if(req[i].isQ)
            if(req[i].deb + 1 != req[i].fin)
                one = false;
 
    if(one) {
        for(int i = 0; i < n; i++)
            last[i] = -1;
        for(int i = 0; i < q; i++) {
            if(req[i].isQ) {
                if(val[req[i].deb] == 1) {
                    cout << t[req[i].deb] + i - last[req[i].deb] << '\n';
                } else {
                    cout << t[req[i].deb] << '\n';
                }
            } else {
                if(val[req[i].pos] == 1) {
                    t[req[i].pos] += i - last[req[i].pos];
                } else {
                    last[req[i].pos] = i;
                }
                val[req[i].pos] = 1 - val[req[i].pos];
            }
        }
        return 0;
    } else {
        for(int i = M; i < N; i++) {
            node[i].deb = i - M;
            node[i].fin = i - M + 1;
            if(i >= n + M) {
                node[i].maxi = -INF;
            } else {
                if(val[i - M] == 0)
                    node[i].maxi = INF;
                else
                    node[i].maxi = -1;
            }
        }
        for(int i = M-1; i > 0; i--) {
            node[i].deb = node[i*2].deb;
            node[i].fin = node[i*2+1].fin;
            node[i].maxi = max(node[i*2].maxi, node[i*2+1].maxi);
        }
 
        for(int i = 0; i < q; i++) {
            if(req[i].isQ) {
                int l = getMax(1, req[i].deb, req[i].fin);
                cout << max(0, i - l) << '\n';
                //cout << req[i].deb << ' ' << req[i].fin << ' ' << max(0, i - l) << ' ' << l << '\n';
            } else {
                setMax(req[i].pos + M, i);
            }
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 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
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 9144 KB Output is correct
2 Correct 88 ms 11992 KB Output is correct
3 Correct 81 ms 12392 KB Output is correct
4 Correct 98 ms 14540 KB Output is correct
5 Correct 107 ms 14664 KB Output is correct
6 Correct 87 ms 14224 KB Output is correct
7 Correct 107 ms 14096 KB Output is correct
8 Correct 114 ms 15436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12628 KB Output is correct
2 Correct 7 ms 12588 KB Output is correct
3 Correct 8 ms 12628 KB Output is correct
4 Correct 9 ms 12636 KB Output is correct
5 Correct 120 ms 22956 KB Output is correct
6 Correct 196 ms 23632 KB Output is correct
7 Correct 230 ms 24288 KB Output is correct
8 Correct 355 ms 26496 KB Output is correct
9 Correct 120 ms 20196 KB Output is correct
10 Correct 142 ms 20900 KB Output is correct
11 Correct 129 ms 21096 KB Output is correct
12 Correct 264 ms 24000 KB Output is correct
13 Correct 266 ms 26604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 12640 KB Output is correct
2 Incorrect 10 ms 12628 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 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 72 ms 9144 KB Output is correct
9 Correct 88 ms 11992 KB Output is correct
10 Correct 81 ms 12392 KB Output is correct
11 Correct 98 ms 14540 KB Output is correct
12 Correct 107 ms 14664 KB Output is correct
13 Correct 87 ms 14224 KB Output is correct
14 Correct 107 ms 14096 KB Output is correct
15 Correct 114 ms 15436 KB Output is correct
16 Correct 7 ms 12628 KB Output is correct
17 Correct 7 ms 12588 KB Output is correct
18 Correct 8 ms 12628 KB Output is correct
19 Correct 9 ms 12636 KB Output is correct
20 Correct 120 ms 22956 KB Output is correct
21 Correct 196 ms 23632 KB Output is correct
22 Correct 230 ms 24288 KB Output is correct
23 Correct 355 ms 26496 KB Output is correct
24 Correct 120 ms 20196 KB Output is correct
25 Correct 142 ms 20900 KB Output is correct
26 Correct 129 ms 21096 KB Output is correct
27 Correct 264 ms 24000 KB Output is correct
28 Correct 266 ms 26604 KB Output is correct
29 Correct 7 ms 12640 KB Output is correct
30 Incorrect 10 ms 12628 KB Output isn't correct
31 Halted 0 ms 0 KB -