题目描述:
主要思路:
看题目,可以看出来是个一维差分,但是区间覆盖范围过大,同时使用离散化。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
map<int,int> a;
typedef long long ll;
int main()
{
int n;
cin>>n;
int now=0;
while(n--)
{
int m;
char op;
cin>>m>>op;
if(op=='R') //左闭右开
{
a[now]+=1;
a[now+m]-=1;
now+=m;
}
else
{
a[now]-=1;
a[now-m]+=1;
now-=m;
}
}
ll ans=0;
int num=0;
int last;
for(auto x:a)
{
if(num>1) ans+=x.first-last;
num+=x.second;
last=x.first;
}
cout<<ans<<endl;
return 0;
}