Submission #760860

# Submission time Handle Problem Language Result Execution time Memory
760860 2023-06-18T19:15:06 Z sysia Fire (JOI20_ho_t5) C++17
100 / 100
978 ms 151348 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;
const int mod = 998244353;

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;

int sgn(int x){
    if (x >= 0) return 1;
    return -1;
}

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
    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){
                //oba poczatki --> chcemy najpierw update
                return a.second < b.second;
            } else {
                //poczatek i koniec --> najpierw poczatek
                return false;
            }
        } else {
            if (b.first >= 0){
                return true;
            } else {
                //oba konce
                return a.second > b.second;
            }
        }
    });
    Tree t(2*n+3);
    for (auto &[smiec, id]: ord){
        if (id < 0){
            id = -id;
            id--;
            // cerr << "poczatek ";
            debug(rec[id].y1, rec[id].x1, rec[id].x2, rec[id].i, rec[id].val);
            if (rec[id].i < 0){
                //query
                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--;
            // cerr << "koniec ";
            debug(rec[id].y1, rec[id].x1, rec[id].x2, rec[id].i, rec[id].val);
            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);
    }
    for (auto [x1, x2, y1, y2, val, i]: rectangles){
        debug(x1, x2, 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];
    debug(a);
    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);
    }
    debug(L);
    debug(R);
    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);
    }
    for (auto [x1, x2, y1, y2, val, i]: parallelograms){
        debug(x1, x2, y1, y2, val);
    }
    // cerr << "\n";
    for (auto [x1, x2, y1, y2, val, i]: rectangles){
        debug(x1, x2, y1, y2, val);
    }
    // return;
    recsweep(rectangles);
    debug(ans);
    // return;
    // cerr << "\n";
    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;
}

Compilation message

ho_t5.cpp: In function 'void parsweep(std::vector<prl>&)':
ho_t5.cpp:151:15: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
  151 |     for (auto [x1, x2, y1, y2, val, i]: rectangles){
      |               ^~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp: In function 'void solve()':
ho_t5.cpp:197:15: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
  197 |     for (auto [x1, x2, y1, y2, val, i]: parallelograms){
      |               ^~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:201:15: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
  201 |     for (auto [x1, x2, y1, y2, val, i]: rectangles){
      |               ^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 444 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 444 KB Output is correct
14 Correct 1 ms 440 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 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 444 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 468 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 512 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 792 ms 131624 KB Output is correct
3 Correct 793 ms 131208 KB Output is correct
4 Correct 806 ms 131720 KB Output is correct
5 Correct 840 ms 132028 KB Output is correct
6 Correct 806 ms 131244 KB Output is correct
7 Correct 798 ms 132324 KB Output is correct
8 Correct 804 ms 132432 KB Output is correct
9 Correct 817 ms 132356 KB Output is correct
10 Correct 848 ms 131256 KB Output is correct
11 Correct 854 ms 132592 KB Output is correct
12 Correct 812 ms 130868 KB Output is correct
13 Correct 918 ms 131736 KB Output is correct
14 Correct 821 ms 131660 KB Output is correct
15 Correct 842 ms 131760 KB Output is correct
16 Correct 876 ms 131484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 747 ms 132008 KB Output is correct
3 Correct 738 ms 130832 KB Output is correct
4 Correct 768 ms 132740 KB Output is correct
5 Correct 757 ms 130956 KB Output is correct
6 Correct 775 ms 131668 KB Output is correct
7 Correct 763 ms 131792 KB Output is correct
8 Correct 778 ms 131804 KB Output is correct
9 Correct 739 ms 131080 KB Output is correct
10 Correct 726 ms 130544 KB Output is correct
11 Correct 779 ms 132664 KB Output is correct
12 Correct 772 ms 131756 KB Output is correct
13 Correct 769 ms 132476 KB Output is correct
14 Correct 797 ms 131168 KB Output is correct
15 Correct 768 ms 132248 KB Output is correct
16 Correct 791 ms 131640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 937 ms 148096 KB Output is correct
2 Correct 928 ms 148528 KB Output is correct
3 Correct 973 ms 151348 KB Output is correct
4 Correct 978 ms 148508 KB Output is correct
5 Correct 925 ms 148812 KB Output is correct
6 Correct 934 ms 148580 KB Output is correct
7 Correct 932 ms 150444 KB Output is correct
8 Correct 936 ms 150104 KB Output is correct
9 Correct 931 ms 148776 KB Output is correct
10 Correct 923 ms 149608 KB Output is correct
11 Correct 961 ms 149528 KB Output is correct
12 Correct 929 ms 149812 KB Output is correct
13 Correct 959 ms 149104 KB Output is correct
14 Correct 927 ms 149588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 444 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 440 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 1 ms 444 KB Output is correct
14 Correct 1 ms 440 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 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 444 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 468 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 512 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 792 ms 131624 KB Output is correct
34 Correct 793 ms 131208 KB Output is correct
35 Correct 806 ms 131720 KB Output is correct
36 Correct 840 ms 132028 KB Output is correct
37 Correct 806 ms 131244 KB Output is correct
38 Correct 798 ms 132324 KB Output is correct
39 Correct 804 ms 132432 KB Output is correct
40 Correct 817 ms 132356 KB Output is correct
41 Correct 848 ms 131256 KB Output is correct
42 Correct 854 ms 132592 KB Output is correct
43 Correct 812 ms 130868 KB Output is correct
44 Correct 918 ms 131736 KB Output is correct
45 Correct 821 ms 131660 KB Output is correct
46 Correct 842 ms 131760 KB Output is correct
47 Correct 876 ms 131484 KB Output is correct
48 Correct 747 ms 132008 KB Output is correct
49 Correct 738 ms 130832 KB Output is correct
50 Correct 768 ms 132740 KB Output is correct
51 Correct 757 ms 130956 KB Output is correct
52 Correct 775 ms 131668 KB Output is correct
53 Correct 763 ms 131792 KB Output is correct
54 Correct 778 ms 131804 KB Output is correct
55 Correct 739 ms 131080 KB Output is correct
56 Correct 726 ms 130544 KB Output is correct
57 Correct 779 ms 132664 KB Output is correct
58 Correct 772 ms 131756 KB Output is correct
59 Correct 769 ms 132476 KB Output is correct
60 Correct 797 ms 131168 KB Output is correct
61 Correct 768 ms 132248 KB Output is correct
62 Correct 791 ms 131640 KB Output is correct
63 Correct 937 ms 148096 KB Output is correct
64 Correct 928 ms 148528 KB Output is correct
65 Correct 973 ms 151348 KB Output is correct
66 Correct 978 ms 148508 KB Output is correct
67 Correct 925 ms 148812 KB Output is correct
68 Correct 934 ms 148580 KB Output is correct
69 Correct 932 ms 150444 KB Output is correct
70 Correct 936 ms 150104 KB Output is correct
71 Correct 931 ms 148776 KB Output is correct
72 Correct 923 ms 149608 KB Output is correct
73 Correct 961 ms 149528 KB Output is correct
74 Correct 929 ms 149812 KB Output is correct
75 Correct 959 ms 149104 KB Output is correct
76 Correct 927 ms 149588 KB Output is correct
77 Correct 822 ms 131472 KB Output is correct
78 Correct 825 ms 132512 KB Output is correct
79 Correct 833 ms 131992 KB Output is correct
80 Correct 830 ms 131476 KB Output is correct
81 Correct 816 ms 131080 KB Output is correct
82 Correct 810 ms 131812 KB Output is correct
83 Correct 837 ms 131440 KB Output is correct
84 Correct 830 ms 131360 KB Output is correct
85 Correct 835 ms 131824 KB Output is correct
86 Correct 850 ms 131564 KB Output is correct
87 Correct 868 ms 132612 KB Output is correct
88 Correct 834 ms 132324 KB Output is correct
89 Correct 906 ms 131044 KB Output is correct
90 Correct 844 ms 131968 KB Output is correct
91 Correct 823 ms 131104 KB Output is correct
92 Correct 830 ms 130820 KB Output is correct
93 Correct 897 ms 131444 KB Output is correct
94 Correct 890 ms 132944 KB Output is correct
95 Correct 861 ms 132636 KB Output is correct
96 Correct 831 ms 131768 KB Output is correct
97 Correct 864 ms 131888 KB Output is correct
98 Correct 848 ms 130812 KB Output is correct
99 Correct 858 ms 131068 KB Output is correct
100 Correct 829 ms 131668 KB Output is correct
101 Correct 890 ms 131036 KB Output is correct
102 Correct 899 ms 132240 KB Output is correct