bzoj 4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
来源:互联网 发布:管家婆数据导入失败 编辑:程序博客网 时间:2024/05/22 14:51
题意:
两列n的排列,相同的数连边,假如一对数的差
题解:
容易想到,按数从小到大加入,统计现在有多少数与当前数交差,再加i-k。
至于统计乱搞一下,正解是cdq,但本蒟蒻只会树套树,反正空间放开也过的了。官方数据都过了,精神AC。
为什么每次树套树都出SB错误啊啊啊啊。
code:
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>#define LL long longusing namespace std;struct node{ int a,b;}a[100010];struct trnode{ int lc,rc,c;}tr[40000000];int root[200010],tot=0;int lson[200010],rson[200010],len=0;int n,k;int lowbit(int x){return x&(-x);}void update(int &x,int l,int r,int k,int c){ if(!x) x=++tot; if(l==r){tr[x].c+=c;return;} int mid=(l+r)/2; if(k<=mid) update(tr[x].lc,l,mid,k,c); else update(tr[x].rc,mid+1,r,k,c); tr[x].c=tr[tr[x].lc].c+tr[tr[x].rc].c;}int findans(int x,int l,int r,int fl,int fr){ if(!x) return 0; if(l==fl&&r==fr) return tr[x].c; int mid=(l+r)/2; if(fr<=mid) return findans(tr[x].lc,l,mid,fl,fr); if(fl>mid) return findans(tr[x].rc,mid+1,r,fl,fr); return findans(tr[x].lc,l,mid,fl,mid)+findans(tr[x].rc,mid+1,r,mid+1,fr);}void change(int x,int l,int r,int k1,int k2,int c){ update(root[x],1,n,k2,c); if(l==r) return; int mid=(l+r)/2; if(k1<=mid) change(lson[x],l,mid,k1,k2,c); else change(rson[x],mid+1,r,k1,k2,c);}int get(int x,int l,int r,int fl,int fr,int k1,int k2){ if(l==fl&&r==fr) return findans(root[x],1,n,k1,k2); int mid=(l+r)/2; if(fr<=mid) return get(lson[x],l,mid,fl,fr,k1,k2); if(fl>mid) return get(rson[x],mid+1,r,fl,fr,k1,k2); return get(lson[x],l,mid,fl,mid,k1,k2)+get(rson[x],mid+1,r,mid+1,fr,k1,k2);}int bt(int l,int r){ int x=++len; if(l!=r) { int mid=(l+r)/2; lson[x]=bt(l,mid); rson[x]=bt(mid+1,r); } return x;}int main(){ scanf("%d %d",&n,&k); for(int i=1;i<=n;i++) { int x;scanf("%d",&x); a[x].a=i; } for(int i=1;i<=n;i++) { int x;scanf("%d",&x); a[x].b=i; } bt(1,n); LL ans=0; for(int i=k+1;i<=n;i++) { ans+=(LL)get(1,1,n,1,a[i].a,a[i].b,n); ans+=(LL)get(1,1,n,a[i].a,n,1,a[i].b); change(1,1,n,a[i-k].a,a[i-k].b,1); } printf("%lld",ans);}
cdq(抄的)
#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#define LL long longusing namespace std;const int Maxn = 100010;struct node { int num, a, b;}list[Maxn], b1[Maxn], b2[Maxn]; int bl1, bl2;bool cmp1(node x, node y) { return x.a < y.a; }bool cmp2(node x, node y) { return x.b > y.b; }int n, K;LL ans;int s[Maxn];int lowbit(int x) { return x & (-x); }void add(int x, int k) { while(x <= n){ s[x] += k; x += lowbit(x); } }int qry(int x) { int ret = 0; while(x > 0){ ret += s[x]; x -= lowbit(x); } return ret; }void cdq(int l, int r) { if(l == r) return; int mid = (l+r)>>1; bl1 = bl2 = 0; int i, j, k; for(i = l; i <= mid; i++) b1[++bl1] = list[i]; for(i = mid+1; i <= r; i++) b2[++bl2] = list[i]; sort(b1+1, b1+bl1+1, cmp2); sort(b2+1, b2+bl2+1, cmp2); j = 1; for(i = 1; i <= bl2; i++){ while(j <= bl1 && b1[j].b > b2[i].b){ add(b1[j].num, 1); j++; } ans += qry(b2[i].num-K-1); if(b2[i].num+K < n) ans += qry(n) - qry(b2[i].num+K); } j--; while(j) add(b1[j].num, -1), j--; cdq(l, mid); cdq(mid+1, r);}int main() { //freopen("b.in", "r", stdin); //freopen("b.out", "w", stdout); int i, j, k; scanf("%d%d", &n, &K); for(i = 1; i <= n; i++) list[i].num = i; for(i = 1; i <= n; i++){ int x; scanf("%d", &x); list[x].a = i; } for(i = 1; i <= n; i++){ int x; scanf("%d", &x); list[x].b = i; } sort(list+1, list+n+1, cmp1); cdq(1, n); printf("%lld\n", ans); return 0;}
数据生成器:
#include<cstdio>#include<cstdlib>#include<iostream>#include<ctime>using namespace std;int a[100010];int main(){ srand(time(0)); int n=rand()%5+1,k=rand()%(n+1); printf("%d %d\n",n,k); for(int i=1;i<=n;i++) a[i]=i; for(int i=1;i<=100;i++) { int x=rand()%n+1,y=rand()%n+1; swap(a[x],a[y]); } for(int i=1;i<=n;i++) printf("%d ",a[i]);printf("\n"); for(int i=1;i<=n;i++) a[i]=i; for(int i=1;i<=100;i++) { int x=rand()%n+1,y=rand()%n+1; swap(a[x],a[y]); } for(int i=1;i<=n;i++) printf("%d ",a[i]);printf("\n");}
阅读全文
2 0
- bzoj 4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套Treap
- [BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- BZOJ 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套splay【未AC】
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- unity点乘和叉乘的使用
- CCF201703-1 分蛋糕
- Mac上强大的清理工具CleanMyMac 激活码 免费下载
- Unity3D-Vector3
- 计算机网络另附抓包
- bzoj 4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- 在真机上执行react-native run-android出现com.android.ddmlib.InstallException: Failed to finalize session错误
- 终于知道 代码好坏的区别
- 汉化 工具 批量 自动化 批处理
- Unity 3D项目问题笔记 //01_在 unity 中切割 2D图片
- 谈历史
- 9/4...Bootstrap学习,及思路总结
- loadrunner Web_类函数之web_add_cookie_ex()
- PHP报错Cannot adopt OID in UCD-SNMP-MIB、 LM-SENSORS-MIB