LOJ #6277. 数列分块入门 1

\(\color{#0066ff}{题目描述}\)

给出一个长为 n 的数列,以及 n 个操作,操作涉及区间加法,单点查值。

\(\color{#0066ff}{输入格式}\)

第一行输入一个数字 n。

第二行输入 n 个数字,第 i 个数字为 \(a_i\) ,以空格隔开。

接下来输入 n 行询问,每行输入四个数字 \(\mathrm{opt},l,r,c,\)以空格隔开。

\(\mathrm{opt} = 0\),表示将位于 \([l,r]\) 的之间的数字都加 c。

\(\mathrm{opt} = 1\),表示询问 \(a_r\)的值(l 和 c 忽略)。

\(\color{#0066ff}{输出格式}\)

对于每次询问,输出一行一个数字表示答案。

\(\color{#0066ff}{输入样例}\)

4
1 2 2 3
0 1 3 1
1 0 1 0
0 1 2 2
1 0 2 0

\(\color{#0066ff}{输出样例}\)

2
5

\(\color{#0066ff}{题解}\)

最基础分块题,树状数组线段树随便切
区间加,整块打标记,散块暴力加
单点查询,权值+所在块标记
#include<cstdio>
#include<queue>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#define _ 0
#define LL long long
#define Space putchar(' ')
#define Enter putchar('\n')
#define fuu(x,y,z) for(int x=(y),x##end=z;x<=x##end;x++)
#define fu(x,y,z)  for(int x=(y),x##end=z;x<x##end;x++)
#define fdd(x,y,z) for(int x=(y),x##end=z;x>=x##end;x--)
#define fd(x,y,z)  for(int x=(y),x##end=z;x>x##end;x--)
#define mem(x,y)   memset(x,y,sizeof(x))
#ifndef olinr
inline char getc()
{
    static char buf[100001],*p1=buf,*p2=buf;
    return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100001,stdin),p1==p2)? EOF:*p1++;
}
#else
#define getc() getchar()
#endif
template<typename T>inline void in(T &x)
{
    int f=1; char ch; x=0;
    while(!isdigit(ch=getc()))(ch=='-')&&(f=-f);
    while(isdigit(ch)) x=x*10+(ch^48),ch=getc();
    x*=f;
}
const int inf=0x7fffffff;
struct K
{
    int tag,l,r;
    K() {l=inf,r=-inf;}
}e[50505];
struct seq
{
    int val,bel;
}a[50505];
int num;
int n;
inline void init()
{
    num=std::sqrt(n);
    fuu(i,1,n)
    {
        in(a[i].val),a[i].bel=(i-1)/num+1;
        e[a[i].bel].l=std::min(e[a[i].bel].l,i);
        e[a[i].bel].r=std::max(e[a[i].bel].r,i);
    }
}
inline void add(int l,int r,int c)
{
    fuu(i,a[l].bel+1,a[r].bel-1) e[i].tag+=c;
    fuu(i,l,std::min(r,e[a[l].bel].r)) a[i].val+=c;
    if(a[l].bel!=a[r].bel) fuu(i,std::max(l,e[a[r].bel].l),r) a[i].val+=c;
}
inline int query(int p)
{
    return a[p].val+e[a[p].bel].tag;
}
int main()
{
    in(n);
    init();
    int p,l,r,c;
    while(n--)
    {
        in(p),in(l),in(r),in(c);
        if(p==0) add(l,r,c);
        else printf("%d\n",query(r));
    }
    return ~~(0^_^0);
}

转载于:https://www.cnblogs.com/olinr/p/10065426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值