当前位置:网站首页>Prefix equality [DP | hash]
Prefix equality [DP | hash]
2022-07-19 14:20:00 【Codiplay】
E - Prefix Equality
The question : Give two sequences of integers a,b, Next is q Time to ask , Ask and give two integers x,y, ask a Before x Digit numbers and b Before y Whether the types of digits are exactly the same .
DP Thought !
- We only care about a front x Information stored in digits and b front y Bit stored information
- What does information store ? For this question , The numbers may be the same , We only care about where the numbers first appear , Store in prea Inside
- ansa For a Before x A digital ,b The maximum value of the coordinate where the corresponding number appears , If a Before x In the numbers b There is no certain number in , Set to inf.
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
int a[200010], b[200010];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
std::map<int, int> prea,preb;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
if(!prea.count(a[i]))prea[a[i]] = i;
}
for (int i = 1; i <= n; i ++ ) {
cin >> b[i];
if(!preb.count(b[i]))preb[b[i]] = i;
}
std::vector<int> ansa(n + 1, 0), ansb(n + 1, 0);
for (int i = 1; i <= n; i ++ ) {
ansa[i] = max(ansa[i - 1], preb.count(a[i]) ? preb[a[i]] : inf);
ansb[i] = max(ansb[i - 1], prea.count(b[i]) ? prea[b[i]] : inf);
}
int t;
cin >> t;
while(t -- ) {
int x, y;
cin >> x >> y;
cout << ((ansb[y] <= x && ansa[x] <= y) ? "Yes" : "No") << '\n';
}
return 0;
}Hash
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll a[200010], b[200010];
set<ll> se;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin >> n;
ll s = 0;
for (int i = 1; i <= n; i ++ ) {
ll x;
cin >> x;
if(se.find(x) == se.end()) {
s = (s + x * (x + 137) % mod * (x + 997) % mod) % mod;
}
a[i] = s;
se.insert(x);
}
se.clear();
s = 0;
for (int i = 1; i <= n; i ++ ) {
ll x;
cin >> x;
if(se.find(x) == se.end()) {
s = (s + x * (x + 137) % mod * (x + 997) % mod) % mod;
}
b[i] = s;
se.insert(x);
}
int t;
cin >> t;
while(t -- ) {
int x, y;
cin >> x >> y;
if(a[x] == b[y]) {
cout << "Yes\n";
}
else cout << "No\n";
}
return 0;
}边栏推荐
- 4某公司在6个城市c1,c2,c3…c6中有分公司,已知城市ci到cj(I,j=1,2,3,…6)的联通情况下及费用的大小列于以下带权邻接矩阵中C中
- Robotics at google:laura Graesser | i-sim2real: strengthen the learning robot strategy in the close human-computer interaction cycle
- Several small open source projects of mine over the years
- FreeRTOS personal notes - multi priority support
- NO.5整数的表示与运算
- Luogu p2422 good feeling solution
- si446使用记录(三):MATCH功能
- Microservice calling component feign practice
- Ranking and evaluation of the second "green tree Cup" Mathematics Competition
- Take a look at try{}catch{}
猜你喜欢
随机推荐
Comprehensive analysis of C language multimedia open source framework GStreamer
Version announcement | Apache Doris 1.1 release version officially released!
Huawei wireless device configuration dynamic load balancing
Silent AI: how does shengteng AI solve the problem of sign language learning with large models?
研二非科班研究生如何备战秋招
No.4 bits, bytes, information storage
How to prepare for the autumn recruitment for the graduate students of the second non science class of Graduate School
Use of Google browser developer tools (Master!)
Colliding mice collision engineering analysis
Notes with a face value of 10exp (303)
Okaleido or get out of the NFT siege, are you optimistic about it?
Some puzzles about data dictionary
Brief introduction of Bezier curve
非凸优化问题经典必看综述“从对称性到几何性”,罗切斯特大学等
00 后博士获聘南大特任副研究员,曾 4 岁上小学,14 岁考入南大!
NFT市场格局仍未变化,Okaleido能否掀起新一轮波澜?
How to quickly calculate the FID, is, sfid, precision, recall and other key evaluation indicators of the generated model?
Several small open source projects of mine over the years
O'Neill's RPS curve compilation method (original by Dr. Tao)
Unity realizes UI backpack equipment dragging function









