Bryce1010模板
http://codeforces.com/problemset/problem/1000/C
题意:问你从[l,r]区间的被多少条线覆盖,列出所有答案。
思路:类似括号匹配的做法
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=2e5+10;
struct Node
{
ll num;
ll dir;//0左1右
//ll cnt;
}node[MAXN*2];
ll cnt[MAXN*2];
bool cmp(Node a,Node b)
{
if(a.num!=b.num)return a.num<b.num;
else return a.dir<b.dir;
}
int main()
{
ll n;
cin>>n;
for(ll i=0;i<2*n;i++)
{
if(i&1)
{
cin>>node[i].num;
node[i].dir=1;
}
else
{
cin>>node[i].num;
node[i].dir=0;
}
}
sort(node,node+2*n,cmp);
// for(ll i=0;i<2*n;i++)
// cout<<node[i].num<<" "<<node[i].dir<<endl;;
ll tot=0;
for(ll i=0;i<2*n;i++)
{
if(node[i].dir==0)//左
{
if(node[i-1].dir==0)
cnt[tot]+=node[i].num-node[i-1].num;
else if(node[i-1].dir==1)
cnt[tot]+=node[i].num-node[i-1].num-1;
tot++;
}
else//右
{
if(node[i-1].dir==1)
cnt[tot]+=node[i].num-node[i-1].num;
else
cnt[tot]+=node[i].num-node[i-1].num+1;
tot--;
}
}
cout<<cnt[1];
for(ll i=2;i<=n;i++)
cout<<" "<<cnt[i];
cout<<endl;
return 0;
}