Submission #605254

# Submission time Handle Problem Language Result Execution time Memory
605254 2022-07-25T14:43:04 Z IWannaHaveAGirlfriend Street Lamps (APIO19_street_lamps) C++17
100 / 100
1032 ms 102428 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 3e5 + 1;
struct SegmentTree
{
    struct FenwickTree
    {
        vector<int> tree;
        int sz;
        void Assign(int _sz)
        {
            sz = _sz;
            tree.resize(sz, 0);
        }
        void add(int pos, int val)
        {
            for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
            {
                tree[pos] += val;
            }
        }
        int sum(int pos)
        {
            int ret = 0;
            for (; pos < sz; pos |= pos + 1)
            {
                ret += tree[pos];
            }
            return ret;
        }
    } tree[4 * N + 1];
    vector<int> num[4 * N + 1];
    void add_num(int v, int TreeL, int TreeR, int L, int R, int val)
    {
        if (L > R)
        {
            return ;
        }
        if (TreeL == L && TreeR == R)
        {
            num[v].push_back(val);
            return ;
        }
        int mid = (TreeL + TreeR)/2;
        add_num(v * 2, TreeL, mid, L, min(R, mid), val);
        add_num(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R, val);
    }
    void build(int v, int TreeL, int TreeR)
    {
        sort(num[v].begin(), num[v].end());
        num[v].erase(unique(num[v].begin(), num[v].end()), num[v].end());
        tree[v].Assign(num[v].size());
        if (TreeL == TreeR)
        {
            return ;
        }
        int mid = (TreeL + TreeR)/2;
        build(v * 2, TreeL, mid);
        build(v * 2 + 1, mid + 1, TreeR);
    }
    int convert(int v, int x)
    {
        return lower_bound(num[v].begin(), num[v].end(), x) - num[v].begin();
    }
    void add_val(int v, int TreeL, int TreeR, int L, int R, int num, int val)
    {
        if (L > R)
        {
            return ;
        }
        if (TreeL == L && TreeR == R)
        {
            tree[v].add(convert(v, num), val);
            return ;
        }
        int mid = (TreeL + TreeR)/2;
        add_val(v * 2, TreeL, mid, L, min(R, mid), num, val);
        add_val(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R, num, val);
    }
    int get(int v, int TreeL, int TreeR, int pos, int fn)
    {
        int ret = tree[v].sum(convert(v, fn));
        if (TreeL == TreeR)
        {
            return ret;
        }
        int mid = (TreeL + TreeR)/2;
        if (pos <= mid)
        {
            return ret + get(v * 2, TreeL, mid, pos, fn);
        }
        else
        {
            return ret + get(v * 2 + 1, mid + 1, TreeR, pos, fn);
        }
    }
} tree;
char lamp[N + 1], st_lamp[N + 1];
int n, q, a[N + 1], b[N + 1];
set<tuple<int, int, int> > st_state, cur;
void read()
{
    cin >> n >> q;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> st_lamp[i];
        lamp[i] = st_lamp[i];
    }
    int l = -1, r = -1;
    for (int i = 1; i <= n; ++ i)
    {
        if (lamp[i] == '0')
        {
            if (l != -1 && r != -1)
            {
                st_state.insert({l, r, 0});
                l = r = -1;
            }
        }
        else
        {
            if (l == -1)
            {
                l = i;
            }
            r = i + 1;
        }
    }
    if (l != -1 && r != -1)
    {
        st_state.insert({l, r, 0});
    }
    cur = st_state;
}
set<tuple<int, int, int> > :: iterator contain(int x)
{
    set<tuple<int, int, int> > :: iterator it = cur.upper_bound({x, N + 10, N + 10});
    if (it != cur.begin())
    {
        --it;
    }
    if (it == cur.end())
    {
        return it;
    }
    int l, r, t;
    tie(l, r, t) = *it;
    if (l <= x && x <= r)
    {
        return it;
    }
    else
    {
        return cur.end();
    }
}
void solve()
{
    for (auto &[l, r, t] : cur)
    {
        //cerr << l << ' ' << r << '\n';
        tree.add_num(1, 1, n + 1, l, r, r);
    }
    for (int i = 1; i <= q; ++ i)
    {
        string s;
        cin >> s;
        if (s == "query")
        {
            cin >> a[i] >> b[i];
        }
        else
        {
            cin >> a[i];
            if (lamp[a[i]] == '0')
            {
                lamp[a[i]] = '1';
                int l = a[i], r = a[i] + 1;
                auto it = contain(a[i] + 1);
                if (it != cur.end())
                {
                    r = get<1> (*it);
                    cur.erase(it);
                }
                it = contain(a[i]);
                if (it != cur.end())
                {
                    l = get<0> (*it);
                    cur.erase(it);
                }
                //cerr << l << ' ' << r << '\n';
                cur.insert({l, r, i});
                tree.add_num(1, 1, n + 1, l, r, r);
            }
            else
            {
                lamp[a[i]] = '0';
                auto it = contain(a[i]);
                int l = get<0> (*it), r = get<1> (*it);
                cur.erase(it);
                //cerr << l << ' ' << r << '\n';
                if (l < a[i])
                {
                    cur.insert({l, a[i], i});
                    tree.add_num(1, 1, n + 1, l, a[i], a[i]);
                }
                if (r > a[i] + 1)
                {
                    cur.insert({a[i] + 1, r, i});
                    tree.add_num(1, 1, n + 1, a[i] + 1, r, r);
                }
            }
        }
    }
    tree.build(1, 1, n + 1);
    cur = st_state;
    copy(st_lamp + 1, st_lamp + n + 1, lamp + 1);
    for (auto &[l, r, t] : cur)
    {
        //cerr << l << ' ' << r << '\n';
        //tree.add_val(1, 1, n + 1, l, r, r, 1);
    }
    for (int i = 1; i <= q; ++ i)
    {
        if (b[i]) // query
        {
            int res = tree.get(1, 1, n + 1, a[i], b[i]);
            auto it = contain(a[i]);
            if (it != cur.end() && get<1> (*it) >= b[i])
            {
                res += i - get<2> (*it);
            }
            cout << res << '\n';
        }
        else // update
        {
            if (lamp[a[i]] == '0')
            {
                lamp[a[i]] = '1';
                int l = a[i], r = a[i] + 1;
                auto it = contain(a[i] + 1);
                if (it != cur.end())
                {
                    r = get<1> (*it);
                    tree.add_val(1, 1, n + 1, a[i] + 1, r, r, i - get<2> (*it));
                    cur.erase(it);
                }
                it = contain(a[i]);
                if (it != cur.end())
                {
                    l = get<0> (*it);
                    tree.add_val(1, 1, n + 1, l, a[i], a[i], i - get<2> (*it));
                    cur.erase(it);
                }
                cur.insert({l, r, i});
            }
            else
            {
                lamp[a[i]] = '0';
                auto it = contain(a[i]);
                int l, r, t;
                tie(l, r, t) = *it;
                cur.erase(it);
                tree.add_val(1, 1, n + 1, l, r, r, i - t);
                if (l < a[i])
                {
                    cur.insert({l, a[i], i});
                    //tree.add_val(1, 1, n + 1, l, a[i], a[i], i - 1 - t);
                }
                if (r > a[i] + 1)
                {
                    cur.insert({a[i] + 1, r, i});
                    //tree.add_val(1, 1, n + 1, a[i] + 1, r, r, i - 1 - t);
                }
            }
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}

Compilation message

street_lamps.cpp: In function 'void solve()':
street_lamps.cpp:222:16: warning: unused structured binding declaration [-Wunused-variable]
  222 |     for (auto &[l, r, t] : cur)
      |                ^~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:289:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  289 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 65996 KB Output is correct
2 Correct 33 ms 66004 KB Output is correct
3 Correct 33 ms 66056 KB Output is correct
4 Correct 35 ms 66064 KB Output is correct
5 Correct 39 ms 66036 KB Output is correct
6 Correct 37 ms 66052 KB Output is correct
7 Correct 32 ms 66080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 71116 KB Output is correct
2 Correct 242 ms 71276 KB Output is correct
3 Correct 306 ms 71756 KB Output is correct
4 Correct 809 ms 100020 KB Output is correct
5 Correct 809 ms 101212 KB Output is correct
6 Correct 787 ms 100236 KB Output is correct
7 Correct 228 ms 75460 KB Output is correct
8 Correct 223 ms 76896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 66124 KB Output is correct
2 Correct 33 ms 66092 KB Output is correct
3 Correct 31 ms 66112 KB Output is correct
4 Correct 32 ms 66024 KB Output is correct
5 Correct 1032 ms 96168 KB Output is correct
6 Correct 976 ms 98864 KB Output is correct
7 Correct 818 ms 95296 KB Output is correct
8 Correct 218 ms 70896 KB Output is correct
9 Correct 118 ms 68812 KB Output is correct
10 Correct 119 ms 68988 KB Output is correct
11 Correct 112 ms 69072 KB Output is correct
12 Correct 211 ms 69536 KB Output is correct
13 Correct 224 ms 70932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 66068 KB Output is correct
2 Correct 33 ms 66068 KB Output is correct
3 Correct 38 ms 66072 KB Output is correct
4 Correct 34 ms 66092 KB Output is correct
5 Correct 387 ms 88228 KB Output is correct
6 Correct 630 ms 97400 KB Output is correct
7 Correct 768 ms 99832 KB Output is correct
8 Correct 991 ms 102428 KB Output is correct
9 Correct 273 ms 74416 KB Output is correct
10 Correct 252 ms 73672 KB Output is correct
11 Correct 270 ms 74488 KB Output is correct
12 Correct 266 ms 73688 KB Output is correct
13 Correct 261 ms 74392 KB Output is correct
14 Correct 303 ms 73612 KB Output is correct
15 Correct 214 ms 75432 KB Output is correct
16 Correct 224 ms 76876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 65996 KB Output is correct
2 Correct 33 ms 66004 KB Output is correct
3 Correct 33 ms 66056 KB Output is correct
4 Correct 35 ms 66064 KB Output is correct
5 Correct 39 ms 66036 KB Output is correct
6 Correct 37 ms 66052 KB Output is correct
7 Correct 32 ms 66080 KB Output is correct
8 Correct 211 ms 71116 KB Output is correct
9 Correct 242 ms 71276 KB Output is correct
10 Correct 306 ms 71756 KB Output is correct
11 Correct 809 ms 100020 KB Output is correct
12 Correct 809 ms 101212 KB Output is correct
13 Correct 787 ms 100236 KB Output is correct
14 Correct 228 ms 75460 KB Output is correct
15 Correct 223 ms 76896 KB Output is correct
16 Correct 35 ms 66124 KB Output is correct
17 Correct 33 ms 66092 KB Output is correct
18 Correct 31 ms 66112 KB Output is correct
19 Correct 32 ms 66024 KB Output is correct
20 Correct 1032 ms 96168 KB Output is correct
21 Correct 976 ms 98864 KB Output is correct
22 Correct 818 ms 95296 KB Output is correct
23 Correct 218 ms 70896 KB Output is correct
24 Correct 118 ms 68812 KB Output is correct
25 Correct 119 ms 68988 KB Output is correct
26 Correct 112 ms 69072 KB Output is correct
27 Correct 211 ms 69536 KB Output is correct
28 Correct 224 ms 70932 KB Output is correct
29 Correct 34 ms 66068 KB Output is correct
30 Correct 33 ms 66068 KB Output is correct
31 Correct 38 ms 66072 KB Output is correct
32 Correct 34 ms 66092 KB Output is correct
33 Correct 387 ms 88228 KB Output is correct
34 Correct 630 ms 97400 KB Output is correct
35 Correct 768 ms 99832 KB Output is correct
36 Correct 991 ms 102428 KB Output is correct
37 Correct 273 ms 74416 KB Output is correct
38 Correct 252 ms 73672 KB Output is correct
39 Correct 270 ms 74488 KB Output is correct
40 Correct 266 ms 73688 KB Output is correct
41 Correct 261 ms 74392 KB Output is correct
42 Correct 303 ms 73612 KB Output is correct
43 Correct 214 ms 75432 KB Output is correct
44 Correct 224 ms 76876 KB Output is correct
45 Correct 123 ms 71096 KB Output is correct
46 Correct 156 ms 71276 KB Output is correct
47 Correct 345 ms 81452 KB Output is correct
48 Correct 751 ms 99628 KB Output is correct
49 Correct 120 ms 72480 KB Output is correct
50 Correct 123 ms 72496 KB Output is correct
51 Correct 162 ms 72652 KB Output is correct
52 Correct 125 ms 72996 KB Output is correct
53 Correct 128 ms 72996 KB Output is correct
54 Correct 128 ms 73000 KB Output is correct