답안 #760862

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760862 2023-06-18T19:19:11 Z sysia Fire (JOI20_ho_t5) C++17
100 / 100
961 ms 147668 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 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){
    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
        }
    });
    Tree t(2*n+3);
    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);
    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>parallelograms, rectangles;
    for (int i = 1; i<=n; i++) {
        parallelograms.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);
        //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);
    }
    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);
    }
    recsweep(rectangles);
    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 2 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 448 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 448 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 444 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 444 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 755 ms 126396 KB Output is correct
3 Correct 715 ms 126008 KB Output is correct
4 Correct 719 ms 126592 KB Output is correct
5 Correct 767 ms 126876 KB Output is correct
6 Correct 755 ms 126260 KB Output is correct
7 Correct 726 ms 127156 KB Output is correct
8 Correct 739 ms 127328 KB Output is correct
9 Correct 770 ms 127204 KB Output is correct
10 Correct 694 ms 125944 KB Output is correct
11 Correct 736 ms 127248 KB Output is correct
12 Correct 704 ms 125652 KB Output is correct
13 Correct 728 ms 126756 KB Output is correct
14 Correct 731 ms 126536 KB Output is correct
15 Correct 717 ms 127040 KB Output is correct
16 Correct 744 ms 126500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 722 ms 126892 KB Output is correct
3 Correct 701 ms 125856 KB Output is correct
4 Correct 751 ms 127620 KB Output is correct
5 Correct 707 ms 126036 KB Output is correct
6 Correct 719 ms 126568 KB Output is correct
7 Correct 715 ms 126756 KB Output is correct
8 Correct 691 ms 126684 KB Output is correct
9 Correct 706 ms 126108 KB Output is correct
10 Correct 679 ms 125568 KB Output is correct
11 Correct 760 ms 127420 KB Output is correct
12 Correct 757 ms 126812 KB Output is correct
13 Correct 717 ms 127248 KB Output is correct
14 Correct 692 ms 126116 KB Output is correct
15 Correct 768 ms 127164 KB Output is correct
16 Correct 734 ms 126572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 918 ms 144432 KB Output is correct
2 Correct 887 ms 144948 KB Output is correct
3 Correct 886 ms 147668 KB Output is correct
4 Correct 887 ms 144664 KB Output is correct
5 Correct 867 ms 145136 KB Output is correct
6 Correct 863 ms 145160 KB Output is correct
7 Correct 961 ms 146992 KB Output is correct
8 Correct 928 ms 146456 KB Output is correct
9 Correct 873 ms 145188 KB Output is correct
10 Correct 884 ms 146160 KB Output is correct
11 Correct 939 ms 145804 KB Output is correct
12 Correct 866 ms 145976 KB Output is correct
13 Correct 849 ms 145348 KB Output is correct
14 Correct 896 ms 145836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 448 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 448 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 444 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 444 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 448 KB Output is correct
33 Correct 755 ms 126396 KB Output is correct
34 Correct 715 ms 126008 KB Output is correct
35 Correct 719 ms 126592 KB Output is correct
36 Correct 767 ms 126876 KB Output is correct
37 Correct 755 ms 126260 KB Output is correct
38 Correct 726 ms 127156 KB Output is correct
39 Correct 739 ms 127328 KB Output is correct
40 Correct 770 ms 127204 KB Output is correct
41 Correct 694 ms 125944 KB Output is correct
42 Correct 736 ms 127248 KB Output is correct
43 Correct 704 ms 125652 KB Output is correct
44 Correct 728 ms 126756 KB Output is correct
45 Correct 731 ms 126536 KB Output is correct
46 Correct 717 ms 127040 KB Output is correct
47 Correct 744 ms 126500 KB Output is correct
48 Correct 722 ms 126892 KB Output is correct
49 Correct 701 ms 125856 KB Output is correct
50 Correct 751 ms 127620 KB Output is correct
51 Correct 707 ms 126036 KB Output is correct
52 Correct 719 ms 126568 KB Output is correct
53 Correct 715 ms 126756 KB Output is correct
54 Correct 691 ms 126684 KB Output is correct
55 Correct 706 ms 126108 KB Output is correct
56 Correct 679 ms 125568 KB Output is correct
57 Correct 760 ms 127420 KB Output is correct
58 Correct 757 ms 126812 KB Output is correct
59 Correct 717 ms 127248 KB Output is correct
60 Correct 692 ms 126116 KB Output is correct
61 Correct 768 ms 127164 KB Output is correct
62 Correct 734 ms 126572 KB Output is correct
63 Correct 918 ms 144432 KB Output is correct
64 Correct 887 ms 144948 KB Output is correct
65 Correct 886 ms 147668 KB Output is correct
66 Correct 887 ms 144664 KB Output is correct
67 Correct 867 ms 145136 KB Output is correct
68 Correct 863 ms 145160 KB Output is correct
69 Correct 961 ms 146992 KB Output is correct
70 Correct 928 ms 146456 KB Output is correct
71 Correct 873 ms 145188 KB Output is correct
72 Correct 884 ms 146160 KB Output is correct
73 Correct 939 ms 145804 KB Output is correct
74 Correct 866 ms 145976 KB Output is correct
75 Correct 849 ms 145348 KB Output is correct
76 Correct 896 ms 145836 KB Output is correct
77 Correct 775 ms 126376 KB Output is correct
78 Correct 795 ms 127288 KB Output is correct
79 Correct 775 ms 127088 KB Output is correct
80 Correct 761 ms 126368 KB Output is correct
81 Correct 761 ms 126092 KB Output is correct
82 Correct 806 ms 126728 KB Output is correct
83 Correct 767 ms 126484 KB Output is correct
84 Correct 767 ms 126140 KB Output is correct
85 Correct 796 ms 126828 KB Output is correct
86 Correct 789 ms 126344 KB Output is correct
87 Correct 806 ms 127292 KB Output is correct
88 Correct 797 ms 127092 KB Output is correct
89 Correct 823 ms 125852 KB Output is correct
90 Correct 794 ms 126728 KB Output is correct
91 Correct 801 ms 125964 KB Output is correct
92 Correct 767 ms 125596 KB Output is correct
93 Correct 788 ms 126104 KB Output is correct
94 Correct 803 ms 127316 KB Output is correct
95 Correct 863 ms 127152 KB Output is correct
96 Correct 800 ms 126412 KB Output is correct
97 Correct 779 ms 126400 KB Output is correct
98 Correct 861 ms 125736 KB Output is correct
99 Correct 795 ms 125968 KB Output is correct
100 Correct 832 ms 126464 KB Output is correct
101 Correct 863 ms 125780 KB Output is correct
102 Correct 804 ms 126976 KB Output is correct