当先锋百科网

首页 1 2 3 4 5 6 7

题目链接

题意:
给你n个花瓶,一开始都没放花,现在有两个操作。
1.要求在从位置p开始往后放val个花,如果该花瓶有花,就不管这个花瓶往下看,若放完n个花瓶还有剩余,就丢掉,然后输出第一个放花的位置和最后一个放花的位置。
2.把a~b区间的花全都丢掉,然后输出丢掉花的数量。

思路:
先看操作1,就是通过前缀和思想找到第一个放花的位置和最后一个放花的位置, 这样好操作,不然找从p开始有第一个花的位置咋找吗。
操作二就用区间长度减去区间空瓶就是答案,然后更新一下就行。

ac代码:

/**
* Think twice, code once.
* 1.integer overflow(maybe even long long overflow : (a+b >= c) -> (a >= c-b)
* 2.runtime error
* 3.boundary condition
* ---------------------------------------------------------------------------------------
* Author : zzy
* Date : 2020-03-21-17.04.53 Saturday
*/
#include <bits/stdc++.h>

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 50001+1000;
int tc, n, m, no[maxn<<2], laz[maxn<<2];

void pushUp(int node) {
    no[node] = no[node<<1]+no[node<<1|1];
}
void pushDown(int node, int l, int r) {
    int mid = (l+r)>>1;
    int lf = mid-l+1;
    int rt = r-mid;
    if (laz[node] == 1) {
        no[node<<1] = no[node<<1|1] = 0;
        laz[node<<1] = laz[node<<1|1] = laz[node];
        laz[node] = -1;
    }
    if (laz[node] == 0) {
        no[node<<1] = lf;
        no[node<<1|1] = rt;
        laz[node<<1] = laz[node<<1|1] = laz[node];
        laz[node] = -1;
    }
}
void build(int node, int l, int r) {
    int len = r-l+1;
    no[node] = len;
    laz[node] = -1;
    if (l < r) {
        int mid = (l+r)>>1;
        build(node<<1, l, mid);
        build(node<<1|1, mid+1, r);
    }
}
int querySum(int node, int l, int r, int ql, int qr) {
    if (l >= ql && r <= qr) return no[node];
    pushDown(node, l, r);
    int sum = 0;
    int mid = (l+r)>>1;
    if (ql <= mid) sum += querySum(node<<1, l, mid, ql, qr);
    if (qr > mid) sum += querySum(node<<1|1, mid+1, r, ql, qr);
    return sum;
}
int queryPos(int node, int l, int r, int val) {
    if (l == r) return l;
    pushDown(node, l, r);
    int mid = (l+r)>>1;
    if (no[node<<1] >= val) queryPos(node<<1, l, mid, val);
    else queryPos(node<<1|1, mid+1, r, val-no[node<<1]);
}
void updateOne(int node, int l, int r, int ul, int ur) {
    if (l >= ul && r <= ur) {
        no[node] = 0;
        laz[node] = 1;
        return;
    }
    pushDown(node, l, r);
    int mid = (l+r)>>1;
    if (ul <= mid) updateOne(node<<1, l, mid, ul, ur);
    if (ur > mid) updateOne(node<<1|1, mid+1, r, ul, ur);
    pushUp(node);
}
void updateZero(int node, int l, int r, int ul, int ur) {
    if (l >= ul && r <= ur) {
        no[node] = r-l+1;
        laz[node] = 0;
        return;
    }
    pushDown(node, l, r);
    int mid = (l+r)>>1;
    if (ul <= mid) updateZero(node<<1, l, mid, ul, ur);
    if (ur > mid) updateZero(node<<1|1, mid+1, r, ul, ur);
    pushUp(node);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif

    cin >> tc;
    while (tc--) {
        cin >> n >> m;
        build(1, 1, n);
        forn(i, m) {
            int op, x, y, pos, val;
            cin >> op;
            if (op == 1) {
                cin >> pos >> val;
                ++pos;
                int res = querySum(1, 1, n, pos, n);
                if (!res) cout << "Can not put any one." << endl;
                else {
                    int pref = pos==1?0:querySum(1, 1, n, 1, pos-1);
                    int sum;
                    if (res < val) sum = res;
                    else sum = val;
                    int fp = queryPos(1, 1, n, pref+1);
                    int sp = queryPos(1, 1, n, pref+sum);
                    cout << fp-1 << ' ' << sp-1 << '\n';
                    updateOne(1, 1, n, fp, sp);
                }
            } else {
                cin >> x >> y;
                ++x;
                ++y;
                cout << (y-x+1-querySum(1, 1, n, x, y)) << endl;
                updateZero(1, 1, n, x, y);
            }
        }
        cout << '\n';
    }

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}