0%

思路讲解

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

于是我们将区间按照左端点排序,然后从前往后添加右端点数据进入树状数组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……)

思路讲解

设c为斜边,a,b为直角边

{a2+b2=c2a+b=ca2+b2=(a+b)22ab=0\begin{cases} a^2 + b^2 = c^2 \\ a + b = c \end{cases} \\ a^2+b^2=(a+b)^2 \\ 2ab=0

所以说a,b中必然有一为0。换言之,满足题设条件的充要条件是两个点在同一水平线或同一垂直线上。

AC代码

直接AC,没什么好说的

https://codeforces.com/problemset/submission/2072/309435277

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
// Problem: E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?
// Contest: Codeforces - Codeforces Round 1006 (Div. 3)
// URL: https://codeforces.com/problemset/problem/2072/E
// Memory Limit: 256 MB
// Time Limit: 3000 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>(5e3)+10,INF=static_cast<ll>(5e18)+3;

ll K,T;

inline void solve(){
cin>>K;
vector<pll> ans;
ll upj=1;
for(int i=1;i<=500;++i){
if(K==0) break;
ll idx=0;
for(int j=upj;j<=500+upj;++j){
if(K-idx<0) {
upj=j;
break;
}
K-=idx;
ans.pb({i,j});
++idx;
}
}
// #ifdef LOCAL
// cerr<< << ans.size() << '\n';
// #endif
cout<<ans.size()<<"\n";
for(int i=0;i<ans.size();++i){
cout<<ans[i].fi<<" "<<ans[i].se<<"\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/2072/309435277
*/

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

思路讲解

主要我们借用这道题目练习一下C++的分词以及逐行输入。

搜过了,其实也没什么方便的分词方法,substr也是依靠序号进行切片,没什么卵用。

哈哈,其实在赛场上我也是做出来了,但用的是python,他的整行输入是真的好用,就是动态类型啥的我老写C++就觉得不习惯。

AC代码

采用测试数据在本地进行测试,鸣谢CPeditor的支持。

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
#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>(1e6)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,T,A[MAXN];

inline void solve(){
cin>>N;
cin.get();
for(int i=1;i<=N;++i){
string sline;
getline(cin,sline);
sline+=" ";
string s;
set<string> ans;
bool isConti=false;
for(int i=0;i<sline.size();++i){
if(sline[i]==' ' && isConti){
isConti=false;
ans.insert(s);
s.clear();
continue;
}
s+=sline[i];
isConti=true;
}
cout<<ans.size()<<"\n";
}
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
/*

*/

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

思路讲解

这种位运算的题我显然就没有那么会做。

但是不会做手也不能闲着(如果一定要做这道题目的话),可以写个对拍程序呀,或者什么的,打表看看规律。

结果最后还是要按位分析,不过也不用怕,加法的进位其实无所谓的,因为加法与位运算在这道题里面是隔离的。

AC代码

https://codeforces.com/problemset/submission/2057/309432960

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
// Problem: C. Trip to the Olympiad
// Contest: Codeforces - Hello 2025
// URL: https://codeforces.com/problemset/problem/2057/C
// Memory Limit: 256 MB
// Time Limit: 2000 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>(1e6)+10,INF=static_cast<ll>(5e18)+3;

ll L,R,T,A[MAXN],pow2[45],sumPow2[45];

inline void solve(){
cin>>L>>R;
// ll target=0;
// for(int i=L;i<=R;++i){
// for(int j=L;j<=R;++j){
// for(int k=L;k<=R;++k){
// ll ltar=(i^j)+(i^k)+(k^j);
// if(ltar>=target){
// target=ltar;
// ans[0]=i;
// ans[1]=j;
// ans[2]=k;
// }
// }
// }
// }
ll opR=R;
vector<ll> bitR;
while(opR){
bitR.push_back(opR%2);
opR/=2;
}
ll a=R,b=0,c=0;
// 每一位上,110或者100或者101布局是最合理的
for(int i=bitR.size()-1;i>=0;--i){
if(bitR[i]==1){
if(b+pow2[i]<=R && c+sumPow2[i-1]>=L){ // 可不可以110布局
b+=pow2[i];
}else if(b+sumPow2[i-1]>=L && c+pow2[i]<=R){ // 可不可以101布局
c+=pow2[i];
}else if(b+sumPow2[i-1]>=L && c+sumPow2[i-1]>=L){ // 可不可以100布局
continue;
}else if(b+sumPow2[i-1]<L && c+sumPow2[i-1]<L){ // 最后再不行就111布局吧
b+=pow2[i];
c+=pow2[i];
}
}else{
if(b+pow2[i]<=R && c+pow2[i]<=R){ // 011
b+=pow2[i];
c+=pow2[i];
}else if(b+pow2[i]<=R && c+sumPow2[i-1]>=L){ // 010
b+=pow2[i];
}else if(b+sumPow2[i-1]>=L && c+pow2[i]<=R){ // 001
c+=pow2[i];
}else if(c+pow2[i]>R && b+pow2[i]>R){ // 000
continue;
}
}
}
if(a==b) b-=1;
if(b==c) c-=1;
cout<<a<<" "<<b<<" "<<c<<"\n"; // 题目竟然还要求a,b,c不同,离谱
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
pow2[0]=1;
for(int i=1;i<=40;++i){
pow2[i]=2*pow2[i-1];
}
sumPow2[0]=pow2[0];
for(int i=1;i<=40;++i){
sumPow2[i]=sumPow2[i-1]+pow2[i];
}
while(T--){
solve();
}
return 0;
}
/*

*/

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

题目还要求a,b,c需要不同,我直接相同就-1,虽然我感觉似乎没那么妥当,但AC了。

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
---
title: 1850G-The-Morning-Star
categories:
- codeforces题解
tags:
- null
date: 2025-03-05 11:48:05
---
我们都知道,想要找到同x或者同y的点的数量易如反掌。

那么,同对角线的那?

其实也很简单,找x+y || x-y的值是相同的点的数量就行
```cpp
// Problem: G. The Morning Star
// Contest: Codeforces - Codeforces Round 886 (Div. 4)
// URL: https://codeforces.com/problemset/problem/1850/G
// Memory Limit: 256 MB
// Time Limit: 2000 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>(1e18)+3;

ll N,M,T;
pll xy[MAXN];
bool cmpy(const pll &a,const pll &b){
if(a.se!=b.se) return a.se<b.se;
return a.fi<b.fi;
}
bool cmpxPy(const pll &a,const pll &b){
if(a.fi+a.se!=b.fi+b.se) return a.fi+a.se<b.fi+b.se;
return a.fi<b.fi;
}
bool cmpxMy(const pll &a,const pll &b){
if(a.fi-a.se!=b.fi-b.se) return a.fi-a.se<b.fi-b.se;
return a.fi<b.fi;
}
inline void solve(){
cin>>N;
for(int i=1;i<=N;++i){
cin>>xy[i].fi>>xy[i].se;
}
sort(xy+1,xy+1+N);
ll ans=0;
for(int i=1;i<=N;++i){
pll uppConst={xy[i].fi,INF},lowConst={xy[i].fi,-INF};
ans+=upper_bound(xy+1,xy+1+N,uppConst)-lower_bound(xy+1,xy+1+N,lowConst)-1;
}
// #ifdef LOCAL
// cerr << ans << '\n';
// #endif
sort(xy+1,xy+1+N,cmpy);
for(int i=1;i<=N;++i){
pll uppConst={INF,xy[i].se},lowConst={-INF,xy[i].se};
ans+=upper_bound(xy+1,xy+1+N,uppConst,cmpy)-lower_bound(xy+1,xy+1+N,lowConst,cmpy)-1;
}
// #ifdef LOCAL
// cerr << ans << '\n';
// #endif
// 同y和同x的匹配完了,同对角线的怎么统计?
// 哈哈,其实和上面的一样的思路,同一对角线,x-y||x+y的值是一样的
sort(xy+1,xy+1+N,cmpxPy);
for(int i=1;i<=N;++i){
ll aPb=xy[i].fi+xy[i].se;
pll uppConst={INF,aPb-INF},lowConst={-INF,aPb+INF};
ans+=upper_bound(xy+1,xy+1+N,uppConst,cmpxPy)-lower_bound(xy+1,xy+1+N,lowConst,cmpxPy)-1;
}
// #ifdef LOCAL
// cerr << ans << '\n';
// #endif
sort(xy+1,xy+1+N,cmpxMy);
for(int i=1;i<=N;++i){
ll aMb=xy[i].fi-xy[i].se;
pll uppConst={INF,-aMb+INF},lowConst={-INF,-aMb-INF};
ans+=upper_bound(xy+1,xy+1+N,uppConst,cmpxMy)-lower_bound(xy+1,xy+1+N,lowConst,cmpxMy)-1;
}
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/1850/309011870
*/