zoukankan      html  css  js  c++  java
  • 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('
    ')
    #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
    ",query(r));
    	}
    	return ~~(0^_^0);
    }
    
  • 相关阅读:
    微服务、SpringCloud、k8s、Istio杂谈
    php环境安装
    最近重构公司消息服务的架构设计
    test
    博文目录(最新更新:2019.8.5)
    读过的书
    我在北京这几年(全)
    【原】深度学习的一些经验总结和建议 | To do v.s Not To Do
    如何高效利用一场技术分享?
    深度学习分布式训练及CTR预估模型应用
  • 原文地址:https://www.cnblogs.com/olinr/p/10065426.html
Copyright © 2011-2022 走看看