0%

CF1843E Tracking Segments

思路讲解

二分答案+树状数组

其实这个二分答案还是挺难想的,主要还是因为这个东西具有单调性,所以就可以搞二分答案。二分答案还是能节省很多时间。

AC代码

https://codeforces.com/problemset/submission/1843/310708365

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
// Problem: CF1843E Tracking Segments
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1843E
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,T,A[MAXN];
arr lr[MAXN];
ll modify[MAXN];
ll fiOcc[MAXN]; // 该值第一次出现的位置
ll tr[MAXN];
bool isHave[MAXN];
inline ll lowbit(ll x){
return x&(-x);
}
inline void add(ll p){
while(p<=N){
tr[p]+=1;
p+=lowbit(p);
}
}
inline ll query(ll l,ll r){
ll lres=0,rres=0;
l-=1;
while(l>0){
lres+=tr[l];
l-=lowbit(l);
}
while(r>0){
rres+=tr[r];
r-=lowbit(r);
}
return rres-lres;
}
inline bool check(ll x){
if(x==N+1) return true;
FOR(i,1,N){
tr[i]=0;
isHave[i]=false;
}
FOR(i,1,x){ // 保证不重复加
if(isHave[modify[i]]) continue;
isHave[modify[i]]=true;
add(modify[i]);
}
FOR(i,1,M){
ll len=lr[i][1]-lr[i][0]+1;
ll res=query(lr[i][0],lr[i][1]);
if(res>len-res){
return true;
}
}
return false;
}
inline void solve(){
cin>>N>>M;
FOR(i,1,M){
cin>>lr[i][0]>>lr[i][1];
}
ll Q;
cin>>Q;
FOR(i,1,Q){
ll x;
cin>>modify[i];
}
ll l=1,r=Q+1;
while(l<r){
ll mid=l+r>>1;
if(check(mid)){
r=mid;
}else{
l=mid+1;
}
}
cout<<(l!=Q+1?l:-1)<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/problemset/submission/1843/310708365
*/

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