题面
The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily!
题意
在一个数组里面,取出值从a到b的序列,但是每次取出数的必须是连续的.
求每次最少取多少次.
询问中存在交换操作
思路
线段数维护区间和.
点\(x\)表示\(x+1\)是否在\(x\)后面.

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>#define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 300086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);int num[maxn], pos[maxn];int bit[maxn];int lowbit(int x) {return x & -x;
}int query(int pos) {int ans = 0;while (pos) {ans += bit[pos];pos -= lowbit(pos);}return ans;
}void update(int pos, int val) {while (pos < maxn) {bit[pos] += val;pos += lowbit(pos);}
}int main() {ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &num[i]);pos[num[i]] = i;}for (int i = 1; i < n; i++) {if (pos[i] > pos[i + 1]) {update(i, 1);}}int m;scanf("%d", &m);while (m--) {int type;scanf("%d", &type);if (type == 1) {int l, r;scanf("%d%d", &l, &r);printf("%d\n", query(r-1) - query(l - 1) + 1);} else {int x,y;scanf("%d%d",&x,&y);swap(num[x],num[y]);x=num[x];y=num[y];int t1 = pos[x];int t2 = pos[y];if(t1>t2){swap(x,y);swap(t1,t2);}if(pos[x+1]>=pos[x]&&pos[x+1]<=pos[y]){update(x,1);}if(x>1&&x-1!=y&&pos[x-1]>=pos[x]&&pos[x-1]<=pos[y]){update(x-1,-1);}if(pos[y+1]>=pos[x]&&pos[y+1]<=pos[y]){update(y,-1);}if(y>1&&y-1!=x&&pos[y-1]>=pos[x]&&pos[y-1]<=pos[y]){update(y-1,1);}swap(pos[x],pos[y]);}}return 0;
}

转载于:https://www.cnblogs.com/ZGQblogs/p/11514748.html

Shave Beaver! CodeForces - 331B2 (线段树)相关推荐

  1. CodeForces ABBYY Cup 3.0 - Finals B Shave Beaver! (思维+线段树)

    题目链接 题意:给你n个数1~n的序列,随后又q次询问,每次询问有2种操作,操作1:查询从序列中取出数值为x,x+1,...,y-1,y的数的最少需要取多少次(且每取一次只能是一个递增的子序列)    ...

  2. CodeForces - 786BLegacy——线段树建图+最短路

    [题目描述] CodeForces - 786BLegacy [题目分析] 题目大概意思就是有三种操作: 从某个点到另一个点 从某个点到另一个区间 从某个区间到另一个点 然后询问从其中一个点到其他所有 ...

  3. Sum of Medians CodeForces - 85D(线段树+离散化)

    Sum of Medians 题目链接:CodeForces - 85D 题意:对于一个集合set(有序的)有三个操作(集合是有序的,下标由1开始): 一:add x:在集合中加入x; 二:del  ...

  4. Codeforces 1140F 线段树 分治 并查集

    题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...

  5. CodeForces 444C 线段树

    想分块想了很久一点思路都没有,结果一看都是写的线段树= = ...完全忘记了还有线段树这种操作 题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差 线段树记得+lazy标记 ...

  6. Codeforces 438D 线段树 解题报告

    D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his hou ...

  7. Marvolo Gaunt's Ring CodeForces - 855B+线段树+维护区间最大值和最小值

    题目链接: Marvolo Gaunt's Ring CodeForces - 855B 题目大意: 给定一段序列:a1,a2,a3,--an, 给定三个数:p,q,r(注意数据范围,代码里ans=- ...

  8. Letters Removing CodeForces - 899F (线段树维护序列)

    大意: 给定字符串, 每次删除一段区间的某种字符, 最后输出序列. 类似于splay维护序列. 每次删除都会影响到后面字符的位置 可以通过转化为查询前缀和=k来查找下标. #include <i ...

  9. CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换,清晰易懂)

    CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换)   首先一共有四个操作,第一个和第四个都是线段树的基本操作,直接用线段树实现.      第 ...

最新文章

  1. python批量爬取文档
  2. 双模sa_2020年5G终端发展展望:NSA/SA双模终端将成市场“主力军”!
  3. mysql 插入汉字异常: Incorrect string value: '\xE8\xB0\xA2\xE9\x9D\x99' for column 'uname' at row 1...
  4. play2框架 jpa mysql_单元测试 – Playframework 2.2.x Java JPA – 用于单元测试和生产的独立数据库...
  5. dijkstra算法学习
  6. DUBBO与ZOOKEEPER、SPRINGMVC整合和使用
  7. 【Programming Clip】点分十进制IP和长整型转换
  8. 自定义控件使用InnerDefaultProperty提示“内不允许包含文字内容”
  9. 上传文件实时显示网速怎么实现_“双11”来了!三亚1487个5G基站带你拼网速_政务_澎湃新闻...
  10. 小白电赛备战(1)msp430 f5529数据手册(中英文)
  11. [WebSocket]使用WebSocket实现实时多人答题对战游戏
  12. 如何用java代码给Word文档添加水印?
  13. 【C语言】验证哥德巴赫猜想:任何一个大于2的偶数均可表示成为两个素数之和。
  14. C++ Programming Basic acknowledge
  15. RBP系统管理之用户审批
  16. 如何设计一枚「拟态」按钮
  17. 手把手教你升级Keil MDK的ARM编译器
  18. 机器学习—特征工程—OneHotEncoder独热编码
  19. 服务器接地位置,idc数据服务器机房搬迁防雷接地案例方案分析
  20. 提名卡斯卡迪亚的免费软件社区贡献者

热门文章

  1. Windows开机小键盘灯不亮不能用
  2. 【Git学习】解决GitLab内存消耗大的问题
  3. matlab批量修改图片的大小_MATLAB实现批量处理图像图片的两种方法
  4. 游戏进化之路,Metaverse 的特征及难题
  5. 冬日丹崖风貌--承德碧霞山
  6. 中划线与下划线的区别
  7. 互联网支付系统整体架构
  8. 微信小程序点击获取昵称头像
  9. 作业报告12 定期存款利息计算器
  10. DirectX12 - Triangle Culling and Winding Order(三角形的剔除与绕序)