[随机 Hash] Codeforces 799F Round #413 F. Beautiful fountains rows
来源:互联网 发布:上海美工 编辑:程序博客网 时间:2024/06/15 11:08
就是给题目中给出的每个区间随一个值 然后判断一个区间是否合法 把区间内所有数xor和 再xor上 区间中出现过的所有数的xor和 为 0
前缀和统计下 本人脸黑 单hash交了好几发都是WA 迫不得已双hash
#include<cstdio>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#include<ctime>#define pb push_back using namespace std;typedef unsigned long long ull;typedef long long ll;typedef pair<ull,ull> abcd;inline ll sum(ll n){ return n*(n+1)/2;}inline ll sum2(ll n){ return n*(n+1)*(2*n+1)/6;}inline ll calc(ll n){ return (n+1)*sum(n)-sum2(n);}const int N=200005;#define read(x) scanf("%d",&(x))int n,m;int l[N],r[N];vector<int> L[N],R[N];ull h1[N],h2[N],a[N],b[N];map<abcd,ll> F,G;ll ans;int cnt[N];int main(){ srand(time(0)); freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(m); for (int i=1;i<=n;i++){ read(l[i]); read(r[i]); L[l[i]].pb(i); R[r[i]].pb(i); a[i]=(ull)rand()*rand()*rand()*rand()*rand(); b[i]=(ull)rand()*rand()*rand()*rand()*rand(); h1[l[i]]^=a[i]; h1[r[i]+1]^=a[i]; h2[l[i]]^=b[i]; h2[r[i]+1]^=b[i]; cnt[l[i]]++; cnt[r[i]+1]--; } for (int i=1;i<=m;i++) h1[i]^=h1[i-1],h2[i]^=h2[i-1]; for (int i=1;i<=m;i++) h1[i]^=h1[i-1],h2[i]^=h2[i-1]; G[abcd(0,0)]++; F[abcd(0,0)]=0; ull s1=0,s2=0,t1=0,t2=0; for (int i=1;i<=m;i++){ for (int x:L[i]) s1^=a[x],s2^=b[x]; ans+=G[abcd(s1^h1[i],s2^h2[i])]*i-F[abcd(s1^h1[i],s2^h2[i])]; for (int x:R[i]) t1^=a[x],t2^=b[x]; F[abcd(t1^h1[i],t2^h2[i])]+=i; G[abcd(t1^h1[i],t2^h2[i])]++; } for (int i=1;i<=m;i++) cnt[i]+=cnt[i-1]; for (int i=1;i<=m;) if (cnt[i]==0){ int j=i; while (j+1<=m && cnt[j+1]==0) j++; ans-=calc(j-i+1); i=j+1; }else i++; printf("%I64d\n",ans); return 0;}
阅读全文
0 0
- [随机 Hash] Codeforces 799F Round #413 F. Beautiful fountains rows
- [随机算法+Hash] Codeforces 799F Round #413 F. Beautiful fountains rows
- [Codeforces799F] [哈希] Beautiful fountains rows
- Codeforces Round #413:C. Fountains
- Codeforces Round #413 C. Fountains
- codeforces round #413 C. Fountains
- Codeforces Round 877 F
- Codeforces Round #427F && 835F
- Codeforces Round #413 Div. 2 C. Fountains
- Codeforces Round #413 C. Fountains 二分
- Codeforces Round #413 C-Fountains 树状数组
- 【Codeforces】452F Permutation hash+线段树
- 【线段树+Hash】Codeforces 452F Permutation
- [Educational Codeforces Round 17 F (762F)] Tree nesting
- Codeforces 864F Round#436 F :有向图倍增
- Codeforces 659F F
- CodeForces Round #145(234F) - Fence
- CodeForces Round #145(234F) - Fence
- rtmp协议整理
- [最短路 杂题] Codeforces 806D VK Cup 2017 Round 3 D. Perishable Roads
- [线段树 杂题] Codeforces 806E VK Cup 2017 Round 3 E. Blog Post Rating
- [杂题] Codeforces 799E Round #413 E. Aquarium decoration
- Qt简单知识点
- [随机 Hash] Codeforces 799F Round #413 F. Beautiful fountains rows
- 我的课程与服务
- OAUTH2.0
- 求联通块周长
- 图论(八)最小生成树
- 对绝对地址赋值的问题
- dubbo的配置及使用
- java Spring Boot 教程系列学习
- qt 多个字符串 序列化到文件注意事项。