Submission #760864

# Submission time Handle Problem Language Result Execution time Memory
760864 2023-06-18T19:34:09 Z sysia Fire (JOI20_ho_t5) C++17
100 / 100
885 ms 121764 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, i;
    prl(){}
    prl(int _x1, int _x2, int _y1, int _y2, int _i): x1(_x1), x2(_x2), y1(_y1), y2(_y2), 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, a;
void recsweep(vector<prl>&rec, Tree &t, int sgn){
    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 aa = {x.second, rec[abs(x.second)-1].i};
        T b = {y.second, rec[abs(y.second)-1].i};
        if (aa.first >= 0){
            if (b.first >= 0) return aa.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 aa.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] += sgn*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, a[abs(rec[id].i)]);
            }
        } else {
            id--;
            if (rec[id].i < 0){
                ans[-rec[id].i] += sgn*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, -a[abs(rec[id].i)]);
            }
        }
    }
}

// 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;
    a.resize(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, i);
        //na prawo
        if (L[i]+1 == i) continue;
        rectangles.emplace_back(R[i], n+2, R[i]-i, R[i]-L[i]-2, 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, i);
        //na lewo
        rectangles.emplace_back(-n, i-1, 0, i-L[i]-2, 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, 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, -i);
        // parallelograms.emplace_back(l, r, t, t, 0, -i);
        rectangles2.emplace_back(l-t, r-t, t, t, -i);
    }
    recsweep(rectangles, tt, -1);
    recsweep(rectangles2, tt, 1);
    // 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);

    solve();

    return 0;
}
# Verdict Execution time Memory 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 452 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 1 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 340 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 679 ms 95376 KB Output is correct
3 Correct 651 ms 95044 KB Output is correct
4 Correct 670 ms 95680 KB Output is correct
5 Correct 692 ms 95932 KB Output is correct
6 Correct 650 ms 95304 KB Output is correct
7 Correct 662 ms 96312 KB Output is correct
8 Correct 668 ms 96416 KB Output is correct
9 Correct 670 ms 96332 KB Output is correct
10 Correct 642 ms 94804 KB Output is correct
11 Correct 666 ms 96412 KB Output is correct
12 Correct 637 ms 94492 KB Output is correct
13 Correct 676 ms 95920 KB Output is correct
14 Correct 666 ms 96556 KB Output is correct
15 Correct 680 ms 95952 KB Output is correct
16 Correct 665 ms 95428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 662 ms 94940 KB Output is correct
3 Correct 636 ms 96936 KB Output is correct
4 Correct 669 ms 95896 KB Output is correct
5 Correct 652 ms 94072 KB Output is correct
6 Correct 662 ms 94648 KB Output is correct
7 Correct 680 ms 94924 KB Output is correct
8 Correct 671 ms 94684 KB Output is correct
9 Correct 640 ms 94028 KB Output is correct
10 Correct 653 ms 93424 KB Output is correct
11 Correct 683 ms 95664 KB Output is correct
12 Correct 665 ms 94796 KB Output is correct
13 Correct 691 ms 96684 KB Output is correct
14 Correct 660 ms 94188 KB Output is correct
15 Correct 666 ms 96668 KB Output is correct
16 Correct 660 ms 94728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 845 ms 119612 KB Output is correct
2 Correct 850 ms 120008 KB Output is correct
3 Correct 879 ms 121764 KB Output is correct
4 Correct 861 ms 119784 KB Output is correct
5 Correct 839 ms 119952 KB Output is correct
6 Correct 849 ms 120080 KB Output is correct
7 Correct 885 ms 121200 KB Output is correct
8 Correct 869 ms 120980 KB Output is correct
9 Correct 849 ms 120080 KB Output is correct
10 Correct 868 ms 120676 KB Output is correct
11 Correct 848 ms 120452 KB Output is correct
12 Correct 872 ms 120588 KB Output is correct
13 Correct 854 ms 120228 KB Output is correct
14 Correct 860 ms 120532 KB Output is correct
# Verdict Execution time Memory 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 452 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 1 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 340 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 679 ms 95376 KB Output is correct
34 Correct 651 ms 95044 KB Output is correct
35 Correct 670 ms 95680 KB Output is correct
36 Correct 692 ms 95932 KB Output is correct
37 Correct 650 ms 95304 KB Output is correct
38 Correct 662 ms 96312 KB Output is correct
39 Correct 668 ms 96416 KB Output is correct
40 Correct 670 ms 96332 KB Output is correct
41 Correct 642 ms 94804 KB Output is correct
42 Correct 666 ms 96412 KB Output is correct
43 Correct 637 ms 94492 KB Output is correct
44 Correct 676 ms 95920 KB Output is correct
45 Correct 666 ms 96556 KB Output is correct
46 Correct 680 ms 95952 KB Output is correct
47 Correct 665 ms 95428 KB Output is correct
48 Correct 662 ms 94940 KB Output is correct
49 Correct 636 ms 96936 KB Output is correct
50 Correct 669 ms 95896 KB Output is correct
51 Correct 652 ms 94072 KB Output is correct
52 Correct 662 ms 94648 KB Output is correct
53 Correct 680 ms 94924 KB Output is correct
54 Correct 671 ms 94684 KB Output is correct
55 Correct 640 ms 94028 KB Output is correct
56 Correct 653 ms 93424 KB Output is correct
57 Correct 683 ms 95664 KB Output is correct
58 Correct 665 ms 94796 KB Output is correct
59 Correct 691 ms 96684 KB Output is correct
60 Correct 660 ms 94188 KB Output is correct
61 Correct 666 ms 96668 KB Output is correct
62 Correct 660 ms 94728 KB Output is correct
63 Correct 845 ms 119612 KB Output is correct
64 Correct 850 ms 120008 KB Output is correct
65 Correct 879 ms 121764 KB Output is correct
66 Correct 861 ms 119784 KB Output is correct
67 Correct 839 ms 119952 KB Output is correct
68 Correct 849 ms 120080 KB Output is correct
69 Correct 885 ms 121200 KB Output is correct
70 Correct 869 ms 120980 KB Output is correct
71 Correct 849 ms 120080 KB Output is correct
72 Correct 868 ms 120676 KB Output is correct
73 Correct 848 ms 120452 KB Output is correct
74 Correct 872 ms 120588 KB Output is correct
75 Correct 854 ms 120228 KB Output is correct
76 Correct 860 ms 120532 KB Output is correct
77 Correct 728 ms 95336 KB Output is correct
78 Correct 728 ms 96540 KB Output is correct
79 Correct 732 ms 96040 KB Output is correct
80 Correct 714 ms 95288 KB Output is correct
81 Correct 724 ms 95056 KB Output is correct
82 Correct 749 ms 95792 KB Output is correct
83 Correct 714 ms 95936 KB Output is correct
84 Correct 712 ms 95104 KB Output is correct
85 Correct 739 ms 95864 KB Output is correct
86 Correct 772 ms 95344 KB Output is correct
87 Correct 765 ms 96564 KB Output is correct
88 Correct 737 ms 96216 KB Output is correct
89 Correct 722 ms 94708 KB Output is correct
90 Correct 740 ms 95840 KB Output is correct
91 Correct 727 ms 96576 KB Output is correct
92 Correct 715 ms 94400 KB Output is correct
93 Correct 741 ms 95276 KB Output is correct
94 Correct 752 ms 96816 KB Output is correct
95 Correct 769 ms 96460 KB Output is correct
96 Correct 730 ms 95572 KB Output is correct
97 Correct 730 ms 95604 KB Output is correct
98 Correct 732 ms 94800 KB Output is correct
99 Correct 746 ms 95292 KB Output is correct
100 Correct 759 ms 95764 KB Output is correct
101 Correct 750 ms 95076 KB Output is correct
102 Correct 771 ms 96408 KB Output is correct