80pts 回滚莫队在线求条
查看原帖
80pts 回滚莫队在线求条
722094
Little_Cancel_Sunny楼主2025/6/28 08:23

楼主调了一万年,已完全红温。 好像是链表维护前驱后继时下标对应出现问题

#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 1000000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long

const int N=1e6+15;
const int inf=1e18;

int n,m,len;
int a[N],ans[N];
int loc[N],L[N];
int stk[N],top;
int tot,res;
int st,ed;
int now[N];
struct lst
{
    int pre,suf,val;
}p[N];
struct question
{
    int l,r;
    int id;
    int bel;
}q[N];

bool cmp(question x,question y)
{
    if(x.bel==y.bel)
        return x.r>y.r;
    return x.bel<y.bel;
}

bool cmpa(int x,int y)
{
    return a[x]<a[y];
}


int absll(int x,int y)
{
    // cout<<"include13_fuck"<<x<<" "<<y<<endl;
    if(x<y)
        return y-x;
    return x-y;
}

int del(int l)
{
    l=now[l];
    int pre=p[l].pre;
    int suf=p[l].suf;
    // int pre=now[p[l].pre];
    // int suf=now[p[l].suf];
    int res=0;
    if(pre)
        res-=absll(loc[pre],loc[l]);
    if(suf)
        res-=absll(loc[suf],loc[l]);
    // cout<<"del "<<l<<" "<<pre<<" "<<suf<<" "<<res<<endl;
    if(pre&&suf)
        res+=absll(loc[pre],loc[suf]);
    p[pre].suf=suf;
    p[suf].pre=pre;
    stk[++top]=l;
    return res;
}

void restore()
{
    int l=stk[top--];
    int pre=p[l].pre;
    int suf=p[l].suf;
    // cout<<"add "<<l<<" "<<pre<<" "<<suf<<endl;
    p[pre].suf=l;
    p[suf].pre=l;
}

signed main()
{
	cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i],loc[i]=i;
    sort(loc+1,loc+n+1,cmpa);
    sort(a+1,a+n+1,cmpa);
    for(int i=1;i<=n;i++)
        now[loc[i]]=i;
    // for(int i=1;i<=n;i++)
    //     cout<<i<<" "<<loc[i]<<" "<<now[i]<<endl;
    for(int i=1;i<=n;i++)
    {
        p[i].pre=i-1;
        p[i].suf=i+1;
        L[i]=inf;
    }
    p[n].suf=0;
    // for(int i=1;i<=n;i++)
    //     cout<<i<<" "<<p[i].pre<<" "<<p[i].suf<<endl;
    for(int i=1;i<n;i++)
        tot=tot+absll(loc[i],loc[i+1]);
    // cout<<tot<<endl;
    len=sqrt(n);
    for(int i=1;i<=m;i++)
    {
        cin>>q[i].l>>q[i].r;
        q[i].id=i;
        q[i].bel=q[i].l/len+1;
        L[q[i].bel]=min(L[q[i].bel],q[i].l);
    }
    sort(q+1,q+m+1,cmp);
    // for(int i=1;i<=m;i++)
    //     cout<<q[i].bel<<" "<<q[i].l<<" "<<q[i].r<<endl;
    int nowl=0,lres=1,rres=n;
    for(int i=1;i<=m;i++)
    {
        // cout<<"question "<<i<<endl;
        if(nowl!=q[i].bel)
        {
            while(rres<n)
                restore(),rres++;
            while(lres<L[q[i].bel])
                tot+=del(lres++);
            rres=n;
            top=0;
            nowl=q[i].bel;
            res=tot;
        }
        while(q[i].r<rres)
            res+=del(rres--);
        int lxl=res;
        int l=lres;
        while(q[i].l>l)
            lxl+=del(l++);
        ans[q[i].id]=lxl;
        while(l>lres)
            restore(),l--;
    }
    for(int i=1;i<=m;i++)
        cout<<ans[i]<<endl;
    return 0;
}

2025/6/28 08:23
加载中...