【Codeforces323C】Two permutations
来源:互联网 发布:免流量软件下载 编辑:程序博客网 时间:2024/06/07 15:28
题意:
给你两个长度为
转为一个序列的问题后就是一道主席树模板题了。
#include <bits/stdc++.h>#define gc getchar()#define mid (l+r>>1)#define N 2000009#define M 40000009using namespace std;int lson[M],rson[M],sum[M],root[N];int n,a[N],pos[N],Q,ans,cnt;int read(){ int x=1; char ch; while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1; int s=ch-'0'; while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0'; return s*x;}void ins(int &cur,int l,int r,int x,int last){ cur=++cnt; lson[cur]=lson[last]; rson[cur]=rson[last]; sum[cur]=sum[last]+1; if (l==r) return; if (x<=mid) ins(lson[cur],l,mid,x,lson[last]); else ins(rson[cur],mid+1,r,x,rson[last]);}int qry(int cur,int l,int r,int L,int R){ if (L<=l&&R>=r) return sum[cur]; int ret=0; if (L<=mid) ret+=qry(lson[cur],l,mid,L,R); if (R>mid) ret+=qry(rson[cur],mid+1,r,L,R); return ret;}int f(int x){ return (x-1+ans)%n+1;}int main(){ n=read(); for (int i=1;i<=n;i++) pos[read()]=i; for (int i=1;i<=n;i++) a[i]=pos[read()]; for (int i=1;i<=n;i++) ins(root[i],1,n,a[i],root[i-1]); Q=read(); while (Q--) { int x=f(read()),y=f(read()),z=f(read()),w=f(read()); if (x>y) swap(x,y); if (z>w) swap(z,w); printf("%d\n",ans=qry(root[w],1,n,x,y)-qry(root[z-1],1,n,x,y)); ans++; } return 0;}
阅读全文
0 0
- 【Codeforces323C】Two permutations
- Codeforces 213E - Two Permutations
- CF 213E Two Permutations(线段树,hash)
- 【线段树+字符串hash】 codeforces 213E Two Permutations
- Hash练习题:【213E】 Two Permutations(线段树,hash)
- CF 213E Two Permutations(线段树,hash)
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- tensorflow.python.platform.gfile
- CSDN Markdown使用教程
- 反编译工具cfr和procyon
- JavaMail使用SSL遇到安全证书问题
- Java大整数运算之计算1!+2!+…+100!的总和.
- 【Codeforces323C】Two permutations
- Android开发之bug收集(持续更新...)
- h5圆形进度条
- tp3.2zfb
- 串--->串的模式匹配:Brute-Force算法和 KMP算法
- XCode9变化
- 第四周项目3.2
- 洛谷 P3390 矩阵快速幂模板
- 使用canal进行mysql数据同步到Redis