답안 #879873

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879873 2023-11-28T08:56:51 Z efedmrlr Fire (JOI20_ho_t5) C++17
100 / 100
648 ms 79572 KB
#include <bits/stdc++.h>

using namespace std;

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } 
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl


#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e16;
const int N = 2e5+5;
const int ALPH = 26;
const int LGN = 20;
const int MOD = 1e9+7;
int n,m,q;

int lastBIT(int x) {
    return x & (-x);
}

struct BITree {
    vector<int> data;
    int sz;
    BITree(int sz) {
        data.assign(sz + 5, 0);
        this->sz = sz;
    }
    void update(int ind, int val) {
        ind++;
        while(ind <= sz) {
            data[ind] += val;
            ind = ind + lastBIT(ind);
        }
    }
    
    int query(int ind) {
        ind++;
        int ret = 0ll;
        while(ind > 0ll) {
            ret += data[ind];
            ind = ind - lastBIT(ind);
        }
        return ret;
    }
    int query(int l, int r) {
        return query(r) - query(l - 1ll);
    }
    void set_el(int ind, int val) {
        update(ind, val - query(ind, ind));
    } 
};
struct Event {
    int t, el, type;
    Event() {
        t = 0; el = 0; type = 0;
    }
    Event(int tm, int e, int ty) {
        t = tm;
        el = e;
        type = ty;
    }
    const bool operator<(const Event &oth) {
        return t < oth.t;
    }
    void print() {
        cout<<"time: "<<t<<" el:"<<el<<" type: "<<type;
        cout<<"\n";
    }
};
struct Query {
    int t, l,r, ind;
    Query() {
        t = 0; l = 0; r = 0; ind = 0;
    }
    Query(int tm, int lf, int rg, int id) {
        t = tm; l = lf; r = rg; ind = id;
    }
    const bool operator<(const Query &oth) {
        return t < oth.t;
    }
    void input(int id) {
        cin>>t>>l>>r;
        ind = id;
    }
    void print() {
        dbg(t); dbg(l); dbg(r); dbg(ind);
    }
};
vector<Event> evs;
/*
type 1 = expansion to stable
type 2 = stable to decay
type 3 = decay to death
times are the start time of coming phase
*/
vector<Query> que;
vector<int> s;
vector<int> lg, rg;
void getEvents() {
    evs.clear();
    for(int i = 1; i<=n; i++) {
        int ev1 = min(rg[i] - i, i - lg[i]), ev2 = max(rg[i] - i, i - lg[i]);
        int ev3 = rg[i] - lg[i];
        evs.pb(Event(ev1, i, 1));
        evs.pb(Event(ev2, i, 2));
        evs.pb(Event(ev3, i, 3));
    }
    sort(evs.rbegin(), evs.rend());
}
void find_greater() {
    set<int> st;
    vector<array<int,2> > sc(n);
    for(int i = 1; i<=n; i++) {
        sc[i - 1] = {s[i], i};
    }
    sort(sc.rbegin(), sc.rend());

    st.insert(0); st.insert(n + 1);
    for(auto c : sc) {
        rg[c[1]] = *st.upper_bound(c[1]);
        st.insert(c[1]); 
    }
    st.clear();
    st.insert(-INF); st.insert(n + 1);
    for(auto c : sc) {
        lg[c[1]] = *prev(st.upper_bound(c[1]));
        st.insert(c[1]); 
    }

}
vector<int> anc[LGN];
void calc_anc() {
    for(int i = 0; i<LGN; i++) {
        anc[i].assign(n+3, 0);
    }
    for(int i = 1; i<=n; i++) {
        if(lg[i] == -INF) anc[0][i] = 0ll;
        else anc[0][i] = lg[i];

    }
    for(int k = 1; k<LGN; k++) {
        for(int i = 1; i<=n; i++) {
            anc[k][i] = anc[k - 1][anc[k - 1][i]];
        }
    }
}
int find_anc(int node, int cap) {
    for(int i = LGN - 1; i>=0; i--) {
        if(anc[i][node] != 0 && anc[i][node] >= cap) {
            node = anc[i][node];
        }
    }
    return node;
}

int calcRBound(int tm, int r, BITree &expand, BITree &stable, BITree &decay, BITree &decay_t) {
    int edge = find_anc(r, r - tm);
    // dbg(tm); dbg(r);
    // dbg(edge);
    int ret = 0;
    if(edge > 1) {
        ret += expand.query(edge - 1ll) * (tm + 1ll);
        ret += stable.query(edge - 1ll);
        ret += decay.query(edge - 1ll);
        ret += decay_t.query(edge - 1ll) * tm; 
    }
    int edger = r;
    int edgel = edge;
    if(anc[0][edgel] != 0ll) {
        edgel =max(edgel, anc[0][edgel] + tm + 1ll) ; 
    }
    // dbg(ret);
    ret += s[edge] * max(0ll, edger - edgel + 1ll);
    // dbg(s[edge] * max(0ll, edger - edgel + 1));
    
    return ret;
}

int calcQuery(Query cur, BITree &expand, BITree &stable, BITree &decay, BITree &decay_t) {
    return calcRBound(cur.t, cur.r, expand, stable, decay, decay_t) - 
        (cur.l < 2 ? 0ll : calcRBound(cur.t, cur.l - 1, expand, stable, decay, decay_t)); 
}

inline void solve() {
    cin>>n>>q;
    s.assign(n+3, 0);
    lg.assign(n+3, 0);
    rg.assign(n+3, 0);
    que.assign(q, Query());
    REP(i,n) cin>>s[i + 1];
    REP(i,q) que[i].input(i + 1);
    sort(que.begin(), que.end());
    find_greater();
    getEvents();
    calc_anc();
    // dbg(lg); dbg(rg);
    BITree expand(n + 3), stable(n + 3), decay(n + 3), decay_t(n + 3); 
    vector<int> res(q+3, 0);
    for(int i = 1; i<=n; i++) {
        expand.set_el(i, s[i]);
    }
    // for(int i = 0; i<evs.size(); i++) evs[i].print();
    for(auto cur : que) {

        //adjust Events
        // cur.print();
        while(evs.size() && cur.t >= evs.back().t) {
            auto ev = evs.back();
            evs.pop_back();
            if(ev.type == 1) {
                expand.set_el(ev.el, 0);
                stable.set_el(ev.el, ev.t * s[ev.el]);
            }   
            else if(ev.type == 2) {
                decay.set_el(ev.el, (ev.t - 1) * s[ev.el]) ;
                decay_t.set_el(ev.el, -1ll * s[ev.el]);
            }   
            else if(ev.type == 3) {
                stable.set_el(ev.el, 0);
                decay.set_el(ev.el, 0);
                decay_t.set_el(ev.el, 0);
            } 
            else {
                // ev.print();
                assert(0);
            }      
        }
        // cout<<"events done"<<endl;
        res[cur.ind] = calcQuery(cur, expand, stable, decay, decay_t);
    }
    for(int i = 1; i<=q; i++) {
        cout<<res[i]<<"\n";
    }
}
 
signed main() {
    //fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}

Compilation message

ho_t5.cpp: In function 'std::string to_string(std::string, int, int)':
ho_t5.cpp:7:208: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
      |                                                                                                                                                                                                             ~~~^~~~~~~~~~
ho_t5.cpp: In function 'void solve()':
ho_t5.cpp:14:26: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
      |                          ^
ho_t5.cpp:203:5: note: in expansion of macro 'REP'
  203 |     REP(i,n) cin>>s[i + 1];
      |     ^~~
ho_t5.cpp:14:26: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   14 | #define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
      |                          ^
ho_t5.cpp:204:5: note: in expansion of macro 'REP'
  204 |     REP(i,q) que[i].input(i + 1);
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 432 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 352 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 356 KB Output is correct
27 Correct 1 ms 356 KB Output is correct
28 Correct 1 ms 356 KB Output is correct
29 Correct 1 ms 356 KB Output is correct
30 Correct 1 ms 356 KB Output is correct
31 Correct 1 ms 356 KB Output is correct
32 Correct 1 ms 352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 557 ms 76884 KB Output is correct
3 Correct 567 ms 74796 KB Output is correct
4 Correct 576 ms 75900 KB Output is correct
5 Correct 579 ms 78696 KB Output is correct
6 Correct 557 ms 75840 KB Output is correct
7 Correct 587 ms 76788 KB Output is correct
8 Correct 547 ms 78980 KB Output is correct
9 Correct 566 ms 78760 KB Output is correct
10 Correct 538 ms 76936 KB Output is correct
11 Correct 592 ms 78688 KB Output is correct
12 Correct 524 ms 75480 KB Output is correct
13 Correct 568 ms 77744 KB Output is correct
14 Correct 552 ms 79164 KB Output is correct
15 Correct 538 ms 74220 KB Output is correct
16 Correct 528 ms 74324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 548 ms 75500 KB Output is correct
3 Correct 522 ms 75412 KB Output is correct
4 Correct 543 ms 78616 KB Output is correct
5 Correct 514 ms 76384 KB Output is correct
6 Correct 512 ms 76284 KB Output is correct
7 Correct 554 ms 77216 KB Output is correct
8 Correct 591 ms 74856 KB Output is correct
9 Correct 531 ms 74928 KB Output is correct
10 Correct 523 ms 74744 KB Output is correct
11 Correct 541 ms 76804 KB Output is correct
12 Correct 503 ms 77328 KB Output is correct
13 Correct 542 ms 78124 KB Output is correct
14 Correct 517 ms 76664 KB Output is correct
15 Correct 542 ms 78160 KB Output is correct
16 Correct 648 ms 78296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 485 ms 71900 KB Output is correct
2 Correct 468 ms 70508 KB Output is correct
3 Correct 482 ms 74012 KB Output is correct
4 Correct 484 ms 70452 KB Output is correct
5 Correct 482 ms 70792 KB Output is correct
6 Correct 448 ms 69840 KB Output is correct
7 Correct 518 ms 72212 KB Output is correct
8 Correct 492 ms 71748 KB Output is correct
9 Correct 458 ms 71928 KB Output is correct
10 Correct 463 ms 72712 KB Output is correct
11 Correct 499 ms 72304 KB Output is correct
12 Correct 472 ms 72644 KB Output is correct
13 Correct 505 ms 69640 KB Output is correct
14 Correct 510 ms 71844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 432 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 352 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 356 KB Output is correct
27 Correct 1 ms 356 KB Output is correct
28 Correct 1 ms 356 KB Output is correct
29 Correct 1 ms 356 KB Output is correct
30 Correct 1 ms 356 KB Output is correct
31 Correct 1 ms 356 KB Output is correct
32 Correct 1 ms 352 KB Output is correct
33 Correct 557 ms 76884 KB Output is correct
34 Correct 567 ms 74796 KB Output is correct
35 Correct 576 ms 75900 KB Output is correct
36 Correct 579 ms 78696 KB Output is correct
37 Correct 557 ms 75840 KB Output is correct
38 Correct 587 ms 76788 KB Output is correct
39 Correct 547 ms 78980 KB Output is correct
40 Correct 566 ms 78760 KB Output is correct
41 Correct 538 ms 76936 KB Output is correct
42 Correct 592 ms 78688 KB Output is correct
43 Correct 524 ms 75480 KB Output is correct
44 Correct 568 ms 77744 KB Output is correct
45 Correct 552 ms 79164 KB Output is correct
46 Correct 538 ms 74220 KB Output is correct
47 Correct 528 ms 74324 KB Output is correct
48 Correct 548 ms 75500 KB Output is correct
49 Correct 522 ms 75412 KB Output is correct
50 Correct 543 ms 78616 KB Output is correct
51 Correct 514 ms 76384 KB Output is correct
52 Correct 512 ms 76284 KB Output is correct
53 Correct 554 ms 77216 KB Output is correct
54 Correct 591 ms 74856 KB Output is correct
55 Correct 531 ms 74928 KB Output is correct
56 Correct 523 ms 74744 KB Output is correct
57 Correct 541 ms 76804 KB Output is correct
58 Correct 503 ms 77328 KB Output is correct
59 Correct 542 ms 78124 KB Output is correct
60 Correct 517 ms 76664 KB Output is correct
61 Correct 542 ms 78160 KB Output is correct
62 Correct 648 ms 78296 KB Output is correct
63 Correct 485 ms 71900 KB Output is correct
64 Correct 468 ms 70508 KB Output is correct
65 Correct 482 ms 74012 KB Output is correct
66 Correct 484 ms 70452 KB Output is correct
67 Correct 482 ms 70792 KB Output is correct
68 Correct 448 ms 69840 KB Output is correct
69 Correct 518 ms 72212 KB Output is correct
70 Correct 492 ms 71748 KB Output is correct
71 Correct 458 ms 71928 KB Output is correct
72 Correct 463 ms 72712 KB Output is correct
73 Correct 499 ms 72304 KB Output is correct
74 Correct 472 ms 72644 KB Output is correct
75 Correct 505 ms 69640 KB Output is correct
76 Correct 510 ms 71844 KB Output is correct
77 Correct 570 ms 76732 KB Output is correct
78 Correct 538 ms 79424 KB Output is correct
79 Correct 560 ms 77860 KB Output is correct
80 Correct 646 ms 77008 KB Output is correct
81 Correct 546 ms 75236 KB Output is correct
82 Correct 550 ms 79572 KB Output is correct
83 Correct 604 ms 78156 KB Output is correct
84 Correct 573 ms 75160 KB Output is correct
85 Correct 571 ms 79140 KB Output is correct
86 Correct 565 ms 75640 KB Output is correct
87 Correct 602 ms 78852 KB Output is correct
88 Correct 552 ms 78872 KB Output is correct
89 Correct 559 ms 75152 KB Output is correct
90 Correct 550 ms 77656 KB Output is correct
91 Correct 550 ms 76760 KB Output is correct
92 Correct 531 ms 77964 KB Output is correct
93 Correct 559 ms 79344 KB Output is correct
94 Correct 563 ms 78140 KB Output is correct
95 Correct 580 ms 78268 KB Output is correct
96 Correct 584 ms 76680 KB Output is correct
97 Correct 557 ms 76960 KB Output is correct
98 Correct 595 ms 74184 KB Output is correct
99 Correct 583 ms 77120 KB Output is correct
100 Correct 596 ms 77116 KB Output is correct
101 Correct 638 ms 78844 KB Output is correct
102 Correct 581 ms 78004 KB Output is correct