LOJ#6278. 数列分块入门 2

内存限制:256 MiB时间限制:500 ms标准输入输出

题目类型:传统评测方式:文本比较

上传者: hzwer

提交提交记录统计讨论测试数据

题目描述

给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的元素个数。

输入格式

第一行输入一个数字 nnn。

第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开。

接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔开。

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

若 opt=1\mathrm{opt} = 1opt=1,表示询问 [l,r][l, r][l,r] 中,小于 c2c^2c?2?? 的数字的个数。

输出格式

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

样例

样例输入

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

样例输出

3
0
2

数据范围与提示

对于 100% 100\%100% 的数据,1≤n≤50000,−231≤others 1 \leq n \leq 50000, -2^{31} \leq \mathrm{others}1≤n≤50000,−2?31??≤others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans≤2?31??−1。

对于区间和我们可以按照正常思路做。

对于第二个询问,我们可以用vector维护每个块内的有序表,

零散的块直接暴力,否则在vector内二分

注意vector的编号是从0开始的

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int MAXN=1e6+10;
const int INF=1e8+10;
inline char nc()
{
    static char buf[MAXN],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    char c=nc();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=nc();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=nc();}
    return x*f;
}
vector<int>v[1001];//用vector储存分块后块内的有序表
int block,L[MAXN],R[MAXN],a[MAXN],tag[MAXN],belong[MAXN],N;
void Sort(int p)
{
    v[p].clear();
    for(int i=L[p*block];i<=min(R[p*block],N);i++)
        v[p].push_back(a[i]);
    sort(v[p].begin(),v[p].end());
}
void IntervalAdd(int l,int r,int val)
{
    for(int i=l;i<=min(r,R[l]);i++) a[i]+=val;
    Sort(belong[l]);
    if(belong[l]!=belong[r])
    {
        for(int i=L[r];i<=r;i++)     a[i]+=val;
        Sort(belong[r]);
    }
    for(int i=belong[l]+1;i<=belong[r]-1;i++) tag[i]+=val;
}
int Query(int l,int r,int val)
{
    int ans=0;
    for(int i=l;i<=min(r,R[l]);i++)
        if(a[i]+tag[belong[l]]<val) ans++;
    if(belong[l]!=belong[r])
        for(int i=L[r];i<=r;i++)
            if(a[i]+tag[belong[r]]<val) ans++;
    for(int i=belong[l]+1;i<=belong[r]-1;i++)
    {
        int x=val-tag[i];
        ans+=lower_bound(v[i].begin(),v[i].end(),x)-v[i].begin();
    }
    return ans;
}
int main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
    freopen("b.out","w",stdout);
    #else
    #endif
    N=read();block=sqrt(N);
    for(int i=1;i<=N;i++) a[i]=read();
    for(int i=1;i<=N;i++) belong[i]=(i-1)/block+1,L[i]=(belong[i]-1)*block+1,R[i]=belong[i]*block;
    for(int i=1;i<=N;i++) v[belong[i]].push_back(a[i]);
    for(int i=1;i<=belong[N];i++) Sort(i);
    for(int i=1;i<=N;i++)
    {
        int opt=read(),l=read(),r=read(),c=read();
        if(opt==0) IntervalAdd(l,r,c);
        else        printf("%d\n",Query(l,r,c*c));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zwfymqz/p/8445216.html

时间: 2024-07-30 01:39:58

LOJ#6278. 数列分块入门 2的相关文章

LOJ#6284. 数列分块入门 8

#6284. 数列分块入门 8 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论 1 测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间询问等于一个数 ccc 的元素,并将这个区间的所有元素改为 ccc. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入三个数字 ll

Loj 6279. 数列分块入门 3

题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的前驱(比其小的最大元素). 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 iii 个数字为 aia_iai?,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字

LibreOJ 6278. 数列分块入门 2 题解

题目链接:https://loj.ac/problem/6278 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,询问区间内小于某个值 \(x\) 的元素个数. 输入格式 第一行输入一个数字 \(n\). 第二行输入 \(n\) 个数字,第 \(i\) 个数字为 \(a_i\),以空格隔开. 接下来输入 \(n\) 行询问,每行输入四个数字 \(opt\).\(l\).\(r\).\(c\),以空格隔开. 若 \(opt=0\),表示将位于 \([l,r]

Loj 6277 数列分块入门 1

题目链接:https://loj.ac/problem/6277 题面: 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 iii 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l,

loj#6285 数列分块入门 9 ( 回 滚 )

题目 :  链接 :https://loj.ac/problem/6285 题意:给出一个长为 n的数列,以及 n个操作,操作涉及询问区间的最小众数. 思路:虽然这不是一道 回滚莫队题,就是 暴力分块 的题, 但是 还是 可以用回滚莫队 写滴,好像大部分题解都是 暴力分块. #include<bits/stdc++.h> #define LL long long #define ULL unsigned long long #define rep(i,j,k) for(int i=j;i<

LOJ#6281. 数列分块入门 5

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论 1 测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间开方,区间求和. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 opt=

LOJ#6282. 数列分块入门 6

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及单点插入,单点询问,数据随机生成. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 

LOJ#6283. 数列分块入门 7

内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计讨论测试数据 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间乘法,区间加法,单点询问. 输入格式 第一行输入一个数字 nnn. 第二行输入 nnn 个数字,第 i 个数字为 aia_ia?i??,以空格隔开. 接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt.lll.rrr.ccc,以空格隔开. 若 op

LiberOJ 6278 数列分块入门 2(分块)

 题解:非常高妙的分块,每个块对应一个桶,桶内元素全部sort过,加值时,对于零散块O(sqrt(n))暴力修改,然后暴力重构桶.对于大块直接整块加.查询时对于非完整块O(sqrt(n))暴力遍历.对于完整的大块用lower_bound或者手写二分log(sqrt(n)查找,总复杂度O(n*sqrt(n)*log(sqrt(n))) 代码如下: #include<cmath> #include<vector> #include<cstdio> #include<c