Splay+poj3468

本文探讨使用Splay树解决区间操作问题,通过旋转操作优化区间加和与查询效率,实现复杂操作的高效处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Language:
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 65829 Accepted: 20274
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.
原来用线段树写过,最近开始做Splay,那这个题练手了

对于区间操作,首先把第l个数旋转到根,然后把第r+1个数旋转到跟的右孩子,那么区间加和就是对跟的右孩子的左孩子整个区间加上val,可以用类似线段树的懒惰标记进行标记

查询的时候差不多,旋转完之后就直接查询整个区间的和就可以了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define Key_value ch[ch[root][1]][0]
typedef long long LL;
const int maxn=100010;
int N,Q,A[maxn];
int pre[maxn],ch[maxn][2],key[maxn],add[maxn],size[maxn];
LL sum[maxn];
int root,tot1;
int s[maxn],tot2;
void NewNode(int &r,int f,int val)
{
    if(tot2)r=s[tot2--];
    else r=++tot1;
    pre[r]=f;
    size[r]=1;
    key[r]=val;
    add[r]=sum[r]=0;
    ch[r][0]=ch[r][1]=0;
}
void pushup(int x)
{
    size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
    sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+key[x];
}
void build(int &x,int l,int r,int f)
{
    if(l>r)return;
    int mid=(l+r)>>1;
    NewNode(x,f,A[mid]);
    build(ch[x][0],l,mid-1,x);
    build(ch[x][1],mid+1,r,x);
    pushup(x);
}
void init()
{
    root=tot1=tot2=0;
    ch[root][0]=ch[root][1]=pre[root]=size[root]=0;
    key[root]=sum[root]=add[root]=0;
    NewNode(root,0,-1);
    NewNode(ch[root][1],root,-1);
    build(Key_value,1,N,ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}
void update_add(int x,int val)
{
    if(!x)return;
    sum[x]+=(LL)val*size[x];
    key[x]+=val;
    add[x]+=val;
}
void pushdown(int r)
{
    if(add[r])
    {
        update_add(ch[r][0],add[r]);
        update_add(ch[r][1],add[r]);
        add[r]=0;
    }
}
void Rotate(int x,int kind)
{
    int y=pre[x];
    pushdown(y);
    pushdown(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][kind]=y;
    pre[y]=x;
    pushup(y);
}
void Splay(int r,int goal)
{
    pushdown(r);
    while(pre[r]!=goal)
    {
        if(pre[pre[r]]==goal)
        {
            pushdown(pre[r]);
            pushdown(r);
            Rotate(r,ch[pre[r]][0]==r);
        }
        else
        {
            pushdown(pre[pre[r]]);
            pushdown(pre[r]);
            pushdown(r);
            int y=pre[r];
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==r)
            {
                Rotate(r,!kind);
                Rotate(r,kind);
            }
            else
            {
                Rotate(y,kind);
                Rotate(r,kind);
            }
        }
    }
    pushup(r);
    if(goal==0)root=r;
}
int get_kth(int r,int k)
{
    pushdown(r);
    int t=size[ch[r][0]]+1;
    if(t==k)return r;
    if(t>k)return get_kth(ch[r][0],k);
    else return get_kth(ch[r][1],k-t);
}
LL Query_sum(int l,int r)
{
    Splay(get_kth(root,l),0);
    Splay(get_kth(root,r+2),root);
    return sum[Key_value];
}
void ADD(int l,int r,int val)
{
    Splay(get_kth(root,l),0);
    Splay(get_kth(root,r+2),root);
    update_add(Key_value,val);
    pushup(ch[root][1]);
    pushup(root);
}
int main()
{
    char op[10];
    int a,b,c;
    while(scanf("%d%d",&N,&Q)!=EOF)
    {
        for(int i=1;i<=N;i++)scanf("%d",&A[i]);
        init();
        while(Q--)
        {
            scanf("%s%d%d",op,&a,&b);
            if(op[0]=='Q')
            {
                LL ans=Query_sum(a,b);
                printf("%lld\n",ans);
            }
            else
            {
                scanf("%d",&c);
                ADD(a,b,c);
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值