CodeForces -1207F Remainder Problem(分块)
发布日期:2021-05-15 23:00:27 浏览次数:12 分类:精选文章

本文共 4518 字,大约阅读时间需要 15 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

���������������������������������5e5���������������������������������������������������������������������������������������������������������������������������������������O(n��)���������������������������������������������������������������������������������������

������������

���������������������������������������������������������������������������������������������������sqrt(n)������������������������n = 5e5���sqrt(5e5) ��� 707������������������������������5e5 / 707 ��� 707���

  • ������1������������������x������������������������������������������������������������������������
  • ������2������������x = y������������������������x���������������������������������
    • ���x ��� sqrt(n)������������������������������������������������������������
    • ���x > sqrt(n)���������������������������������������������������������������

������������������������������������O(n^(3/4))������������������������������������

������������������

  • ���������������

    • ���������������sqrt(n)���������������������������n / sqrt(n)���
    • ������������������������������������������
  • ������1���������������

    • ���������������������������������������
    • ���������������������������������������������������������������������������������
  • ������2���������������

    • ������x ��� sqrt(n)������������������������������x = y������������������������������
    • ������x > sqrt(n)������������������������������������x = y���������������������������������������������������������������������������������������
  • ������������

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std; #define ll long long #define inf 0x3f3f3f3f #define ull unsigned long long #include
    #include
    #include
    #include
    void mainFunction() { ll n = 5e5 + 5; int siz = sqrt(n) + 1; // ������������������ vector
    a(n); map
    blockHelper; // ��������������������������������� // ��������������������������������������������������������������������������������� } // main������������������������������������������������������������������ int main() { // ������Block������������������������������ int sz = static_cast
    (sqrt(5e5)) + 1; vector
    a(5e5 + 5, 0); map
    > block; // ��������������������������������������������������������������� for (int i = 0; i < sz; ++i) { block[i] = vector
    (1 + 2*sz, 0); } // ��������������������� ll q = read(); for (; q > 0; --q) { int opt = read(); int x = read(); if (x < 0 || x >= 5e5 + 5) { // ��������������������������������������������������� continue; } int y = read(); if (x < sz) { // ������������1��������������� a[x] += y; for (int i = 0; i < sz; ++i) { int posInBlock = (x % sz) == 0 ? sz : x % sz; block[i][posInBlock] += y; } } else { // ������������2��������������� ll sum = 0; int realX = x; if (realX <= sz) { // ������x > sz��������������������������������� for (int i = 0; i < sz; ++i) { int pos = (y % sz == 0) ? sz : y % sz; sum += a[i*sz + pos]; } } else { // ��������������������������������������� // ������������������������������ // ������������������������������������������������������ // ������������������������i���������������������mod x == y��������� // ������Sum += a[i*sz + pos] * count // ��������������������������������������������� int blockCount = (realX - sz) / sz + 1; sum = 0; for (int i = 0; i < sz; ++i) { int pos = y % sz == 0 ? sz : y % sz; int base = i * sz; int currentMod = (base + pos) % realX; if (currentMod == y) { sum += block[i][pos]; } } } cout << sum << '\n'; } } return 0; } // ���������������������������������������������������������

    ������������

    • ������������������������������������sqrt(n)������������������������������������������������������������������������������������
    • ���������������������������������������������������������������������������������������������������������������������
    • ���������������������������������������������������������������������������������������������

    ������������������

    • ���������������������������������������������������������������������������������������������������
    • ���������������������������������������������������x���������������������������������������������������
    • ���������������������map���vector���������������������������������������������������������

    ���������������������������������������������O(n��)������������������O(n^(3/4))������������������������������������������������

    上一篇:CodeForces -1168A Increasing by Modulo(二分答案)
    下一篇:CodeForces -1005F Berland and the Shortest Paths(最短路径树)

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月11日 17时37分40秒