0%

ABC-389-F - Rated Range(打印)

思路讲解

树状数组+二分

你可以理解为用树状数组的区间加减加速模拟法(模拟法最大的瓶颈不就是区间加减吗?)

image

然后分数是只增不减的,初始分数比别人低,经过N次比赛后也不会比别人高

所以我们就能够用二分法找到区间的L,和R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
inline ll findl(ll x){
ll l=1,r=MAXrange;
while (l<r) {
ll mid=l+r>>1;
if(search(mid)>=x){
r=mid; // 太大了,小一点
}else{
l=mid+1;
}
}
return l;
}
inline ll findr(ll x){
ll l=1,r=MAXrange;
while (l<r) {
ll mid=l+r+1>>1;
if(search(mid)<=x){
l=mid;
}else{
r=mid-1;
}
}
return l;
}

AC代码

AC

https://atcoder.jp/contests/abc389/submissions/61877078

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define ROF(i, a, b) for (int i = a; i >= b; --i)


using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
const ll MAXN=static_cast<ll>(2e5)+10,MAXrange=7e5+10;

ll N,Q;
vector<pll> lr(MAXN);
ll tr[MAXrange];
inline ll lowbit(ll x){
return x&(-x);
}
inline void add(ll x,ll l, ll r){
while (l<=MAXrange) {
tr[l]+=x;
l+=lowbit(l);
}
r+=1;
while(r<=MAXrange){
tr[r]-=x;
r+=lowbit(r);
}
}
inline ll search(ll x){
ll res=0;
while (x>0) {
res+=tr[x];
x-=lowbit(x);
}
return res;
}
inline ll findl(ll x){
ll l=1,r=MAXrange;
while (l<r) {
ll mid=l+r>>1;
if(search(mid)>=x){
r=mid; // 太大了,小一点
}else{
l=mid+1;
}
}
return l;
}
inline ll findr(ll x){
ll l=1,r=MAXrange;
while (l<r) {
ll mid=l+r+1>>1;
if(search(mid)<=x){
l=mid;
}else{
r=mid-1;
}
}
return l;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>N;
FOR(i, 1, N){
cin>>lr[i].first>>lr[i].second;
}
// 初始化dp
FOR(i, 1, MAXrange){
// 在i~i区间上区间加i(实际上就是我懒得建树)
add(i, i, i);
}
FOR(i, 1, N){
ll l=findl(lr[i].first);
ll r=findr(lr[i].second);
// 如果找到的r端点的值(现在的分数)连左端点都不满足,说明查找失败
if(search(r)<lr[i].first){
continue;
// 如果找到的l端点的值(现在的分数)连右端点都不满足,也说明查找失败
}else if(search(l)>lr[i].second){
continue;
}
add(1,l,r);
}
cin>>Q;
FOR(_, 1, Q){
ll x;
cin>>x;
cout<<search(x)<<'\n';
}
return 0;
}
// AC https://atcoder.jp/contests/abc389/submissions/61877078

心路历程(WA,TLE,MLE……)

提交了好多次RE,发现可能是C++23不支持宏定义了