Codeforces Round #616 (Div. 2) D
莫队的模板
struct node{
int l,r,id;
}q[maxn];
int cmp(node a,node b) {
return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & ) ? a.r < b.r : a.r > b.r);
}
n=strlen(s);
size = sqrt(n);
bnum = n / size;
for(int i = ; i <= bnum; ++i)
for(int j = (i - ) * size + ; j <= i * size; ++j) {
belong[j] = i;
}
int ql = q[i].l, qr = q[i].r;
while(l < ql) now -= !--cnt[aa[l++]];
while(l > ql) now += !cnt[aa[--l]]++;
while(r < qr) now += !cnt[aa[++r]]++;
while(r > qr) now -= !--cnt[aa[r--]];
ans[q[i].id] = now;//now代表ql~qr之间有多少不同的数字
思路:
区间有多少种不同的字母,ql==qr yes ,三种以上yes,首尾不相等的yes其他都是no
莫队写法
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define it register int
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
const int maxn=2e5+;
char s[maxn];
int cnt[maxn],belong[maxn],b[maxn],l=,r=,now=,n,ql,qr,ls,ci;
struct node{
int l,r,id,da;
}q[maxn];
int cmp(node a,node b) {
return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & ) ? a.r < b.r : a.r > b.r);
}
int cmp1(node a,node b){
return a.id<b.id;
}
il void add(int pos){
if(!cnt[s[pos]-'a']){++now;}
++cnt[s[pos]-'a'];
}
il void del(int pos){
--cnt[s[pos]-'a'];
if(!cnt[s[pos]-'a'])--now;
}
il int work(int q,int p){
while(l<q)del(l++);
while(l>q)add(--l);
while(r<p)add(++r);
while(r>p)del(r--);
return now;
}
int main(){
scanf("%s",s+);
ls=strlen(s+);
ci=sqrt(ls);
int ge=ls/ci;
for(it i=;i<=ge;i++){
for(it j=(i-)*ci+;j<=i*ci;j++){
belong[j]=i;
}
}
scanf("%d",&n);
for(it i=;i<n;i++){
scanf("%d%d",&q[i].l,&q[i].r);q[i].id=i;
}
sort(q,q+n,cmp);
for(it i=;i<n;i++){
it ql=q[i].l,qr=q[i].r;
it z=work(ql,qr);
if(ql==qr){
q[i].da=;
}
else if(z>=){
q[i].da=;
}
else if(s[ql]!=s[qr]){
q[i].da=;
}
else{
q[i].da=-;
}
}
sort(q,q+n,cmp1);
for(it i=;i<n;i++){
printf(q[i].da==?"Yes\n":"No\n");
}
return ;
}
二维树状数组写法
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define il inline
#define it register int
#define lowbit(x) (x)&(-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
const int maxn=2e5+;
char s[maxn];
struct node{
int tr[maxn];
void inint(){
memset(tr,,sizeof(tr));
}
void updata(int x,int y){
for(int i=x; i<=maxn; i+=lowbit(i)){
tr[i]+=y;
}
}
int geshu(int x,int y){
int sum1=,sum2=;
for(int i=x; i>; i-=lowbit(i)){
sum1+=tr[i];
}
for(int i=y; i>; i-=lowbit(i)){
sum2+=tr[i];
}
return sum2-sum1;
}
} a[];
int main(){
for(it i=;i<;i++){
a[i].inint();
}
scanf("%s",s+);
int ls=strlen(s+);
for(it i=;i<=ls;i++){
a[s[i]-'a'].updata(i,);
}
it n,l,r;
scanf("%d",&n);
for(it i=;i<n;i++){
scanf("%d%d",&l,&r);
it z=;
if(l==r || s[l]!=s[r]){
printf("Yes\n");continue;
}
for(it i=;i<;i++){
if(a[i].geshu(l,r)){
z++;
}
}
if(z>=){
printf("Yes\n");
}
else{
printf("No\n");
}
}
return ;
}
这场打得有些自闭了
搞不懂c题,B题wa4发发现思路是错误的,最后在辉辉给出正确思路之后才过
D题就瞄了一眼,以为很难,赛后补题写了一个二维树状数组就过了,看了大佬的博客发现可以用莫队写,就试着用模板写
Codeforces Round #616 (Div. 2) D的更多相关文章
- Codeforces Round #616 (Div. 2) B. Array Sharpening
t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...
- Codeforces Round #616 (Div. 2) C. Mind Control
题目链接:http://codeforces.com/contest/1291/problem/C 思路: 我们可以很容易想到,只有前m-1个人才能影响m的选择的大小,后面的人无法影响. 如果所有人都 ...
- Codeforces Round #616 (Div. 2)
地址:http://codeforces.com/contest/1291 A题就不写解析了,就是给一个数,是不是本身满足这个条件或者删除某些数字来达到这个条件:奇数,各个位上的数字加起来是偶数. # ...
- Codeforces Round #616 (Div. 2) 题解
A. Even But Not Even 题意: 定义一个数所有位置的和为偶数它本身不为偶数的数为ebne,现在给你一个数字字符串,你可以删除任意位置上的数字使其变为ebne输出任意改变后的结果,如果 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- Unity手机端手势基本操作
主要有单指移动3D物体.单指旋转3D物体.双指缩放3D物体. 基类 using UnityEngine; using System.Collections; /// <summary> / ...
- poj 3057(bfs+二分匹配)
题目链接:http://poj.org/problem?id=3057 题目大概意思是有一块区域组成的房间,房间的边缘有门和墙壁,'X'代表墙壁,'D'代表门,房间内部的' . '代表空区域,每个空区 ...
- 概率dp sgu495
题意: 有n个奖品,m个人排队来选礼物,对于每个人,他打开的盒子,可能有礼物,也有可能已经被之前的人取走了,然后把盒子放回原处.为最后m个人取走礼物的期望. 思路1: 排队取,第1个人取到1个,dp[ ...
- 吴裕雄 python 机器学习——数据预处理标准化StandardScaler模型
from sklearn.preprocessing import StandardScaler #数据预处理标准化StandardScaler模型 def test_StandardScaler() ...
- Java中查询某个日期下所有时间段的数据
除了利用时间段进行查询外,还有一个方法: 利用mybatis中的函数,将datetime转为date <if test="purch_date!= null and purch_dat ...
- Drawer 侧边栏、以及侧边栏内 容布局
一.Flutter Drawer 侧边栏 在 Scaffold 组件里面传入 drawer 参数可以定义左侧边栏,传入 endDrawer 可以定义右侧边栏.侧边栏默认是隐藏的,我们可以通过手指滑动显 ...
- bodyParser.urlencoded({ })里extended: true和false区别???
- Linux - Shell - 字符串拼接
概述 shell 的字符串拼接 1. 字符串声明 概述 字符串的基本操作 脚本 1 # 声明字符串 str01="str01" echo ${str01} # 单引号也可以 # 不 ...
- 数据库备份与还原:mysqldump,source
*数据库备份* 1.备份方法一:适用于myslam表: 直接将tb_name.frm.tb_name.myd.tb_name.myi三个文件保存,备份即可. 需要的时候直接解压到,移动到相应的数据库目 ...
- PHPStorm设置Ctrl+滚轮调整字体大小
1.点击左上角的File,再点击setting: 2.Editor->General,选择Change font size (Zoom) with Ctrl+Mouse Wheel: 3.点击O ...