Rope Can`t Water it.
查看原帖
Rope Can`t Water it.
379067
futz12楼主2021/10/25 20:22
#include <iostream>
#include <bits/extc++.h>
#define lowbit(x) ((-(x)) & (x))
using namespace std;
using namespace __gnu_cxx;

const int N = 1e5 + 128;

rope<int> Tree;

rope<int> *his[N];

int n, m;

inline void add(int u, int offset)
{
    for (; u <= n; u += lowbit(u))
        Tree.replace(u, Tree[u] + offset);
}

inline int query(rope<int> &T, int u)
{
    int ans = 0;
    for (; u > 0; u -= lowbit(u))
        ans += T[u];
    return ans;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i <= n; i++)
        Tree.append(0);
    his[0] = new rope<int>(Tree);
    for (int i = 1; i <= m; i++)
    {
        int opt = 0;
        cin >> opt;
        if (opt == 0)
        {
            int l, r, x;
            cin >> l >> r >> x;
            add(l, x);
            add(r + 1, -x);
        }
        else
        {
            int x;
            cin >> x;
            int now = query(Tree, x);
            int l = 0, r = i - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                //cout << query(*his[mid], x) << endl;
                if (query(*his[mid], x) < (now + 1) / 2)
                    l = mid + 1;
                else
                    r = mid;
            }
            cout << i - l << endl;
        }
        his[i] = new rope<int>(Tree);
    }
    return 0;
}
2021/10/25 20:22
加载中...