Editorial for Zigzag


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        map<int, map<int, int>> segs;
        auto add_segment = [&](int x, int y, int len) {
            segs[x - y][y]++; segs[x - y][y + len + 1]--;
        };
        for (int i = 0; i < n; i++) {
            int x, y, l, c; cin >> x >> y >> l >> c;
            add_segment(x, y, l / 2);
            add_segment(x + (c == 0), y + (c == 1), l - 1 - l / 2);
        }
        int64_t ans = 0;
        for (const auto& [_, ma] : segs) {
            int prev_point = 0, count = 0;
            for (const auto& [y, add] : ma) {
                ans += 1LL * count * (count - 1) / 2 * (y - prev_point);
                count += add;
                prev_point = y;
            }
        }
        cout << ans << '\n';
    }
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.