原题:https://loj.ac/problem/2639
题解:就是求动态逆序对的和。考虑暴力的方法,对于区间[L,R] 把[L+1.R-1]的元素和L,R比,其实就是要求[L+1,R-1]中所有大于L的和,所有小于L的和,很容易想到数列分块,把每一个块排序,二分查找求和。要注意不要用STL,注意常数优化。
#include<bits/stdc++.h>
#define ll long long
#define M 1000000007
using namespace std;
const int N=1e5+10;
struct node{
ll x,y;
}a[N];
inline char gc() {
static char now[1<<16], *S, *T;
if(S == T) {T = (S = now) + fread(now, 1, 1<<16, stdin); if(S == T) return EOF;}
return *S++;
}
inline int rd(){
int x=0;int f=1;char s=getchar();
while(!isdigit(s)) f=(s=='-'?-1:f),s=getchar();
while(isdigit(s)) x=(x<<1)+(x<<3)+s-'0',s=getchar();
return x*f;
}
inline void print(int x) {
if(x == 0) {puts("0"); return ;}
if(x < 0) putchar('-'), x = 0 - x;
int d[30], now = -1;
while(x) {d[++now] = x % 10; x/= 10;}
for(int i = now; i >= 0; --i) putchar('0' + d[i]); puts("");
}
int n,m,q;
ll ans=0;
struct data{//这块常数有点大
ll x,y;
data(){x=0;y=0;}
data(ll a,ll b){x=a;y=b;}
friend data operator + (const data &a,const data &b){
return data((a.x+b.x)%M,(a.y+b.y)%M);
}
friend data operator - (const data &a,const data &b){
return data((a.x-b.x+M)%M,(a.y-b.y+M)%M);
}
}bit[N];
inline int lowbit(ll x){return x&(-x);}
inline void add(ll x,data c){
for(x;x<=n;x+=lowbit(x)) bit[x]=bit[x]+c;
}
inline data sum(int x){
data ans=(data){0,0};
for(x;x;x-=lowbit(x))ans=ans+bit[x];
return ans;
}
node v[250][250];//不要用vector
ll val[250][250];
data s[250][250];
int pos[N];
bool cmp(node a,node b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline void reset(int xx){
int j;int L=min(n,xx*m)-(xx-1)*m; v[xx][0].x=L;v[xx][0].y=xx;
for(int i=(xx-1)*m+1,j=1;i<=min(n,xx*m);i++,j++)v[xx][j]=a[i];
sort(v[xx]+1,v[xx]+L+1,cmp);
s[xx][0]=(data){0,0};
val[xx][0]=0;
for(int i=1,j=1;i<=L;i++){
s[xx][i]=s[xx][i-1]+data(v[xx][i].y,1);
val[xx][i]=v[xx][i].x;
}
}
void query1(int l,int r,ll c,ll vv){//左端点
if(l>r) return ;
data a1,a2;
for(int i=l;i<=min(r,pos[l]*m);i++) {
if(a[i].x>c) ans+=(vv+a[i].y)%M,ans%=M;
else {
ans-=(vv+a[i].y)%M;if(ans<0) ans+=M;
}
}
if(pos[l]!=pos[r]){
for(int i=(pos[r]-1)*m+1;i<=r;i++)
if(a[i].x>c) ans+=vv+a[i].y,ans%=M;
else {
ans-=vv+a[i].y;if(ans<0) ans+=M;
}
}
for(int i=pos[l]+1;i<=pos[r]-1;i++){
int L;L=v[i][0].x;
int t=lower_bound(val[i],val[i]+L+1,c)-(val[i])-1;
a1=s[i][t];
a2=s[i][L]-a1;
ans+=a2.x;ans+=vv*(a2.y)%M; ans%=M;
ans-=a1.x;ans-=vv*a1.y%M;if(ans<0) ans+=M;
}
}
void query2(int l,int r,ll c,ll vv){//右端点
if(l>r) return ;
data a1,a2;
for(int i=l;i<=min(r,pos[l]*m);i++) {
if(a[i].x<c) ans+=vv+a[i].y,ans%=M;
else {
ans-=vv+a[i].y;if(ans<0) ans+=M;
}
}
if(pos[l]!=pos[r]){
for(int i=(pos[r]-1)*m+1;i<=r;i++)
if(a[i].x<c) ans+=vv+a[i].y,ans%=M;
else {
ans-=vv+a[i].y;if(ans<0) ans+=M;
}
}
for(int i=pos[l]+1;i<=pos[r]-1;i++){
int L=v[i][0].x;
int t=lower_bound(val[i],val[i]+L+1,c)-(val[i])-1;
a2=s[i][t];
a1=s[i][L]-a2;
ans+=a2.x;ans+=vv*(a2.y)%M; ans%=M;
ans-=a1.x;ans-=vv*a1.y%M;if(ans<0)ans+=M;
}
}
void change(int l,int r){
swap(a[l],a[r]);
reset(pos[l]);
if(pos[l]!=pos[r])reset(pos[r]);
}
int main(){
n=rd();q=rd();m=sqrt(n);
ans=0;
for(int i=1;i<=n;i++){
a[i].x=rd();a[i].y=rd();
data temp=sum(n)-sum(a[i].x);
ans=(ans+temp.x+1ll*a[i].y*temp.y%M)%M;
add(a[i].x,data(a[i].y,1));
}
for(int i=1;i<=n;i++) pos[i]=(i-1)/m+1;
for(int i=1;i<=pos[n];i++)reset(i);
for(int i=1;i<=q;i++){
int L=rd();int R=rd();
if(L>R) swap(L,R);
if(L==R){
print(ans);continue;
}
query1(L+1,R-1,a[L].x,a[L].y);
query2(L+1,R-1,a[R].x,a[R].y);
if(a[L].x<a[R].x) ans=(ans+a[L].y+a[R].y)%M;
if(a[L].x>a[R].x) ans=(ans-(a[L].y+a[R].y)%M+M)%M;
print(ans);
change(L,R);
}
return 0;
}