답안 #760863

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760863 2023-06-18T19:29:10 Z sysia Fire (JOI20_ho_t5) C++17
100 / 100
919 ms 131032 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;

struct prl{
    //parallelogram 
    int x1, x2, y1, y2, val, i;
    prl(){}
    prl(int _x1, int _x2, int _y1, int _y2, int _v, int _i): x1(_x1), x2(_x2), y1(_y1), y2(_y2), val(_v), i(_i) {}
};


struct Tree{
    vector<int>tab, lazy;
    int size = 1;
    
    Tree(int n){
        while (size < n) size*=2;
        tab.assign(2*size, 0);
        lazy.assign(2*size, 0);
    }

    void clear(){
        tab.assign(2*size, 0);
        lazy.assign(2*size, 0);
    }

    void update(int x, int lx, int rx, int l, int r, int v){
        if (lx > r || rx < l) return;
        if (lx >= l && rx <= r){
            tab[x] += v * (rx-lx+1);
            lazy[x] += v;
            return;            
        }
        push(x, lx, rx);
        int m = (lx+rx)/2;
        update(2*x, lx, m, l, r, v);
        update(2*x+1, m+1, rx, l, r, v);
        tab[x] = tab[2*x] + tab[2*x+1];
    }

    void push(int x, int lx, int rx){
        if (!lazy[x] || lx == rx) return;
        int m = (rx-lx+1)/2;
        tab[2*x] += lazy[x] * m;
        tab[2*x+1] += lazy[x] * m;
        lazy[2*x] += lazy[x];
        lazy[2*x+1] += lazy[x];
        lazy[x] = 0;  
    }

    int query(int x, int lx, int rx, int l, int r){
        if (lx > r || rx < l) return 0ll;
        if (lx >= l && rx <= r) return tab[x];
        push(x, lx, rx);
        int m = (lx+rx)/2;
        return query(2*x, lx, m, l, r) + query(2*x+1, m+1, rx, l, r);
    }
};
int n;

vector<int>ans;
void recsweep(vector<prl>&rec, Tree &t){
    int m = (int)rec.size();
    vector<T>ord;
    for (int i = 1; i<=m; i++){
        ord.emplace_back(rec[i-1].y1, -i);
        if (rec[i-1].i >= 0) ord.emplace_back(rec[i-1].y2, i);
    }
    //najpierw odejmujemy stare, pozniej dodajemy, pozniej zapytania
    stable_sort(ord.begin(), ord.end(), [&](auto x, auto y){
        if (x.first != y.first) return x.first < y.first;
        //ten sam x
        T a = {x.second, rec[abs(x.second)-1].i};
        T b = {y.second, rec[abs(y.second)-1].i};
        if (a.first >= 0){
            if (b.first >= 0) return a.second < b.second; //oba poczatki --> chcemy najpierw update
            else return false; //poczatek i koniec --> najpierw poczatek
        } else {
            if (b.first >= 0) return true;
            else return a.second > b.second; //oba konce
        }
    });
    t.clear();
    for (auto &[smiec, id]: ord){
        if (id < 0){
            id = -id; id--;
            if (rec[id].i < 0){
                ans[-rec[id].i] += t.query(1, 0, t.size-1, n+rec[id].x1, n+rec[id].x2);
            } else {
                t.update(1, 0, t.size-1, n+rec[id].x1, n+rec[id].x2, rec[id].val);
            }
        } else {
            id--;
            if (rec[id].i < 0){
                ans[-rec[id].i] += t.query(1, 0, t.size-1, n+rec[id].x1, n+rec[id].x2);
            } else {
                t.update(1, 0, t.size-1, n+rec[id].x1, n+rec[id].x2, -rec[id].val);
            }
        }
    }
}

// void parsweep(vector<prl>&a){
//     vector<prl>rectangles;
//     for (auto &[x1, x2, y1, y2, val, i]: a){
//         rectangles.emplace_back(x1-y1, x2-y1, y1, y2, val, i);
//     }
//     recsweep(rectangles);
// }

void solve(){
    int q; cin >> n >> q;
    vector<int>a(n+1);
    Tree tt(2*n+3);
    for (int i = 1; i<=n; i++) cin >> a[i];
    stack<int>s;
    ans.assign(q+1, 0);
    vector<int>L(n+1, -n);
    for (int i = 1; i<=n; i++){
        while ((int)s.size() && a[s.top()] <= a[i]) s.pop();
        if ((int)s.size()) L[i] = s.top(); 
        s.push(i);
    }
    vector<int>R(n+1, n+1);
    while ((int)s.size()) s.pop();
    for (int i = n; i>=1; i--) {
        while ((int)s.size() && a[s.top()] < a[i]) s.pop();
        if ((int)s.size()) R[i] = s.top(); 
        s.push(i);
    }
    vector<prl>rectangles2, rectangles;
    for (int i = 1; i<=n; i++) {
        // parallelograms.emplace_back(L[i]+1, i, 0, R[i]-L[i]-2, a[i], i);
        rectangles2.emplace_back(L[i]+1, i, 0, R[i]-L[i]-2, a[i], i);
        //na prawo
        if (L[i]+1 == i) continue;
        rectangles.emplace_back(R[i], n+2, R[i]-i, R[i]-L[i]-2, -a[i], i);
        // parallelograms.emplace_back(R[i]+1, n+2, R[i]-i, R[i]-L[i]-2, a[i], i);
        rectangles2.emplace_back(i+1, n+2-R[i]+i, R[i]-i, R[i]-L[i]-2, a[i], i);
        //na lewo
        rectangles.emplace_back(-n, i-1, 0, i-L[i]-2, -a[i], i);
        // parallelograms.emplace_back(-n, L[i], 0, i-L[i]-2, a[i], i);
        rectangles2.emplace_back(-n, L[i], 0, i-L[i]-2, a[i], i);

        //x1, x2, y1, y2, val, i ----> x1-y1, x2-y1, y1, y2, val, i;
    }
    for (int i = 1; i<=q; i++){
        int t, l, r; cin >> t >> l >> r;
        rectangles.emplace_back(l, r, t, t, 0, -i);
        // parallelograms.emplace_back(l, r, t, t, 0, -i);
        rectangles2.emplace_back(l-t, r-t, t, t, -0, -i);
    }
    recsweep(rectangles, tt);
    recsweep(rectangles2, tt);
    // parsweep(parallelograms);
    for (int i = 1; i<=q; i++) cout << ans[i] << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 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 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 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 652 ms 103048 KB Output is correct
3 Correct 670 ms 102604 KB Output is correct
4 Correct 681 ms 103272 KB Output is correct
5 Correct 664 ms 103620 KB Output is correct
6 Correct 699 ms 102884 KB Output is correct
7 Correct 666 ms 104016 KB Output is correct
8 Correct 702 ms 104188 KB Output is correct
9 Correct 685 ms 104112 KB Output is correct
10 Correct 654 ms 102380 KB Output is correct
11 Correct 706 ms 104040 KB Output is correct
12 Correct 650 ms 102056 KB Output is correct
13 Correct 665 ms 103528 KB Output is correct
14 Correct 678 ms 111332 KB Output is correct
15 Correct 697 ms 110520 KB Output is correct
16 Correct 660 ms 103052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 670 ms 102672 KB Output is correct
3 Correct 719 ms 111868 KB Output is correct
4 Correct 760 ms 108980 KB Output is correct
5 Correct 671 ms 105964 KB Output is correct
6 Correct 675 ms 102256 KB Output is correct
7 Correct 738 ms 102472 KB Output is correct
8 Correct 691 ms 102300 KB Output is correct
9 Correct 656 ms 106052 KB Output is correct
10 Correct 665 ms 100900 KB Output is correct
11 Correct 738 ms 105996 KB Output is correct
12 Correct 736 ms 102428 KB Output is correct
13 Correct 683 ms 111296 KB Output is correct
14 Correct 655 ms 106044 KB Output is correct
15 Correct 702 ms 111240 KB Output is correct
16 Correct 674 ms 102256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 832 ms 128748 KB Output is correct
2 Correct 837 ms 129084 KB Output is correct
3 Correct 874 ms 131032 KB Output is correct
4 Correct 875 ms 128852 KB Output is correct
5 Correct 844 ms 129184 KB Output is correct
6 Correct 853 ms 129048 KB Output is correct
7 Correct 900 ms 130484 KB Output is correct
8 Correct 873 ms 130096 KB Output is correct
9 Correct 851 ms 129164 KB Output is correct
10 Correct 853 ms 129816 KB Output is correct
11 Correct 863 ms 129672 KB Output is correct
12 Correct 846 ms 129844 KB Output is correct
13 Correct 919 ms 129348 KB Output is correct
14 Correct 852 ms 129728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 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 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 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 652 ms 103048 KB Output is correct
34 Correct 670 ms 102604 KB Output is correct
35 Correct 681 ms 103272 KB Output is correct
36 Correct 664 ms 103620 KB Output is correct
37 Correct 699 ms 102884 KB Output is correct
38 Correct 666 ms 104016 KB Output is correct
39 Correct 702 ms 104188 KB Output is correct
40 Correct 685 ms 104112 KB Output is correct
41 Correct 654 ms 102380 KB Output is correct
42 Correct 706 ms 104040 KB Output is correct
43 Correct 650 ms 102056 KB Output is correct
44 Correct 665 ms 103528 KB Output is correct
45 Correct 678 ms 111332 KB Output is correct
46 Correct 697 ms 110520 KB Output is correct
47 Correct 660 ms 103052 KB Output is correct
48 Correct 670 ms 102672 KB Output is correct
49 Correct 719 ms 111868 KB Output is correct
50 Correct 760 ms 108980 KB Output is correct
51 Correct 671 ms 105964 KB Output is correct
52 Correct 675 ms 102256 KB Output is correct
53 Correct 738 ms 102472 KB Output is correct
54 Correct 691 ms 102300 KB Output is correct
55 Correct 656 ms 106052 KB Output is correct
56 Correct 665 ms 100900 KB Output is correct
57 Correct 738 ms 105996 KB Output is correct
58 Correct 736 ms 102428 KB Output is correct
59 Correct 683 ms 111296 KB Output is correct
60 Correct 655 ms 106044 KB Output is correct
61 Correct 702 ms 111240 KB Output is correct
62 Correct 674 ms 102256 KB Output is correct
63 Correct 832 ms 128748 KB Output is correct
64 Correct 837 ms 129084 KB Output is correct
65 Correct 874 ms 131032 KB Output is correct
66 Correct 875 ms 128852 KB Output is correct
67 Correct 844 ms 129184 KB Output is correct
68 Correct 853 ms 129048 KB Output is correct
69 Correct 900 ms 130484 KB Output is correct
70 Correct 873 ms 130096 KB Output is correct
71 Correct 851 ms 129164 KB Output is correct
72 Correct 853 ms 129816 KB Output is correct
73 Correct 863 ms 129672 KB Output is correct
74 Correct 846 ms 129844 KB Output is correct
75 Correct 919 ms 129348 KB Output is correct
76 Correct 852 ms 129728 KB Output is correct
77 Correct 771 ms 102888 KB Output is correct
78 Correct 741 ms 104180 KB Output is correct
79 Correct 753 ms 103692 KB Output is correct
80 Correct 746 ms 102896 KB Output is correct
81 Correct 775 ms 106080 KB Output is correct
82 Correct 746 ms 103392 KB Output is correct
83 Correct 758 ms 110584 KB Output is correct
84 Correct 743 ms 102692 KB Output is correct
85 Correct 778 ms 103472 KB Output is correct
86 Correct 733 ms 106036 KB Output is correct
87 Correct 759 ms 104192 KB Output is correct
88 Correct 750 ms 103864 KB Output is correct
89 Correct 743 ms 102192 KB Output is correct
90 Correct 768 ms 103680 KB Output is correct
91 Correct 741 ms 111460 KB Output is correct
92 Correct 735 ms 101892 KB Output is correct
93 Correct 745 ms 102808 KB Output is correct
94 Correct 787 ms 104552 KB Output is correct
95 Correct 781 ms 104264 KB Output is correct
96 Correct 747 ms 103084 KB Output is correct
97 Correct 774 ms 103232 KB Output is correct
98 Correct 754 ms 102260 KB Output is correct
99 Correct 752 ms 102800 KB Output is correct
100 Correct 755 ms 103388 KB Output is correct
101 Correct 772 ms 102564 KB Output is correct
102 Correct 810 ms 104216 KB Output is correct