样例过,全wa
查看原帖
样例过,全wa
455515
wumingdeyu楼主2023/2/19 16:59
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstdlib>
#include<iostream>
using namespace std;

const int N=5e5+10,inf=0x7f7f7f7f;
int n,t,original[N];
int fa[N];
long long sum[N];
int head[N],tot;

struct edges
{
    int to,dis,nxt;
}edge[N];

inline void add(int x,int y,int z)
{
    edge[++tot]={y,z,head[x]},head[x]=tot;
}

struct node
{
    int max,pos;
    bool operator < (const node & a) const
    {
        return max<a.max;
    }
};
struct segment
{
    int left,right;
    node ATRI;
}tree[N<<2];

inline void update(int pos)
{
    tree[pos].ATRI=max(tree[pos<<1].ATRI,tree[pos<<1|1].ATRI);
}

inline void build(int pos,int left,int right)
{
    tree[pos]={left,right,{0,0}};
    if(left==right)
    {
        tree[pos].ATRI={original[left],left};
        return;
    }

    int mid=(left+right)>>1;

    build(pos<<1,left,mid);
    build(pos<<1|1,mid+1,right);

    update(pos);
}

inline node ask(int pos,int left,int right)
{
    if(left<=tree[pos].left&&tree[pos].right<=right)
        return tree[pos].ATRI;
    
    node ans={0,0};
    int mid=(tree[pos].left+tree[pos].right)>>1;

    if(tree[pos].left<=left&&left<=mid) ans=max(ans,ask(pos<<1,left,mid));
    if(mid+1<=right&&right<=tree[pos].right) ans=max(ans,ask(pos<<1|1,mid+1,right));

    return ans;
}

inline void find()
{
    int top=0,stake[N];
    memset(stake,0,sizeof(stake));
 //   stake[top]=inf;
    for(int i=1;i<=n;i++)
    {
        while(top&&original[stake[top]]<original[i])
        {
            add(i,stake[top],i-stake[top]);
            fa[stake[top--]]=i;
        }
        stake[++top]=i;
    }
    while(top)
    {
        add(n+1,stake[top],n+1-stake[top]);
        fa[stake[top--]]=n+1;
    }
}

inline void dfs(int u)
{
    for(int i=head[u];i;i=edge[i].nxt)
    {
        int to=edge[i].to;
        sum[to]=sum[u]+edge[i].dis*original[to];
        dfs(to);
    }
}

int main()
{
 //   freopen("test.out","w",stdout);
    scanf("%d%d",&n,&t);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&original[i]);
    }

    int u=0,v=0,q=0,left=0,right=0;
    long long lastans=0;

    build(1,1,n);
    find();
    dfs(n+1);
    while(t--)
    {
        scanf("%d%d",&u,&v);

        left=1+(u^lastans)%n;
        q=1+(v^(lastans+1))%(n-left+1);
        right=left+q-1;

        node tmp=ask(1,left,right);

        lastans=(right-tmp.pos+1)*tmp.max+sum[left]-sum[tmp.pos];

       cout<<lastans<<endl;
   //  printf("%d %d\n",left,right);
    }
 //   fclose(stdout);
    return 0;
}



/*
13 6
1 1 4 5 1 4 1 9 1 9 8 1 0
3 4 
1 5 
2 7 
8 9
5 1 
6 6
*/

救救叭

2023/2/19 16:59
加载中...