莫队的模板

 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的更多相关文章

  1. Codeforces Round #616 (Div. 2) B. Array Sharpening

    t题目链接:http://codeforces.com/contest/1291/problem/B 思路: 用极端的情况去考虑问题,会变得很简单. 无论是单调递增,单调递减,或者中间高两边低的情况都 ...

  2. Codeforces Round #616 (Div. 2) C. Mind Control

    题目链接:http://codeforces.com/contest/1291/problem/C 思路: 我们可以很容易想到,只有前m-1个人才能影响m的选择的大小,后面的人无法影响. 如果所有人都 ...

  3. Codeforces Round #616 (Div. 2)

    地址:http://codeforces.com/contest/1291 A题就不写解析了,就是给一个数,是不是本身满足这个条件或者删除某些数字来达到这个条件:奇数,各个位上的数字加起来是偶数. # ...

  4. Codeforces Round #616 (Div. 2) 题解

    A. Even But Not Even 题意: 定义一个数所有位置的和为偶数它本身不为偶数的数为ebne,现在给你一个数字字符串,你可以删除任意位置上的数字使其变为ebne输出任意改变后的结果,如果 ...

  5. 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 ...

  6. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. 【做题笔记】洛谷P1464 Function

    我先谔谔一波 /kk 我谔谔 看题第一眼:欸这不就是按题意递归嘛,,直接搞不就好了 3 min 后,重新看题 然后自己手玩了几个样例,噢,递归太多了,铁定会 T 啊...... 然后,作为一个从没写过 ...

  2. 【转载】C/C++预处理器

    转自:http://www.cnblogs.com/lidabo/archive/2012/08/27/2658909.html C/C++编译系统编译程序的过程为预处理.编译.链接.预处理器是在程序 ...

  3. Linux上后台运行node和springboot服务

    环境:Ubuntu18.04 阿里云云服务器 尝试全局安装forever和pm2均失败,最后以linux自带的nohub启动,以前同样用nohub启动springboot 命令: nohup npm ...

  4. 第十七篇 Linux下常用命令汇总

  5. Winform遍历窗口的所有控件(几种方式实现)

    本文链接:https://blog.csdn.net/u014453443/article/details/85088733 扣扣技术交流群:460189483 C#遍历窗体所有控件或某类型所有控件 ...

  6. springboot笔记-2-.核心的上下文以及配置扫描解析(上)

    前言 上一节中说明了springboot是如何做到自动发现配置的,那么本节看下spring如何创建上下文并解析这些配置,加载我们注册到容器管理中的类.上节已经成功的创建了SpringApplicati ...

  7. 第一篇:thinkPHP学习目的

    公司找人开发了一套程序,用的是thinkphp,我一直都是做前端的,后端对PHP也有一些了解,能看懂代码,但是不能写,因为公司不想再招人,嘱托我来维护. 翻看了thinkphp的官方文档,也去看了一下 ...

  8. 数据库程序接口——JDBC——功能第五篇——批量处理

    综述 批量处理一般指批量插入,批量更新,删除通过可以指定where条件实现.批量插入的实现方式有三种类型.statement,preparedStatement,callableStatement. ...

  9. Atcoder Beginner Contest151D(迷宫问题求任意两点最短路径的最大值,BFS)

    BFS可以求得最短路,DFS会找到从当前点到图中叶子结点的路径. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using na ...

  10. 6_2 铁轨(UVa514)<栈>

    在一个叫「堆叠市」的城市中有一个有名的火车站.由于地形限制以及经费的关系,火车站及唯一的铁路的样子如下图: 现在火车从A方向来,预定从B方向离开.火车共有N节车厢(N <=1000),并且各车厢 ...