Shave Beaver! CodeForces - 331B2 (线段树)
题面
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 (线段树)相关推荐
- CodeForces ABBYY Cup 3.0 - Finals B Shave Beaver! (思维+线段树)
题目链接 题意:给你n个数1~n的序列,随后又q次询问,每次询问有2种操作,操作1:查询从序列中取出数值为x,x+1,...,y-1,y的数的最少需要取多少次(且每取一次只能是一个递增的子序列) ...
- CodeForces - 786BLegacy——线段树建图+最短路
[题目描述] CodeForces - 786BLegacy [题目分析] 题目大概意思就是有三种操作: 从某个点到另一个点 从某个点到另一个区间 从某个区间到另一个点 然后询问从其中一个点到其他所有 ...
- Sum of Medians CodeForces - 85D(线段树+离散化)
Sum of Medians 题目链接:CodeForces - 85D 题意:对于一个集合set(有序的)有三个操作(集合是有序的,下标由1开始): 一:add x:在集合中加入x; 二:del ...
- Codeforces 1140F 线段树 分治 并查集
题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...
- CodeForces 444C 线段树
想分块想了很久一点思路都没有,结果一看都是写的线段树= = ...完全忘记了还有线段树这种操作 题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差 线段树记得+lazy标记 ...
- Codeforces 438D 线段树 解题报告
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his hou ...
- Marvolo Gaunt's Ring CodeForces - 855B+线段树+维护区间最大值和最小值
题目链接: Marvolo Gaunt's Ring CodeForces - 855B 题目大意: 给定一段序列:a1,a2,a3,--an, 给定三个数:p,q,r(注意数据范围,代码里ans=- ...
- Letters Removing CodeForces - 899F (线段树维护序列)
大意: 给定字符串, 每次删除一段区间的某种字符, 最后输出序列. 类似于splay维护序列. 每次删除都会影响到后面字符的位置 可以通过转化为查询前缀和=k来查找下标. #include <i ...
- CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换,清晰易懂)
CodeForces - 1401 F Reverse and Swap(线段树, 区间翻转, 区间交换) 首先一共有四个操作,第一个和第四个都是线段树的基本操作,直接用线段树实现. 第 ...
最新文章
- python批量爬取文档
- 双模sa_2020年5G终端发展展望:NSA/SA双模终端将成市场“主力军”!
- mysql 插入汉字异常: Incorrect string value: '\xE8\xB0\xA2\xE9\x9D\x99' for column 'uname' at row 1...
- play2框架 jpa mysql_单元测试 – Playframework 2.2.x Java JPA – 用于单元测试和生产的独立数据库...
- dijkstra算法学习
- DUBBO与ZOOKEEPER、SPRINGMVC整合和使用
- 【Programming Clip】点分十进制IP和长整型转换
- 自定义控件使用InnerDefaultProperty提示“内不允许包含文字内容”
- 上传文件实时显示网速怎么实现_“双11”来了!三亚1487个5G基站带你拼网速_政务_澎湃新闻...
- 小白电赛备战(1)msp430 f5529数据手册(中英文)
- [WebSocket]使用WebSocket实现实时多人答题对战游戏
- 如何用java代码给Word文档添加水印?
- 【C语言】验证哥德巴赫猜想:任何一个大于2的偶数均可表示成为两个素数之和。
- C++ Programming Basic acknowledge
- RBP系统管理之用户审批
- 如何设计一枚「拟态」按钮
- 手把手教你升级Keil MDK的ARM编译器
- 机器学习—特征工程—OneHotEncoder独热编码
- 服务器接地位置,idc数据服务器机房搬迁防雷接地案例方案分析
- 提名卡斯卡迪亚的免费软件社区贡献者