``````#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 100010
#define M 1000000007
using namespace std;
ll ans,sum;
int vis[5];
int n,m,a[5][2],x,y;
void dfs(int k,int now,int dis)
{
if(dis+abs(now-y)<ans)
ans=dis+abs(now-y);
if(k>3)
return ;
for(int i=0;i<3;i++)
{
if(!vis[i])
{
vis[i]=1;
dfs(k+1,a[i][0],dis+abs(now-a[i][1])+1);
dfs(k+1,a[i][1],dis+abs(now-a[i][0])+1);
vis[i]=0;
}
}
}
int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<3;i++)
scanf("%d%d",&a[i][0],&a[i][1]);
memset(vis,0,sizeof(vis));
sum=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
ans=abs(x-y);
dfs(1,x,0);
sum=(sum+i*ans)%M;
}
printf("%I64d\n",sum);
}
return 0;
}

``````

//WA了考虑的太少。。

``````#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 100010
#define M 1000000007
using namespace std;
struct zz
{
int x;
int y;
int l;
}p[N];
int cmp(zz a ,zz b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int main()
{
int t;
int n,m;
int i,j,k;
int x,y,s,e;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<3;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[i].x>p[i].y)
{
int tt=p[i].x;p[i].x=p[i].y;p[i].y=tt;
}
p[i].l=p[i].y-p[i].x;
}
sort(p,p+3,cmp);
ll sum=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&s,&e);
int ss=-1,ee=-1;
int l=e-s;
if(s>e)
{
int tt=s;s=e;e=tt;
}
for(j=0;j<3;j++)
{
if(s<=p[j].x)
{
ss=j;
break;
}
}
for(j=2;j>=0;j--)
{
if(e>=p[j].y)
{
ee=j;
break;
}
}
int kl=0;
if(ss==ee)
sum=(sum+(l-p[ss].l+1)*i)%M;
else if(ss<ee)
{
for(j=ss;j<=ee;j++)
kl+=p[j].l;
sum=(sum+(l-kl+1)*i)%M;
}
else
sum=(sum+l*i)%M;
}
printf("%lld\n",sum);
}
return 0;
}``````

//开始时用的是SPFA写，超时了，

``````#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 100010
#define M 1000000007
using namespace std;
struct zz
{
int from;
int to;
int val;
int next;
}edge[N];
int n,m;
int dis[N];
int vis[N];
{
edge[edgenum]=E;
}
void SPFA(int x)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
q.push(x);
dis[x]=0;
vis[x]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
int t;
int i,j,k;
int x,y,s,e;
scanf("%d",&t);
while(t--)
{
edgenum=0;
scanf("%d%d",&n,&m);
for(i=1;i<n-1;i++)
{
}
for(i=0;i<3;i++)
{
scanf("%d%d",&x,&y);
}
SPFA(1);
int sum=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&s,&e);
// printf("%d\n",dis[e]);
sum=(sum+i*(abs)(dis[e]-dis[s]))%M;
}
printf("%d\n",sum);
}
return 0;
}``````