【Atcoder Beginner Contest 207】C - Many Segments
【Atcoder Beginner Contest 207】C - Many Segments
Time Limit: \(2\) sec / Memory Limit: \(1024\) MB
Score : \(300\) points
Problem Statement
You are given \(N\) intervals numbered \(1\) through \(N\), that are as follows:
- if \(t_i=1\), Interval \(i\) is \([l_i,r_i]\);
- if \(t_i=2\), Interval \(i\) is \([l_i,r_i)\);
- if \(t_i=3\), Interval \(i\) is \((l_i,r_i]\);
- if \(t_i=4\), Interval \(i\) is \((l_i,r_i)\);
How many pairs of integers (i,j) satisfying 1≤i<j≤N are there such that Interval i and Interval j intersect?
Constraints
- \(2≤N≤2000\)
- \(1≤ti≤4\)
- \(1≤l_i<r_i≤10^9\)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
\(N\)
\(t_1\) \(l_1\) \(r_1\)
\(t_2\) \(l_2\) \(r_2\)
⋮⋮
\(t_N\) \(l_N\) \(r_N\)
Output
Print the number of pairs of integers \((i,j)\) such that Interval \(i\) and Interval \(j\) intersect.
//就一条,这个+-0.5的操作属实惊到我了,我本来还想整个前缀和来着!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3005;
double l[N],r[N];
int main()
{
int n;
ll ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
int op;
cin>>op>>l[i]>>r[i];
if(op==2||op==4)r[i]-=0.5;
if(op==3||op==4)l[i]+=0.5;
for(int j=1;j<i;j++)if(l[i]<=r[j]&&l[j]<=r[i])ans++;
}
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号