제출 #1349601

#제출 시각아이디문제언어결과실행 시간메모리
1349601mishasimVera and Modern Art (CCO17_art)C++20
5 / 25
2858 ms9896 KiB
#include <bits/stdc++.h>
using namespace std;

// Мапи для всіх можливих пар степенів двійки
unordered_map<long long, long long> m[61][61];

// Функція для створення ключа з двох чисел
inline long long make_key(long long x, long long y) {
    return (x << 32) | y;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long n, q;
    cin >> n >> q;

    // Зчитування фарбувань
    for(long long i = 0; i < n; i++) {
        long long x, y, v;
        cin >> x >> y >> v;

        // Обчислюємо log2(x) і log2(y)
        int lx = 63 - __builtin_clzll(x);
        int ly = 63 - __builtin_clzll(y);

        // Залишки координат по відповідним степеням двійки
        long long px = 1LL << lx;
        long long py = 1LL << ly;
        long long key = make_key(x % px, y % py);

        m[lx][ly][key] += v;
    }

    // Обробка запитів
    while(q--) {
        long long r, c;
        cin >> r >> c;
        long long res = 0;

        // Перебір усіх степенів двійки <= координат запиту
        for(int xx = 0; xx <= 60; xx++) {
            long long px = 1LL << xx;
            if(px > r) break;
            long long rx = r % px;

            for(int yy = 0; yy <= 60; yy++) {
                long long py = 1LL << yy;
                if(py > c) break;
                long long ry = c % py;

                long long key = make_key(rx, ry);
                if(m[xx][yy].count(key))
                    res += m[xx][yy][key];
            }
        }

        cout << res << "\n";
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...