답안 #739366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
739366 2023-05-10T11:10:17 Z nguyentunglam 가로등 (APIO19_street_lamps) C++17
100 / 100
2414 ms 105792 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 3e5 + 10;
int a[N], ans[N];
int n, q;
tuple<int, int, int> query[N];
pair<int, int> tt[N];
vector<int> node[N], bit[N];
void fakeup (int x, int y) {
    for(x; x <= n; x += x & -x) node[x].push_back(y);
}
void up(int x, int yy, int val) {
    for(x; x <= n; x += x & -x) {
        for(int y = upper_bound(node[x].begin(), node[x].end(), yy) - node[x].begin(); y <= node[x].size(); y += y & -y) {
            bit[x][y] += val;
        }
    }
}
int get(int x, int yy) {
    int ret = 0;
    for(x; x; x -= x & -x) {
        for(int y = upper_bound(node[x].begin(), node[x].end(), yy) - node[x].begin(); y; y -= y & -y) {
            ret += bit[x][y];
        }
    }
    return ret;
}
void compress() {
    for(int i = 1; i <= n; i++) {
        sort(node[i].begin(), node[i].end());
        node[i].resize(unique(node[i].begin(), node[i].end()) - node[i].begin());
        bit[i].resize(node[i].size() + 1);
    }
}
void fakeinc(int x, int y, int u, int v) {
    fakeup(x, y);
    fakeup(x, v + 1);
    fakeup(u + 1, y);
    fakeup(u + 1, v + 1);
}
void inc(int x, int y, int u, int v, int val) {
    up(x, y, val);
    up(x, v + 1, -val);
    up(u + 1, y, -val);
    up(u + 1, v + 1, val);
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n >> q;
    string str; cin >> str; str = " " + str;
    set<int> s = {0, n + 1};
    for(int i = 1; i <= n; i++) {
        a[i] = str[i] - '0';
        if (!a[i]) s.insert(i);
    }
    for(int timer = 1; timer <= q; timer++) {
        string ask; cin >> ask;
        if (ask[0] == 't') {
            int i; cin >> i;
            a[i] ^= 1;
            if (a[i]) s.erase(s.find(i));
            else s.insert(i);
            auto it = s.upper_bound(i);
            int r = *it - 1;
            it--;
            if (*it == i) it--;
            int l = *it + 1;
            fakeinc(l, i, i, r);
            query[timer] = {0, l, r};
            tt[timer].fi = i;
            tt[timer].se = a[i] ? -timer : timer;
        }
        else {
            int l, r; cin >> l >> r; r--;
            query[timer] = {1, l, r};
            if (*s.lower_bound(l) > r) ans[timer] += timer;
        }
    }
    compress();
    for(int timer = 1; timer <= q; timer++) {
        int type, l, r; tie(type, l, r) = query[timer];
        if (type == 0) {
            int i, val; tie(i, val) = tt[timer];
            inc(l, i, i, r, val);
        }
        else cout << get(l, r) + ans[timer] << endl;
    }
}

Compilation message

street_lamps.cpp: In function 'void fakeup(int, int)':
street_lamps.cpp:14:9: warning: statement has no effect [-Wunused-value]
   14 |     for(x; x <= n; x += x & -x) node[x].push_back(y);
      |         ^
street_lamps.cpp: In function 'void up(int, int, int)':
street_lamps.cpp:17:9: warning: statement has no effect [-Wunused-value]
   17 |     for(x; x <= n; x += x & -x) {
      |         ^
street_lamps.cpp:18:90: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         for(int y = upper_bound(node[x].begin(), node[x].end(), yy) - node[x].begin(); y <= node[x].size(); y += y & -y) {
      |                                                                                        ~~^~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int get(int, int)':
street_lamps.cpp:25:9: warning: statement has no effect [-Wunused-value]
   25 |     for(x; x; x -= x & -x) {
      |         ^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:55:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:56:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:59:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:60:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14336 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 242 ms 33080 KB Output is correct
2 Correct 309 ms 34668 KB Output is correct
3 Correct 545 ms 41088 KB Output is correct
4 Correct 1369 ms 77704 KB Output is correct
5 Correct 1115 ms 63508 KB Output is correct
6 Correct 1534 ms 84760 KB Output is correct
7 Correct 336 ms 43404 KB Output is correct
8 Correct 127 ms 31852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14548 KB Output is correct
2 Correct 10 ms 14528 KB Output is correct
3 Correct 10 ms 14548 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 2414 ms 100380 KB Output is correct
6 Correct 1769 ms 84772 KB Output is correct
7 Correct 1090 ms 61740 KB Output is correct
8 Correct 128 ms 31900 KB Output is correct
9 Correct 74 ms 19276 KB Output is correct
10 Correct 83 ms 19700 KB Output is correct
11 Correct 79 ms 19672 KB Output is correct
12 Correct 326 ms 43576 KB Output is correct
13 Correct 128 ms 31876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14548 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 10 ms 14548 KB Output is correct
5 Correct 329 ms 37348 KB Output is correct
6 Correct 918 ms 62120 KB Output is correct
7 Correct 1616 ms 83832 KB Output is correct
8 Correct 2269 ms 105792 KB Output is correct
9 Correct 390 ms 43448 KB Output is correct
10 Correct 341 ms 41028 KB Output is correct
11 Correct 386 ms 43772 KB Output is correct
12 Correct 368 ms 41672 KB Output is correct
13 Correct 402 ms 43656 KB Output is correct
14 Correct 342 ms 41612 KB Output is correct
15 Correct 321 ms 43504 KB Output is correct
16 Correct 136 ms 31948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 8 ms 14336 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 10 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 242 ms 33080 KB Output is correct
9 Correct 309 ms 34668 KB Output is correct
10 Correct 545 ms 41088 KB Output is correct
11 Correct 1369 ms 77704 KB Output is correct
12 Correct 1115 ms 63508 KB Output is correct
13 Correct 1534 ms 84760 KB Output is correct
14 Correct 336 ms 43404 KB Output is correct
15 Correct 127 ms 31852 KB Output is correct
16 Correct 9 ms 14548 KB Output is correct
17 Correct 10 ms 14528 KB Output is correct
18 Correct 10 ms 14548 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 2414 ms 100380 KB Output is correct
21 Correct 1769 ms 84772 KB Output is correct
22 Correct 1090 ms 61740 KB Output is correct
23 Correct 128 ms 31900 KB Output is correct
24 Correct 74 ms 19276 KB Output is correct
25 Correct 83 ms 19700 KB Output is correct
26 Correct 79 ms 19672 KB Output is correct
27 Correct 326 ms 43576 KB Output is correct
28 Correct 128 ms 31876 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 8 ms 14548 KB Output is correct
31 Correct 9 ms 14548 KB Output is correct
32 Correct 10 ms 14548 KB Output is correct
33 Correct 329 ms 37348 KB Output is correct
34 Correct 918 ms 62120 KB Output is correct
35 Correct 1616 ms 83832 KB Output is correct
36 Correct 2269 ms 105792 KB Output is correct
37 Correct 390 ms 43448 KB Output is correct
38 Correct 341 ms 41028 KB Output is correct
39 Correct 386 ms 43772 KB Output is correct
40 Correct 368 ms 41672 KB Output is correct
41 Correct 402 ms 43656 KB Output is correct
42 Correct 342 ms 41612 KB Output is correct
43 Correct 321 ms 43504 KB Output is correct
44 Correct 136 ms 31948 KB Output is correct
45 Correct 101 ms 24248 KB Output is correct
46 Correct 162 ms 25996 KB Output is correct
47 Correct 679 ms 45380 KB Output is correct
48 Correct 1350 ms 76504 KB Output is correct
49 Correct 87 ms 20108 KB Output is correct
50 Correct 83 ms 19980 KB Output is correct
51 Correct 86 ms 19916 KB Output is correct
52 Correct 85 ms 19500 KB Output is correct
53 Correct 86 ms 19280 KB Output is correct
54 Correct 84 ms 19688 KB Output is correct