Description

小 C 的兔子不是雪白的,而是五彩缤纷的。每只兔子都有一种颜色,不同的兔子可能有 相同的颜色。小 C 把她标号从 \(1\) 到 \(n\) 的 \(n\) 只兔子排成长长的一排,来给他们喂胡萝卜吃。 排列完成后,第 \(i\) 只兔子的颜色是 \(a_i\)。

俗话说得好,“萝卜青菜,各有所爱”。小 C 发现,不同颜色的兔子可能有对胡萝卜的 不同偏好。比如,银色的兔子最喜欢吃金色的胡萝卜,金色的兔子更喜欢吃胡萝卜叶子,而 绿色的兔子却喜欢吃酸一点的胡萝卜……为了满足兔子们的要求,小 C 十分苦恼。所以,为 了使得胡萝卜喂得更加准确,小 C 想知道在区间 \([l_j,r_j]\) 里有多少只颜色为 \(c_j\) 的兔子。

不过,因为小 C 的兔子们都十分地活跃,它们不是很愿意待在一个固定的位置;与此同 时,小 C 也在根据她知道的信息来给兔子们调整位置。所以,有时编号为 \(x_j\) 和 \(x_j+1\) 的两 只兔子会交换位置。 小 C 被这一系列麻烦事给难住了。你能帮帮她吗?

Input

从标准输入中读入数据。 输入第 1 行两个正整数 \(n,m\)。

输入第 2 行 \(n\) 个正整数,第 \(i\) 个数表示第 \(i\) 只兔子的颜色 \(a_i\)。

输入接下来 \(m\) 行,每行为以下两种中的一种:

  • “\(1\ l_j\ r_j\ c_j\)” :询问在区间 \([l_j,r_j]\) 里有多少只颜色为 \(c_j\) 的兔子;
  • “\(2\ x_j\)”: \(x_j\) 和 \(x_j+1\) 两只兔子交换了位置。

Output

输出到标准输出中。

对于每个 1 操作,输出一行一个正整数,表示你对于这个询问的答案。

woc,写了主席树.结果T了4个点,尝试大力卡常也没过QAQ.

主席树(80pts)

对于颜色建树.

考虑到主席树的性质是维护前缀.

所以我们可以直接查询,可以直接修改.

修改操作是这样的

可能叫build会比较奇怪

build(root[l],root[l],1,300000,col[l],-1);
build(root[l],root[l],1,300000,col[l+1],1);

这个应该不是很难理解.(如果你会主席树的话.)

不理解的话,再想想主席树的性质好了.

80pts代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#define R register
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
void print(int x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)print(x/10);
putchar(x%10+'0');
}
int sum[20000000],lson[20000000],rson[20000000],root[20000000];
int cnt,n,m,col[500003];
void build(int lastroot,int &nowroot,int l,int r,int pos,int k)
{
nowroot=++cnt;
sum[nowroot]=sum[lastroot]+k;
lson[nowroot]=lson[lastroot];
rson[nowroot]=rson[lastroot];
if(l==r)return;
int mid=(l+r)>>1;
if(pos<=mid)
build(lson[lastroot],lson[nowroot],l,mid,pos,k);
else
build(rson[lastroot],rson[nowroot],mid+1,r,pos,k);
}
int query(int lastroot,int nowroot,int l,int r,int pos)
{
if(l==r)return sum[nowroot]-sum[lastroot];
int mid=(l+r)>>1;
if(pos<=mid) return query(lson[lastroot],lson[nowroot],l,mid,pos);
else return query(rson[lastroot],rson[nowroot],mid+1,r,pos);
}
int main()
{
in(n),in(m);
for(R int i=1;i<=n;i++)
{
in(col[i]);
build(root[i-1],root[i],1,300000,col[i],1);
}
for(R int i=1,opt,l,r,k;i<=m;i++)
{
in(opt);
if(opt==1)
{
in(l),in(r),in(k);
print(query(root[l-1],root[r],1,300000,k));
putchar('\n');
}
else
{
in(l);
build(root[l],root[l],1,300000,col[l],-1);
build(root[l],root[l],1,300000,col[l+1],1);
swap(col[l],col[l+1]);
}
}
}

Vector+二分 (100pts)

用\(Vector\)记录每个颜色出现的位置,然后二分.

代码

#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define R register
#define maxn 300010
#define inf 2147483640
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
vector < int > col[maxn] ;
int n , init[maxn] , m ;
inline void work(int l , int r , int x)
{
int posl = lower_bound(col[x].begin() , col[x].end() , l) - col[x].begin() ;
int posr = upper_bound(col[x].begin() , col[x].end() , r) - col[x].begin() - 1 ;
printf("%d",posr-posl+1);
}
inline void work(int x)
{
int l = x , r = x + 1 ;
if(init[l] == init[r]) return ;
int posl = lower_bound(col[init[l]].begin() , col[init[l]].end() , l) - col[init[l]].begin() ;
int posr = lower_bound(col[init[r]].begin() , col[init[r]].end() , r) - col[init[r]].begin() ;
col[init[l]][posl] ++ ;
col[init[r]][posr] -- ;
swap(init[l] , init[r]) ;
}
int main()
{
in(n),in(m) ; int opt , l , r , x ;
for(int i = 1 ; i <= n ; ++i) in(init[i]),col[init[i]].push_back(i);
for(int i = 1 ; i <= m ; ++i)
{
in(opt);
if(opt == 1) in(l),in(r),in(x) , work(l , r , x) ;
else in(x),work(x) ;
}
return 0 ;
}

主席树 STL+二分【p3939】数颜色的更多相关文章

  1. 2018.07.07 洛谷 P3939 数颜色(主席树)

    P3939 数颜色 题目背景 大样例下发链接:http://pan.baidu.com/s/1c0LbQ2 密码:jigg 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不 ...

  2. 洛谷——P3939 数颜色(暴力vecotr+二分)

    P3939 数颜色 $vecotr$里二分就是好用,全是$STL$ 颜色数目比较少,可以对每一种颜色弄一个$vector$记录一下,查找$l,r$内颜色数为$x$的兔子数,直接在$G[x]$这个$ve ...

  3. [luogu]P3939 数颜色[二分]

    [luogu]P3939 数颜色 题目描述 小 C 的兔子不是雪白的,而是五彩缤纷的.每只兔子都有一种颜色,不同的兔子可能有 相同的颜色.小 C 把她标号从 1 到 n 的 n 只兔子排成长长的一排, ...

  4. P3939 数颜色 线段树动态开点

    P3939 数颜色 线段树动态开点 luogu P3939 水.直接对每种颜色开个权值线段树即可,注意动态开点. #include <cstdio> #include <algori ...

  5. 2019.8.3 NOIP模拟测试12 反思总结【P3938 斐波那契,P3939 数颜色,P3940 分组】

    [题解在下面] 早上5:50,Gekoo同学来到机房并表态:“打暴力,打暴力就对了,打出来我就赢了.” 我:深以为然. (这是个伏笔) 据说hzoi的人还差两次考试[现在是一次了]就要重新分配机房,不 ...

  6. P3939 数颜色

    目录 题目 思路1(待修莫队) 思路2(vector+二分) 代码1 代码2 题目 P3939 数颜色 思路1(待修莫队) 哇,这不是莫队模板题吗 3e5,TLE45分 不行 我有信仰啊 pow(n, ...

  7. 【题解】P3939数颜色

    [题解]P3939 数颜色 不要数据结构和模板学傻了... 考虑到兔子们交换都是相邻的,说明任何一次交换只会引起\(O(1)\)的变化. 我们开很多\(vector\)存没种兔子的下标就好了.到时候二 ...

  8. Luogu2839 Middle 主席树、二分答案

    题目传送门:https://www.luogu.org/problemnew/show/P2839 题目大意:给出一个长度为$N$的序列与$Q$次询问,每次询问左端点在$[a,b]$,右端点在$[c, ...

  9. 【BZOJ2653】middle(主席树,二分)

    题意:一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[ ...

随机推荐

  1. BZOJ 1030 文本生成器 | 在AC自动机上跑DP

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1030 题解: 鸽 #include<cstdio> #include<al ...

  2. fs.watch 爬坑

    上星期用 fs.watch 和 readline.createInterface 对pm2的合并日志做了监控,根据指定的错误信息重启服务 发现不管是手动vim编辑日志,还是等待日志自动输出. fs.w ...

  3. iOS 单元测试(Unit Test 和 UI Test)

    之前一直搞过~~最近试了一下下,完美~~ 附上一篇文章,不同的伙伴可以看看: http://www.jianshu.com/p/009844a0b9edUnitTest(简单的单元测试使用) http ...

  4. 关于CRC循环冗余校验的总结(C#)

    1. 实验要求 (1)通过CRC(循环冗余校对)序列的计算,掌握C#语言中类的静态方法与动态方法的区别. (2)Shell与Windows Form 的通信作为扩展提高内容. 2. 实验内容  主要工 ...

  5. codechef T4 IPC Trainers

    IPCTRAIN: 训练营教练题目描述 本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日 程安排有 M 天,每天最多上一节课.第 i 名教练 ...

  6. [POJ1423]Stirling公式的应用

    Stirling公式: n!约等于sqrt(2*pi*n)*(n/e)^n 另外,e约等于2.71828182845409523... 试了一下发现math库里面并不能像pi一样直接调e但是发现挺好记 ...

  7. 密码框JPasswordField 的使用

    JPasswordField的主要方法为setEchoChar(char c),其中的字符C为回显字符. package first; import javax.swing.*; import jav ...

  8. TLS回调函数

    @author: dlive TLS (Thread Local Storage 线程局部存储 )回调函数常用于反调试. TLS回调函数的调用运行要先于EP代码执行,该特性使它可以作为一种反调试技术使 ...

  9. redis配置错误导致服务器不能启动

    redis-server忘了把配置里面的daemonize的no改成yes所以把redis-server加入到了/etc/rc.local里面,在服务器启动的时候就会阻塞在这里,导致服务器不能启动.[ ...

  10. 让你的软件飞起来:RGB转为YUV【转】

    转自:http://blog.csdn.net/wxzking/article/details/5905195 版权声明:本文为博主原创文章,未经博主允许不得转载. 朋友曾经给我推荐了一个有关代码优化 ...