最新文章
这里会显示最新的几篇文章摘要。
记录生活,分享知识,与你一起成长。
这里会显示最新的几篇文章摘要。
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:
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;
}