【线段树+Hash】Codeforces 452F Permutation
来源:互联网 发布:mac os 重装系统 编辑:程序博客网 时间:2024/06/05 20:25
题面在这里
按顺序枚举i,则
维护权值线段树
其实就是
线段树维护正反两个hash值就好了
示例程序:
#include<cstdio>#include<algorithm>#define mp make_pair#define Fst first#define Sec secondusing namespace std;typedef unsigned long long ull;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 red(){ int res=0,f=1;char ch=nc(); while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();} while ('0'<=ch&&ch<='9') res=(res<<3)+(res<<1)+ch-48,ch=nc(); return res*f;}const int maxn=300005;int n;ull p[maxn];struct node{ node *l,*r; int L,R;ull a,b; node () {} node (int _L,int _R):L(_L),R(_R),a(0),b(0) {} void pushup(){ if (L==R) return; a=l->a*p[r->R - r->L +1]+r->a; b=l->b+ r->b*p[l->R - l->L +1]; }}base[maxn<<1],nil;typedef node* P_node;P_node null,Rot,len;void init(){ nil=node(0,0);null=&nil; null->l=null->r=null;len=base;}P_node newnode(int L,int R){ *len=node(L,R); len->l=len->r=null; return len++;}P_node build(int L,int R){ P_node x=newnode(L,R); if (L==R) return x; int mid=L+R>>1; x->l=build(L,mid);x->r=build(mid+1,R); return x;}void insert(P_node x,int k){ if (x->L==x->R) {x->a=x->b=1;return;} int mid=x->L+x->R>>1; if (k<=mid) insert(x->l,k);else insert(x->r,k); x->pushup();}pair<int,int> A,B;void queryA(P_node x,int L,int R){ if (R<x->L||x->R<L) return; if (L<=x->L&&x->R<=R) {A.Fst=x->a*p[A.Sec]+A.Fst;A.Sec+=x->R-x->L+1;return;} queryA(x->r,L,R);queryA(x->l,L,R);}void queryB(P_node x,int L,int R){ if (R<x->L||x->R<L) return; if (L<=x->L&&x->R<=R) {B.Fst=x->b*p[B.Sec]+B.Fst;B.Sec+=x->R-x->L+1;return;} queryB(x->l,L,R);queryB(x->r,L,R);}int main(){ n=red();p[0]=1; for (int i=1;i<=n;i++) p[i]=p[i-1]*233; init();Rot=build(1,n); for (int i=1;i<=n;i++){ int x=red(),k=min(x-1,n-x); A=B=mp(0,0); queryA(Rot,x-k,x);queryB(Rot,x,x+k); if (A.Fst!=B.Fst) return printf("YES"),0; insert(Rot,x); } printf("NO"); return 0;}
阅读全文
1 0
- 【Codeforces】452F Permutation hash+线段树
- 【线段树+Hash】Codeforces 452F Permutation
- Codeforces 452F Permutation【线段树】【哈希】
- [线段树+哈希] Codeforces 452F. Permutation
- Codeforces452F Permutation -- 线段树 + Hash
- 【线段树+Hash】Codeforces452F[Permutation]题解
- codeforces 863F Almost Permutation
- Codeforces Round #452 (Div. 2) F. Letters Removing 线段树
- Codeforces452F(MemSQL Start[c]UP 2.0)[Permutation]--线段树+Hash
- 【CodeForces】240F - TorCoder 线段树
- codeforces 474F Ant colony(线段树)
- Codeforces 240F. TorCoder 线段树
- Codeforces Round #397 F Souvenirs(线段树)
- CodeForces 240F TorCoder(线段树)
- Codeforces 899F Letters Removing [线段树]
- Codeforces 899F Letters Removin-线段树
- codeforces 474F F. Ant colony(线段树+数论)
- codeforces 474F F. Ant colony(线段树+数论)
- 软件测试工程师必读图书
- JSP代码练习(六)
- 《数据结构》实验二:线性表综合实验——(5)间接寻址
- Servlet与MVC分层开发
- Leetcode之Rotate List 问题
- 【线段树+Hash】Codeforces 452F Permutation
- 一点一点学线程(四)concurrent包
- xshell连接树莓派lxpanel配置
- Codeforces Round #438 题解
- 数据结构——顺序表有序顺序表归并
- 搜集糖果
- 链表的相应操作
- OS之并发与并行,异步和多线程的区别
- Qt 学习之路 2(70):进程间通信