B2. Shave Beaver!
 

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!

There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, thatai1 = xai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n.

If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y](x ≤ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y.

All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types:

  • what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive?
  • two beavers on positions x and y (the beavers ax and ay) swapped.

You can assume that any beaver can be shaved any number of times.

Input

The first line contains integer n — the total number of beavers, 2 ≤ n. The second line contains n space-separated integers — the initial beaver permutation.

The third line contains integer q — the number of queries, 1 ≤ q ≤ 105. The next q lines contain the queries. Each query i looks as pi xiyi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 ≤ xi < yi ≤ n.

  • to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem B1);
  • to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems B1+B2).

Note that the number of queries q is limited 1 ≤ q ≤ 105 in both subproblem B1 and subproblem B2.

Output

For each query with pi = 1, print the minimum number of Beavershave 5000 sessions.

Examples
input
5
1 3 4 2 5
6
1 1 5
1 3 4
2 2 3
1 1 5
2 1 5
1 1 5
output
2
1
3
5

 题意:

  给你长度n的序列,m次询问

  1:x -> y 的花费  满足 每次 选择 以一个a值  能到大其右边任意位置 (即最长连续上升子序列)算一次路径,问从x值到达y值,需要几次

  2:x,y  交换a[x],a[y];

题解:

  假设x+1在 x的右边 那么此x的位置值为 1,即任意的区间求和

  有交换操作,线段树维护a[x],a[y]对序列的影响即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 3e5+, M = 2e5++, mod = 1e9+, inf = 0x3fffffff; int id[N],a[N],n,m,v[N*];
void update(int i,int ll,int rr,int x,int c) {
if(ll == rr) {
v[i] = c;
return ;
}
if(x <= mid) update(ls,ll,mid,x,c);
else update(rs,mid+,rr,x,c);
v[i] = v[ls] + v[rs];
}
int ask(int i,int ll,int rr,int x,int y) {
if(ll == x && y == rr) {
return v[i];
}
if(y <= mid) return ask(ls,ll,mid,x,y);
else if(x > mid) return ask(rs,mid+,rr,x,y);
else return ask(ls,ll,mid,x,mid) + ask(rs,mid+,rr,mid+,y);
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]),id[a[i]] = i;
for(int i = ; i < n; ++i) {
if(id[i] > id[i+]) update(,,n,i,);
}
scanf("%d",&m);
for(int i = ; i <= m; ++i) {
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op == ) {
printf("%d\n",ask(,,n,x,y-) + );
} else {
int tmp1 = a[x];
int tmp2 = a[y];
int tt = id[a[x]];
id[a[x]] = id[a[y]];
id[a[y]] = tt;
swap(a[x],a[y]);
if(tmp1+ <= n && id[tmp1] > id[tmp1+]) update(,,n,tmp1,);
if(tmp1- >= && id[tmp1-] < id[tmp1]) update(,,n,tmp1-,); if(tmp2+ <= n && id[tmp2] < id[tmp2+]) update(,,n,tmp2,);
if(tmp2- >= && id[tmp2-] > id[tmp2]) update(,,n,tmp2-,);
}
}
return ;
}

  

codeforce ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver! 线段树的更多相关文章

  1. ABBYY Cup 3.0 - Finals (online version)

    A 开个数组记录一下 #include <iostream> #include<cstdio> #include<cstring> #include<algo ...

  2. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  3. VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树

    题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...

  4. C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)

    题目链接:http://codeforces.com/contest/799/problem/C 题目: 题意: 给你n种喷泉的价格和漂亮值,这n种喷泉题目指定用钻石或现金支付(分别用D和C表示),C ...

  5. Codeforces VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线线段树 求区间相同数的最小距离

    D. Closest Equals Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/prob ...

  6. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  7. composer install 遇到问题 Problem 1 - phpunit/phpunit 5.7.5 requires php ^5.6 || ^7.0 -> your PHP version (5.5.3 0) does not satisfy that requirement.

    $ composer install Loading composer repositories with package information Updating dependencies (inc ...

  8. Pycharm 中You are using pip version 10.0.1, however version 18.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

    今天运行程序的时候出现了: You are using pip version 10.0.1, however version 18.1 is available.You should conside ...

  9. pip install psutil出错-You are using pip version 10.0.1, however version 18.0 is available.

    今天想用python代替shell做运维相关的事,写代码都是在本机,调试在服务器上 C:\Users\0>pip install psutilRequirement already satisf ...

随机推荐

  1. Java中 final static super this instanceof 关键字用法

    一.final关键字 final可以修饰变量.方法及类: 1.当定义一个final变量时,jvm会将其分配到常量池中,其所修饰的对象只能赋值一次,对基本类型来说是其值不可变,引用类型(包括作为函数形参 ...

  2. Ubuntu下安装eclipse

    一.eclipse安装环境JDK的安装 1.下载JDK 从官网下载jdk8 jdk-8u5-linux-x64.tar.gz 2.解压$ tar -zxvf jdk-8u5-linux-x64.tar ...

  3. Appium 客户端库 API

    ## Appium 客户端库 Appium 有对应以下语言的客户端库: 语言 | 代码 :--|--:[Ruby][rubygems] | [GitHub](https://github.com/ap ...

  4. Java for LeetCode 208 Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  5. SAP 透明表之间的关联字段

    VTTK-TPLST=TTDST-TPLST(装运点的关联表及描述表字段TTDST-BEZEI) VTTK-ROUTE=TVROT-ROUTE(装运线路关联表及描述表字段TVROT-BEZEI) VT ...

  6. 【leetcode】Largest Number ★

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. 【python】入门学习(十)

    #入门学习系列的内容均是在学习<Python编程入门(第3版)>时的学习笔记 统计一个文本文档的信息,并输出出现频率最高的10个单词 #text.py #保留的字符 keep = {'a' ...

  8. python学习 登陆验证

    #!/usr/bin/env python #-*- coding=utf-8 -*- #----------------导入模块------------------------------ impo ...

  9. 躲避大龙(codevs 1961)

    题目描述 Description 你早上起来,慢悠悠地来到学校门口,发现已经是八点整了!(这句话里有一个比较重要的条件) 学校共有N个地点,编号为1~N,其中1号为学校门口(也就是你现在所处的位置), ...

  10. fork与vfork的区别与联系

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 2. fo ...