莫队大法好
洛谷P3901 数列找不同
#include<bits/stdc++.h>
#define IL inline
#define R register int
using namespace std;
const int N=1e5+5,inf=0x3f3f3f3f;
IL int read() {
int f=1;
char ch;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
int res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
return res*f;
}
int n,m,len,cnt[N],curl,curr,ans,a[N];//curl=1和curl=0皆可
bool o[N];
struct hh {
int l,r,id;
bool operator <(const hh &a) const {
if(l/len==a.l/len) return r<a.r;
return l<a.l;
}
} q[N];
void add(int x) {
if((++cnt[a[x]])==1) ans++;
}
void del(int x) {
if(--(cnt[a[x]])==0) ans--;
}
int main() {
n=read();
m=read();
len=sqrt(n);
for(R i=1; i<=n; ++i) a[i]=read();
for(R i=1; i<=m; ++i) q[i].l=read(),q[i].r=read(),q[i].id=i,o[i]=0;
sort(q+1,q+1+m);
for(R i=1; i<=m; ++i) {
while(q[i].l>curl) del(curl++);
while(q[i].l<curl) add(--curl);
while(q[i].r>curr) add(++curr);
while(q[i].r<curr) del(curr--);
if(ans==(q[i].r-q[i].l+1)) o[q[i].id]=1;
}
for(R i=1; i<=m; ++i)
if(o[i]) puts("Yes");
else puts("No");
return 0;
}
洛谷P2709 小B的询问
#include<bits/stdc++.h>
#define int long long
#define IL inline
#define R register int
using namespace std;
const int N=1e5+5,inf=0x3f3f3f3f;
IL int read() {
int f=1;
char ch;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
int res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
return res*f;
}
int n,m,len,cnt[N],curl=1,curr,ans,a[N],k;//注意!curl=1
int o[N];
struct hh {
int l,r,id;
bool operator <(const hh &a) const {
if(l/len==a.l/len) return r<a.r;
return l<a.l;
}
} q[N];
void add(int x) {
ans+=2*cnt[a[x]]+1;
cnt[a[x]]++;
}
void del(int x) {
ans-=2*cnt[a[x]]-1;//完全平方展开
cnt[a[x]]--;
}
signed main() {
n=read();
m=read();
k=read();
len=sqrt(n);
for(R i=1; i<=n; ++i) a[i]=read();
for(R i=1; i<=m; ++i) q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+1+m);
for(R i=1; i<=m; ++i) {
while(curl<q[i].l) del(curl++);
while(curl>q[i].l) add(--curl);
while(curr<q[i].r) add(++curr);
while(curr>q[i].r) del(curr--);
o[q[i].id]=ans;
}
for(R i=1; i<=m; ++i) printf("%lld\n",o[i]);
return 0;
}
洛谷P4462 [CQOI2018]异或序列
#include<bits/stdc++.h>
#define int long long
#define IL inline
#define R register int
using namespace std;
const int N=2e5+5,inf=0x3f3f3f3f;
IL int read() {
int f=1;
char ch;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
int res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
return res*f;
}
int n,m,len,cnt[N],curl=1,curr,ans,a[N],k;
int o[N];
struct hh {
int l,r,id;
bool operator <(const hh &a) const {
if(l/len==a.l/len) return r<a.r;
return l<a.l;
}
} q[N];
void add(int x) {
ans+=cnt[a[x]^k];
cnt[a[x]]++;//顺序Attention!
}
void del(int x) {
cnt[a[x]]--;
ans-=cnt[a[x]^k];
}
//区间:[l-1,r]
signed main() {
n=read();
m=read();
k=read();
len=sqrt(n);
for(R i=1; i<=n; ++i) a[i]=read(),a[i]^=a[i-1];
for(R i=1; i<=m; ++i) q[i].l=read()-1,q[i].r=read(),q[i].id=i;//Attention!q[i].l--;
sort(q+1,q+1+m);
for(R i=1; i<=m; ++i) {
while(curl<q[i].l) del(curl++);
while(curl>q[i].l) add(--curl);
while(curr<q[i].r) add(++curr);
while(curr>q[i].r) del(curr--);
o[q[i].id]=ans;
}
for(R i=1; i<=m; ++i) printf("%lld\n",o[i]);
return 0;
}
CF617E XOR and Favorite Number (同上一题,数据范围有所变化)
洛谷P1972 [SDOI2009]HH的项链(著名卡莫队题,然鹅我还是用莫队卡过了,感谢讨论帖的好心人!)
#include<bits/stdc++.h>
//#define int long long
#define IL inline
#define R register int
using namespace std;
const int N=1e6+5,inf=0x3f3f3f3f;
IL int read() {
int f=1;
char ch;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
int res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
return res*f;
}
int n,m,len,cnt[N],curl=1,curr,ans,a[N],k,b[N];
int o[N];
struct hh {
int l,r,id;
bool operator <(const hh &a) const {
if(b[l]==b[a.l])
if(b[l]&1) return r<a.r;
else return r>a.r;
return l<a.l;
}//奇偶性排序
} q[N];
void add(int x) {
cnt[a[x]]++;
ans+=cnt[a[x]]==1;//少用if也会变快
}
void del(int x) {
cnt[a[x]]--;
ans-=cnt[a[x]]==0;
}
int main() {
n=read();
len=sqrt(n);
for(R i=1; i<=n; ++i) a[i]=read(),b[i]=(i-1)/len+1;//预处理b数组会更快
m=read();
for(R i=1; i<=m; ++i) q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+1+m);
for(R i=1; i<=m; ++i) {
while(curl<q[i].l) del(curl++);
while(curl>q[i].l) add(--curl);
while(curr<q[i].r) add(++curr);
while(curr>q[i].r) del(curr--);
o[q[i].id]=ans;
}
for(R i=1; i<=m; ++i) printf("%d\n",o[i]);
return 0;
}
SP3267 DQUERY - D-query(上一题的弱化版,然鹅至今未过,一直在waiting)
洛谷P1494 [国家集训队]小Z的袜子
#include<bits/stdc++.h>
#define int long long
#define IL inline
#define R register int
using namespace std;
const int N=1e5+5,inf=0x3f3f3f3f;
IL int read() {
int f=1;
char ch;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
int res=ch-'0';
while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0';
return res*f;
}
int n,m,len,cnt[N],curl,curr,ans,a[N],k;
int gcd(int x,int y) {return y?gcd(y,x%y):x;}
struct Ans {
int x,y;
void out() {
if(!x) puts("0/1");
else {
int d=gcd(x,y);
x/=d;y/=d;
printf("%lld/%lld\n",x,y);
}
}
}o[N];
struct hh {
int l,r,id;
bool operator <(const hh &a) const {
if(l/len==a.l/len) return r<a.r;
return l<a.l;
}
} q[N];
void add(int i) {
if(cnt[a[i]]>0) ans+=cnt[a[i]]*(cnt[a[i]]+1)-cnt[a[i]]*(cnt[a[i]]-1);//也可以这样写:ans+=cnt[a[i]]*2;
cnt[a[i]]++;
}
void del(int i) {
if(cnt[a[i]]>1) ans+=(cnt[a[i]]-1)*(cnt[a[i]]-2)-cnt[a[i]]*(cnt[a[i]]-1);//也可以这样写:ans-=(cnt[a[i]]-1)*2;
cnt[a[i]]--;
}
signed main() {
n=read();
m=read();
len=sqrt(n);
for(R i=1; i<=n; ++i) a[i]=read();
for(R i=1; i<=m; ++i) q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+1,q+1+m);
for(R i=1; i<=m; ++i) {
while(curl<q[i].l) del(curl++);
while(curl>q[i].l) add(--curl);
while(curr<q[i].r) add(++curr);
while(curr>q[i].r) del(curr--);
o[q[i].id]=(Ans){ans,(q[i].r-q[i].l+1)*(q[i].r-q[i].l)};
}
for(R i=1; i<=m; ++i) o[i].out();
return 0;
}