0%

1915F. Greetings

思路讲解

其实如果形式话的来说,就是要求你这个区间被几个区间覆盖了,当然一对我们只统计一次。

于是我们将区间按照左端点排序,然后从前往后添加右端点数据进入树状数组BIT,然后统计一下之前的(l比你小的,但r比你大的数量,不就是覆盖你的数量吗)数量。

AC代码

https://codeforces.com/contest/1915/submission/309450334

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
// Problem: F. Greetings
// Contest: Codeforces - Codeforces Round 918 (Div. 4)
// URL: https://codeforces.com/problemset/problem/1915/F
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long 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>(2e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,T;
pll lr[MAXN];
ll tr[MAXN*3];
ll sizeN=0;

inline ll lowbit(ll x){
return x&(-x);
}
// inline bool cmp(const pll &a,const pll &b){
// if(a.se!=b.se) return a.se<b.se;
// return a.fi<b.fi;
// }
inline ll add(ll p){
while(p<=sizeN){
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 void solve(){
cin>>N;
vector<ll> li;
for(int i=1;i<=N;++i){
cin>>lr[i].fi>>lr[i].se;
li.pb(lr[i].fi);
li.pb(lr[i].se);
}
sort(all(li));
li.resize(unique(all(li))-li.begin() );
sizeN=li.size();
for(int i=0;i<=sizeN+5;++i){
tr[i]=0;
}
sort(lr+1,lr+N+1);
ll ans=0;
for(int i=1;i<=N;++i){
ll l=lr[i].fi,r=lr[i].se;
ll t=lower_bound(all(li),r)-li.begin()+1;
ans+=query(t,sizeN);
add(t);
}
// #ifdef LOCAL
// for(int i=1;i<=N;++i){
//
// }
// #endif
cout<<ans<<"\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/1915/309450334
*/

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