【题解】Luogu P4867 Gty的二逼妹子序列
原题传送门
同Luogu P4396 [AHOI2013]作业
询问多了10倍,但还能跑过(smog
#include <bits/stdc++.h>
#define N 100005
#define M 1000005
//#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
int n,m,blocksize=0,ans[M],v[N],p[N];
struct BinaryIndexTree{
int tr[N];
inline void update(register int pos,register int x)
{
for(register int i=pos;i<=n;i+=i&(-i))
tr[i]+=x;
}
inline int query(register int pos)
{
int res=0;
for(register int i=pos;i;i-=i&(-i))
res+=tr[i];
return res;
}
}tr;
struct query{
int l,r,a,b,id,bl;
}q[M];
inline bool cmp(register query a,register query b)
{
return a.bl!=b.bl?a.l<b.l:((a.bl&1)?a.r<b.r:a.r>b.r);
}
inline void add(register int x)
{
if(++p[v[x]]==1)
tr.update(v[x],1);
}
inline void del(register int x)
{
if(--p[v[x]]==0)
tr.update(v[x],-1);
}
int main()
{
n=read(),m=read();
blocksize=sqrt(n);
for(register int i=1;i<=n;++i)
v[i]=read();
for(register int i=1;i<=m;++i)
q[i].l=read(),q[i].r=read(),q[i].a=read(),q[i].b=read(),q[i].id=i,q[i].bl=(q[i].l-1)/blocksize+1;
sort(q+1,q+1+m,cmp);
int l=1,r=0;
for(register int i=1;i<=m;++i)
{
int ll=q[i].l,rr=q[i].r;
while(l>ll)
add(--l);
while(l<ll)
del(l++);
while(r<rr)
add(++r);
while(r>rr)
del(r--);
ans[q[i].id]=tr.query(q[i].b)-tr.query(q[i].a-1);
}
for(register int i=1;i<=m;++i)
write(ans[i]),puts("");
return 0;
}
【题解】Luogu P4867 Gty的二逼妹子序列的更多相关文章
- P4867 Gty的二逼妹子序列
题目描述 Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们规定妹子们的美 ...
- 洛谷 P4867 Gty的二逼妹子序列
链接: P4867 题意: 给出长度为 \(n(1\leq n\leq 10^5)\) 的序列 \(s\),保证\(1\leq s_i\leq n\).有 \(m(1\leq m\leq 10^6)\ ...
- Luogu 4867 Gty的二逼妹子序列
BZOJ3809,是权限题. 我永远喜欢莫队. 先莫队一下移下左右指针,然后用一个数据结构维护一下区间$[a, b]$中的颜色的值,跟着指针移动一起修改修改,每一次$query$的时候就相当于查询一下 ...
- 洛谷P4867 Gty的二逼妹子序列(莫队+树状数组)
传送门 本来打算用主席树 然后发现没办法维护颜色数 于是用了莫队加树状数组 然后竟然A了…… //minamoto #include<iostream> #include<cstdi ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- Bzoj 3809: Gty的二逼妹子序列 莫队,分块
3809: Gty的二逼妹子序列 Time Limit: 35 Sec Memory Limit: 28 MBSubmit: 868 Solved: 234[Submit][Status][Dis ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
- BZOJ 3809: Gty的二逼妹子序列
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1387 Solved: 400[Submit][Status][Di ...
- 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法
3809: Gty的二逼妹子序列 Time Limit: 80 Sec Memory Limit: 28 MBSubmit: 1072 Solved: 292[Submit][Status][Di ...
随机推荐
- C# 数字字符串前面不足位补零方法
; Console.WriteLine(i.ToString("D3")); Console.WriteLine(i.ToString(")); Console.Writ ...
- Nuxt.js调用asyncData
<template> <div> Index {{ username }} </div> </template> <script> expo ...
- Python中的引用传参
Python中函数参数是引用传递(注意不是值传递).对于不可变类型,因变量不能修改,所以运算不会影响到变量自身:而对于可变类型来说,函数体中的运算有可能会更改传入的参数变量. 引用传参一: >& ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- layui 将后台传过来的值等价替换
<th lay-data="{field:'opentime',width:'12%' , sort: true, align:'center',templet:'#roleTpl'} ...
- Java学习-052-(mybatis+mysql)访问接口时提示:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
在配置mybatis,访问接口提示: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found),部 ...
- pandas(二)
层级索引: index=[('a',2010),('b',2011),('c',2010'),('a',2012),('e',2010),('f',2011)] age=[18,17,18,16,18 ...
- DS1-13
#include <stdio.h> #define MAXSIZE 10000 int Max3(int A, int B, int C); int DivideAndConquer(i ...
- 【Idea】idea waiting until last debugger command completes
https://blog.csdn.net/KingBoyWorld/article/details/73440717 以上方法可以解决 另: 有说: 并未尝试 https://stackoverfl ...
- windows下安装php reids扩展
1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本. 2.下载php_igbinary-1.2.1-5.5-ts-vc11-x64.zip,php_redis-2.2.5-5.6 ...