最新文章

这里会显示最新的几篇文章摘要。

hotel room(树状数组)

问题 I: Hotel Rooms

时间限制: 1.000 Sec 内存限制: 128 MB

题目描述

When the UCF Programming Team travels, the coaches would like to get hotel rooms that are close to each other. There is a hotel where rooms are numbered 1 through n and these rooms are in a straight line, i.e., Room 2 is next to Room 1, Room 3 is next to Room 2, and so on. So, it is easier to find large number of available rooms that are close to each other.

Given the room reservations, you are to determine the availability of rooms to accommodate the UCF Programing Team (a large group).

输入

The first input line contains two integers: n (1 ≤ n ≤ 5 ×10), indicating the number of hotel rooms and t (1 ≤ t ≤ 10), indicating the number of transactions. Each of the next t input lines contains a transaction to be processed. There will be two types of transactions:

  • Room Reservation: This input line starts with the letter R in the first column, followed by one space, followed by a valid room number. This transaction is reserving the given room (assume that the room is not already reserved).
  • Group Room Availability: This input line starts with the letter A in the first column, followed by one space, followed by a valid starting room number, followed by a space, followed by a valid ending room number. This transaction is asking how many rooms are available in the given range. Assume that the ending room number will not be less than the starting room number, i.e., the requested range is valid.

输出

There is no output required for the room-reservation transactions. For each group-roomavailability transaction, output a separate line providing the total number of available rooms in the requested range.

样例输入

20 8 
A 5 10
R 6
R 9
R 3 
A 5 10
R 13
R 18
A 1 20

样例输出

6 
4 
15

题意

有n个房间,每次输入\(l\)\(r\) 查询\([l,r]\)之间的空房间数量,输入\(R\)\(k\)代表预约房间\(k\),输出每次查询结果

分析

直接考察线段树或者树状数组,这里用数组数组

代码

#include<bits/stdc++.h>
#define de(x) cout << #x << "=" << (x) << '\n';
#define de2(x,y) cout << #x << "=" << (x) << #y << "=" << y << '\n'; 
#define f(x) for(int i = 0;i < x;++i)
using namespace std;

#define int long long

const int N = 5e5+5;
int a[N], c[N];     //a数组是最开始的时候的房间状态,没用上,c是树状数组
int n, t;
int lowbit(int x) {return x & -x;} //计算k,设二进制最低位为第 0 位,则 k 恰好为 x 二进制表示中,最低位的 1 所在的二进制位数

inline int getsum(int x){  //求[1,x]的和
    int ans = 0;
    while(x > 0) ans += c[x], x = x - lowbit(x); //往左边界跳转,直到0
    return ans;
}
void add(int x, int k){   //修改所有父节点的值
    while(x <= n){
        c[x] = c[x] + k;
        x = x + lowbit(x);//往父节点跳转,直到超过n
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    


    cin >> n >> t;
    f(t){
        char ch; 
        cin >> ch;
        if(ch == 'A'){
            int l, r;
            cin >> l >> r;
            cout << r-l+1 - getsum(r) + getsum(l-1) << '\n';
        }else if(ch == 'R'){
            int h;
            cin >> h;
            add(h, 1);
            
        }
    }

    return 0;
}

posted @ 2025-04-04 16:42  bakul  阅读(54)  评论(0)    收藏  举报