Submission #1064938

# Submission time Handle Problem Language Result Execution time Memory
1064938 2024-08-18T19:45:44 Z jerzyk Rectangles (IOI19_rect) C++17
100 / 100
2677 ms 674644 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const int II = 2 * 1000 * 1000 * 1000;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 2500 + 7;
const int NN = 1<<13;
int drz[2 * NN];

int tab[N][N];
vector<int> intr[N][N];
vector<pair<int, int>> que[N][N];
bool czy[N];
long long answer = 0;

void Add(int v, int x)
{
    v += NN;
    while(v > 0)
        {drz[v] += x; v /= 2;}
}

int Query(int a, int b)
{
    a += NN - 1; b += NN + 1;
    int ans = 0;
    while(a / 2 != b / 2)
    {
        if(a % 2 == 0) ans += drz[a + 1];
        if(b % 2 == 1) ans += drz[b - 1];
        a /= 2; b /= 2;
    }
    return ans;
}

void CntR(int n, int m)
{
    for(int i = 1; i <= n; ++i)
    {
        vector<pair<int, int>> kol;
        for(int j = m; j >= 1; --j)
        {
            while((int)kol.size() > 0 && kol.back().st <= tab[i][j])
                kol.pop_back();
            int xd = tab[i][j];
            //cerr << "R: " << i << " " << j << "\n";
            for(int k = kol.size() - 1; k >= 0 && tab[i][j - 1] > xd; --k)
            {
                intr[i][j].pb(kol[k].nd - 1);
                //cerr << kol[k].nd - 1 << " ";
                xd = max(xd, kol[k].st);
            }
            //cout << "\n";
            kol.pb(make_pair(tab[i][j], j));
        }
    }
    for(int j = 1; j <= m; ++j)
    {
        vector<pair<int, int>> akt;
        for(int i = j; i <= m; ++i)
            akt.pb(make_pair(i, 1));
        for(int i = 1; i <= n; ++i)
        {
            vector<pair<int, int>> nxt;
            int k = 0;
            //cerr << "DP: " << i << " " << j << "\n";
            for(int l = 0; l < (int)intr[i][j].size(); ++l)
            {
                while(k < (int)akt.size() && akt[k].st < intr[i][j][l])
                    ++k;
                if(k < (int)akt.size() && akt[k].st == intr[i][j][l])
                {
                    nxt.pb(akt[k]);
                    ++k;
                }else
                    nxt.pb(make_pair(intr[i][j][l], i));

                que[i][intr[i][j][l]].pb(make_pair(j, i - nxt.back().nd + 1));
                //cerr << nxt.back().st << " " << nxt.back().nd << "\n";
            }
            akt = nxt;
            intr[i][j].clear();
        }
    }
}

void CntL(int n, int m)
{
    for(int j = 1; j <= m; ++j)
    {
        vector<pair<int, int>> kol;
        for(int i = 1; i <= n; ++i)
        {
            while((int)kol.size() > 0 && kol.back().st <= tab[i][j])
                kol.pop_back();
            int xd = tab[i][j];
            //cerr << "U: " << i << " " << j << "\n";
            for(int k = kol.size() - 1; k >= 0 && tab[i + 1][j] > xd; --k)
            {
                intr[i][j].pb(kol[k].nd + 1);
                //cerr << kol[k].nd + 1 << "\n";
                xd = max(xd, kol[k].st);
            }
            sort(intr[i][j].begin(), intr[i][j].end());
            kol.pb(make_pair(tab[i][j], i));
        }
    }
    for(int i = 1; i <= n; ++i)
    {
        vector<pair<int, int>> akt;
        for(int j = i; j <= n; ++j)
            akt.pb(make_pair(j, 1));
        for(int j = 1; j <= m; ++j)
        {
            vector<pair<int, int>> nxt;
            int k = 0;
            //cerr << "DP: " << i << " " <<  j << "\n";
            for(int l = 0; l < (int)intr[i][j].size(); ++l)
            {
                while(k < (int)akt.size() && akt[k].st < intr[i][j][l])
                    ++k;
                if(k < (int)akt.size() && akt[k].st == intr[i][j][l])
                {
                    nxt.pb(make_pair(akt[k].nd, akt[k].st));
                    ++k;
                }else
                    nxt.pb(make_pair(j, intr[i][j][l]));
                //cerr << nxt.back().nd << " " << nxt.back().st << "\n";
            }
            akt = nxt;
            sort(akt.begin(), akt.end());
            int lim = akt.size();
            k = -1;
            //cout << "QUE: " << i << " " << j << "\n";
            for(int l = 0; l < (int)que[i][j].size(); ++l)
            {
                while(k < lim - 1 && akt[k + 1].st <= que[i][j][l].st)
                {
                    ++k;
                    Add(i - akt[k].nd + 1, 1);
                }
                answer += (ll)Query(1, que[i][j][l].nd);
                //for(int r = 0; r < lim; ++r)
                    //if(akt[r].st <= que[i][j][l].st && i - akt[r].nd + 1 <= que[i][j][l].nd)
                         //++answer;
            }
            for(k = k; k >= 0; --k)
                Add(i - akt[k].nd + 1, -1);

            for(int l = 0; l < (int)akt.size(); ++l)
                swap(akt[l].st, akt[l].nd);
            sort(akt.begin(), akt.end());
            intr[i][j].clear();
        }
    }
}

long long count_rectangles(vector<vector<int>> xd)
{
    int n = xd.size(), m = xd[0].size();
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            tab[i][j] = xd[i - 1][j - 1];
    CntR(n, m);
    CntL(n, m);
    return answer;
}
# Verdict Execution time Memory Grader output
1 Correct 104 ms 295508 KB Output is correct
2 Correct 100 ms 295704 KB Output is correct
3 Correct 101 ms 295764 KB Output is correct
4 Correct 101 ms 295760 KB Output is correct
5 Correct 107 ms 295508 KB Output is correct
6 Correct 111 ms 295632 KB Output is correct
7 Correct 116 ms 295544 KB Output is correct
8 Correct 101 ms 295504 KB Output is correct
9 Correct 106 ms 295760 KB Output is correct
10 Correct 111 ms 295764 KB Output is correct
11 Correct 105 ms 295764 KB Output is correct
12 Correct 102 ms 295760 KB Output is correct
13 Correct 102 ms 295504 KB Output is correct
14 Correct 102 ms 295488 KB Output is correct
15 Correct 116 ms 295480 KB Output is correct
16 Correct 107 ms 295508 KB Output is correct
17 Correct 106 ms 295508 KB Output is correct
18 Correct 98 ms 295580 KB Output is correct
19 Correct 103 ms 295764 KB Output is correct
20 Correct 105 ms 295500 KB Output is correct
21 Correct 99 ms 295508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 295508 KB Output is correct
2 Correct 100 ms 295704 KB Output is correct
3 Correct 101 ms 295764 KB Output is correct
4 Correct 101 ms 295760 KB Output is correct
5 Correct 107 ms 295508 KB Output is correct
6 Correct 111 ms 295632 KB Output is correct
7 Correct 116 ms 295544 KB Output is correct
8 Correct 101 ms 295504 KB Output is correct
9 Correct 106 ms 295760 KB Output is correct
10 Correct 111 ms 295764 KB Output is correct
11 Correct 105 ms 295764 KB Output is correct
12 Correct 102 ms 295760 KB Output is correct
13 Correct 102 ms 295504 KB Output is correct
14 Correct 102 ms 295488 KB Output is correct
15 Correct 116 ms 295480 KB Output is correct
16 Correct 107 ms 295508 KB Output is correct
17 Correct 106 ms 295508 KB Output is correct
18 Correct 98 ms 295580 KB Output is correct
19 Correct 103 ms 295764 KB Output is correct
20 Correct 105 ms 295500 KB Output is correct
21 Correct 99 ms 295508 KB Output is correct
22 Correct 101 ms 296272 KB Output is correct
23 Correct 103 ms 296140 KB Output is correct
24 Correct 105 ms 296244 KB Output is correct
25 Correct 101 ms 296028 KB Output is correct
26 Correct 103 ms 296272 KB Output is correct
27 Correct 105 ms 296204 KB Output is correct
28 Correct 105 ms 296020 KB Output is correct
29 Correct 104 ms 296044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 295508 KB Output is correct
2 Correct 100 ms 295704 KB Output is correct
3 Correct 101 ms 295764 KB Output is correct
4 Correct 101 ms 295760 KB Output is correct
5 Correct 107 ms 295508 KB Output is correct
6 Correct 111 ms 295632 KB Output is correct
7 Correct 116 ms 295544 KB Output is correct
8 Correct 101 ms 295504 KB Output is correct
9 Correct 106 ms 295760 KB Output is correct
10 Correct 111 ms 295764 KB Output is correct
11 Correct 105 ms 295764 KB Output is correct
12 Correct 102 ms 295760 KB Output is correct
13 Correct 102 ms 295504 KB Output is correct
14 Correct 102 ms 295488 KB Output is correct
15 Correct 116 ms 295480 KB Output is correct
16 Correct 107 ms 295508 KB Output is correct
17 Correct 101 ms 296272 KB Output is correct
18 Correct 103 ms 296140 KB Output is correct
19 Correct 105 ms 296244 KB Output is correct
20 Correct 101 ms 296028 KB Output is correct
21 Correct 103 ms 296272 KB Output is correct
22 Correct 105 ms 296204 KB Output is correct
23 Correct 105 ms 296020 KB Output is correct
24 Correct 104 ms 296044 KB Output is correct
25 Correct 106 ms 295508 KB Output is correct
26 Correct 98 ms 295580 KB Output is correct
27 Correct 103 ms 295764 KB Output is correct
28 Correct 105 ms 295500 KB Output is correct
29 Correct 99 ms 295508 KB Output is correct
30 Correct 109 ms 298652 KB Output is correct
31 Correct 109 ms 298580 KB Output is correct
32 Correct 106 ms 298756 KB Output is correct
33 Correct 107 ms 297808 KB Output is correct
34 Correct 113 ms 298324 KB Output is correct
35 Correct 114 ms 298324 KB Output is correct
36 Correct 111 ms 298324 KB Output is correct
37 Correct 114 ms 298320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 295508 KB Output is correct
2 Correct 100 ms 295704 KB Output is correct
3 Correct 101 ms 295764 KB Output is correct
4 Correct 101 ms 295760 KB Output is correct
5 Correct 107 ms 295508 KB Output is correct
6 Correct 111 ms 295632 KB Output is correct
7 Correct 116 ms 295544 KB Output is correct
8 Correct 101 ms 295504 KB Output is correct
9 Correct 106 ms 295760 KB Output is correct
10 Correct 111 ms 295764 KB Output is correct
11 Correct 105 ms 295764 KB Output is correct
12 Correct 102 ms 295760 KB Output is correct
13 Correct 102 ms 295504 KB Output is correct
14 Correct 102 ms 295488 KB Output is correct
15 Correct 116 ms 295480 KB Output is correct
16 Correct 107 ms 295508 KB Output is correct
17 Correct 101 ms 296272 KB Output is correct
18 Correct 103 ms 296140 KB Output is correct
19 Correct 105 ms 296244 KB Output is correct
20 Correct 101 ms 296028 KB Output is correct
21 Correct 103 ms 296272 KB Output is correct
22 Correct 105 ms 296204 KB Output is correct
23 Correct 105 ms 296020 KB Output is correct
24 Correct 104 ms 296044 KB Output is correct
25 Correct 109 ms 298652 KB Output is correct
26 Correct 109 ms 298580 KB Output is correct
27 Correct 106 ms 298756 KB Output is correct
28 Correct 107 ms 297808 KB Output is correct
29 Correct 113 ms 298324 KB Output is correct
30 Correct 114 ms 298324 KB Output is correct
31 Correct 111 ms 298324 KB Output is correct
32 Correct 114 ms 298320 KB Output is correct
33 Correct 106 ms 295508 KB Output is correct
34 Correct 98 ms 295580 KB Output is correct
35 Correct 103 ms 295764 KB Output is correct
36 Correct 105 ms 295500 KB Output is correct
37 Correct 99 ms 295508 KB Output is correct
38 Correct 174 ms 315988 KB Output is correct
39 Correct 165 ms 313168 KB Output is correct
40 Correct 172 ms 322380 KB Output is correct
41 Correct 167 ms 320848 KB Output is correct
42 Correct 192 ms 327768 KB Output is correct
43 Correct 198 ms 327764 KB Output is correct
44 Correct 197 ms 328016 KB Output is correct
45 Correct 188 ms 325928 KB Output is correct
46 Correct 172 ms 313680 KB Output is correct
47 Correct 195 ms 316500 KB Output is correct
48 Correct 259 ms 323664 KB Output is correct
49 Correct 272 ms 323940 KB Output is correct
50 Correct 179 ms 311052 KB Output is correct
51 Correct 183 ms 309584 KB Output is correct
52 Correct 260 ms 322176 KB Output is correct
53 Correct 275 ms 322384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 296020 KB Output is correct
2 Correct 126 ms 296020 KB Output is correct
3 Correct 133 ms 295516 KB Output is correct
4 Correct 104 ms 295508 KB Output is correct
5 Correct 131 ms 295940 KB Output is correct
6 Correct 128 ms 296196 KB Output is correct
7 Correct 135 ms 295760 KB Output is correct
8 Correct 127 ms 295828 KB Output is correct
9 Correct 131 ms 295764 KB Output is correct
10 Correct 123 ms 295592 KB Output is correct
11 Correct 128 ms 295676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 295508 KB Output is correct
2 Correct 98 ms 295580 KB Output is correct
3 Correct 103 ms 295764 KB Output is correct
4 Correct 105 ms 295500 KB Output is correct
5 Correct 99 ms 295508 KB Output is correct
6 Correct 101 ms 295564 KB Output is correct
7 Correct 647 ms 386136 KB Output is correct
8 Correct 1405 ms 490408 KB Output is correct
9 Correct 1218 ms 491604 KB Output is correct
10 Correct 1207 ms 491600 KB Output is correct
11 Correct 265 ms 331860 KB Output is correct
12 Correct 452 ms 366248 KB Output is correct
13 Correct 480 ms 369232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 295508 KB Output is correct
2 Correct 100 ms 295704 KB Output is correct
3 Correct 101 ms 295764 KB Output is correct
4 Correct 101 ms 295760 KB Output is correct
5 Correct 107 ms 295508 KB Output is correct
6 Correct 111 ms 295632 KB Output is correct
7 Correct 116 ms 295544 KB Output is correct
8 Correct 101 ms 295504 KB Output is correct
9 Correct 106 ms 295760 KB Output is correct
10 Correct 111 ms 295764 KB Output is correct
11 Correct 105 ms 295764 KB Output is correct
12 Correct 102 ms 295760 KB Output is correct
13 Correct 102 ms 295504 KB Output is correct
14 Correct 102 ms 295488 KB Output is correct
15 Correct 116 ms 295480 KB Output is correct
16 Correct 107 ms 295508 KB Output is correct
17 Correct 101 ms 296272 KB Output is correct
18 Correct 103 ms 296140 KB Output is correct
19 Correct 105 ms 296244 KB Output is correct
20 Correct 101 ms 296028 KB Output is correct
21 Correct 103 ms 296272 KB Output is correct
22 Correct 105 ms 296204 KB Output is correct
23 Correct 105 ms 296020 KB Output is correct
24 Correct 104 ms 296044 KB Output is correct
25 Correct 109 ms 298652 KB Output is correct
26 Correct 109 ms 298580 KB Output is correct
27 Correct 106 ms 298756 KB Output is correct
28 Correct 107 ms 297808 KB Output is correct
29 Correct 113 ms 298324 KB Output is correct
30 Correct 114 ms 298324 KB Output is correct
31 Correct 111 ms 298324 KB Output is correct
32 Correct 114 ms 298320 KB Output is correct
33 Correct 174 ms 315988 KB Output is correct
34 Correct 165 ms 313168 KB Output is correct
35 Correct 172 ms 322380 KB Output is correct
36 Correct 167 ms 320848 KB Output is correct
37 Correct 192 ms 327768 KB Output is correct
38 Correct 198 ms 327764 KB Output is correct
39 Correct 197 ms 328016 KB Output is correct
40 Correct 188 ms 325928 KB Output is correct
41 Correct 172 ms 313680 KB Output is correct
42 Correct 195 ms 316500 KB Output is correct
43 Correct 259 ms 323664 KB Output is correct
44 Correct 272 ms 323940 KB Output is correct
45 Correct 179 ms 311052 KB Output is correct
46 Correct 183 ms 309584 KB Output is correct
47 Correct 260 ms 322176 KB Output is correct
48 Correct 275 ms 322384 KB Output is correct
49 Correct 129 ms 296020 KB Output is correct
50 Correct 126 ms 296020 KB Output is correct
51 Correct 133 ms 295516 KB Output is correct
52 Correct 104 ms 295508 KB Output is correct
53 Correct 131 ms 295940 KB Output is correct
54 Correct 128 ms 296196 KB Output is correct
55 Correct 135 ms 295760 KB Output is correct
56 Correct 127 ms 295828 KB Output is correct
57 Correct 131 ms 295764 KB Output is correct
58 Correct 123 ms 295592 KB Output is correct
59 Correct 128 ms 295676 KB Output is correct
60 Correct 101 ms 295564 KB Output is correct
61 Correct 647 ms 386136 KB Output is correct
62 Correct 1405 ms 490408 KB Output is correct
63 Correct 1218 ms 491604 KB Output is correct
64 Correct 1207 ms 491600 KB Output is correct
65 Correct 265 ms 331860 KB Output is correct
66 Correct 452 ms 366248 KB Output is correct
67 Correct 480 ms 369232 KB Output is correct
68 Correct 106 ms 295508 KB Output is correct
69 Correct 98 ms 295580 KB Output is correct
70 Correct 103 ms 295764 KB Output is correct
71 Correct 105 ms 295500 KB Output is correct
72 Correct 99 ms 295508 KB Output is correct
73 Correct 1221 ms 518856 KB Output is correct
74 Correct 964 ms 485204 KB Output is correct
75 Correct 1374 ms 597632 KB Output is correct
76 Correct 1245 ms 582992 KB Output is correct
77 Correct 1504 ms 674644 KB Output is correct
78 Correct 1597 ms 490576 KB Output is correct
79 Correct 1659 ms 510800 KB Output is correct
80 Correct 2524 ms 620624 KB Output is correct
81 Correct 1663 ms 491708 KB Output is correct
82 Correct 2116 ms 562040 KB Output is correct
83 Correct 2677 ms 622676 KB Output is correct
84 Correct 1481 ms 480332 KB Output is correct
85 Correct 2580 ms 605536 KB Output is correct
86 Correct 2447 ms 596048 KB Output is correct
87 Correct 1060 ms 531052 KB Output is correct
88 Correct 1681 ms 674332 KB Output is correct
89 Correct 1527 ms 674384 KB Output is correct
90 Correct 1487 ms 674388 KB Output is correct