当前位置:网站首页>D - Parity game离散化+带权并查集
D - Parity game离散化+带权并查集
2022-07-17 00:15:00 【HYYyyying】
废话之我是**
带权并查集,和那个how many answer are wrong差不多,冲啊!
嗯?怎么写了一发后mle了????
哦哦,原来还要离散化!
嗯?怎么离散化后还是wa?
哦哦,原来我的ans没有初始值为k(每一个询问都是正确的情况)
呜呜呜我好傻
题意
就是给你一个n(0<n<1e9),n是一个由0、1组成的串的长度,给你k次询问,每次询问l, r, ch,ch==“even”/“odd”, 意思就是l–r这个区间的和为偶数或者奇数。问每次询问是否是正确的(与前面的询问发生冲突的就是错误的),如果是错误的且是第一次错误,就输出上次一询问是第几次询问…
带权并查集啊,只不过n太大了,存不下,得先离散化
总结
离散化,将数存到数组里,排序 -> 去重 -> 二分查找
排序就sort就行,去重有个 unique函数,二分查找就lower_bound
然后就老带权并查集了,区间的左端要开区间(类似前缀似的 喵喵喵??)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define pq priority_queue
#define eps 1e-10
#define PI acos(-1.0)
int n, k;
int ok, ans;
int cnt;
const int maxn = 20005;
char s[maxn][10];
int L[maxn];
int a[maxn], b[maxn], fa[maxn], d[maxn];
int find(int x){
if(x == fa[x])
return x;
int tp = find(fa[x]);
d[x] ^= d[fa[x]];
return fa[x] = tp;
}
void init(){
for(int i = 0; i <= cnt; i++){
fa[i] = i;
d[i] = 0;
}
}
void Union(int x, int y, int val){
int fx = find(x);
int fy = find(y);
if(fx != fy){
fa[fy] = fx;
d[fy] ^= d[x] ^ d[y] ^ val;
}
else{
if(d[y] ^ d[x] != val && !ok){
// cout << x << " " << y << " " << val <<endl;
ok = 1;
}
}
}
int main(){
scanf("%d", &n);
scanf("%d", &k);
for(int i = 1; i <= k; i++){
scanf("%d %d", &a[i], &b[i]);
L[cnt++] = a[i] - 1;
L[cnt++] = b[i];
scanf(" %s", s[i]);
}
sort(L, L + cnt);
init();
cnt = unique(L, L + cnt) - L;
ans = k;
for(int i = 1; i <= k; i++){
int l = lower_bound(L, L + cnt, a[i] - 1) - L;
int r = lower_bound(L, L + cnt, b[i]) - L;
if(s[i][0] == 'e'){
Union(l, r, 0);
if(ok){
ans = i - 1;
break;
}
}
else{
Union(l, r, 1);
if(ok){
ans = i - 1;
break;
}
}
}
printf("%d\n", ans);
return 0;
}
边栏推荐
- 【工具篇】SQLite本地数据库在Unity3D的应用
- 【Unity开发小技巧】Unity混音器Mixer控制全局音量
- [unity development tips] unity packs the EXE on the PC side and compresses and packs it into an EXE file
- 转载:SQL注入常见绕过
- Leetcode 1: Two Sum
- STL--vector容器
- Lintcode 366:Fibonacci 斐波那契数列
- Summary of tree and heap knowledge points
- Leetcode 322: Coin Change - 动态规划
- 毕业论文word技巧集合
猜你喜欢

二叉树的遍历

树和堆知识点总结

(with word operation and video explanation) map registration using ArcGIS_ Projection transformation_ General map making_ Thematic map making

元宇宙公链Caduceus项目详解(一):Caduceus Metaverse Protocol的项目理念及技术框架

ENVI:(2022年最详细的教程)自定义坐标系

Gdb+vscode for debugging 4 - GDB executes relevant commands

Vmware Tools最新安装教程(RHEL8)

DoubleDQN的理论基础及其代码实现【Pytorch + Pendulum-v0】

ENVI_ IDL: read OMI data (HDF5) and output it as GeoTIFF file + detailed parsing

DQN理论基础及其代码实现【Pytorch + CartPole-v0】
随机推荐
Chapter 2 - system control principle - > classical control theory
ENVI:(2022年最详细的教程)自定义坐标系
Engineering compilation: makefile and cmake (I)
CTFHub----RCE
第1章-多智能体系统
攻防世界----shrine
[tools] unity2d character controller, which controls 2D players to move and jump in four directions and horizontal directions
【Unity开发小技巧】Unity混音器Mixer控制全局音量
MeterSphere基于JMeter分布式性能压测平台
SSTI模板注入
项目性能优化实战:解决首页白屏问题,自定义 loading 动画优化首屏效果
Line process of pool components
逻辑漏洞---登录验证码安全
成信大ENVI_IDL第一周实验测试:数组的简单运算+详细解析
动态规划问题 - 小兵向前冲
毕业论文word技巧集合
英文商务邮件常用语
Unity3D 游戏人物跳跃落地时发生弹跳,偏移情况的解决方法
成信大ENVI_IDL第三周课堂内容1:读取OMI数据(HDF5文件)以及输出+解析
Jmeter接口测试之响应断言