gym/102091
https://codeforces.com/gym/102091
2018-2019 ACM-ICPC, Asia Nakhon Pathom Regional Contest
A Flying Squirrel
# 题意
有n个柱子。m次询问。
每次询问从x号柱子跳到y号柱子,最多能踩几个柱子。
每次跳跃只能向低的柱子跳,且中间不能有高于起跳点的柱子。
# 思路
化数列为DAG,对于一个柱子u来说,向左跳到能跳的区域中最高的柱子v,我们连边u->v。然后就类似树上的操作。
注意dfs和bfs中都要做好打标记的操作。
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} const int inf = 0x3f3f3f3f; const int mod = 1e9+; /**********showtime************/
const int maxn = 1e5+;
int a[maxn];
vector<int>mp[maxn];
vector<int>hi[maxn]; int mx[maxn<<];
void build(int le, int ri, int rt){
if(le == ri) {
mx[rt] = a[le];
return;
}
int mid = (le + ri) >> ;
build(le, mid, rt<<);
build(mid+,ri,rt<<|);
mx[rt] = max(mx[rt<<], mx[rt<<|]);
}
int query(int L, int R, int le, int ri, int rt){
if(le >= L && ri <= R) {
return mx[rt];
}
int mid = (le + ri) >> ;
int res = ;
if(mid >= L) res = max(res, query(L, R, le, mid, rt<<));
if(mid < R) res = max(res, query(L, R, mid+, ri, rt<<|));
return res;
}
int low[maxn],up[maxn];
int st[maxn];
int top = ;
int dp[maxn], mdp[maxn];
int vis[maxn],used[maxn];
void dfs(int u, int o) {
vis[u] = true;
dp[u] = dp[o] + ;
mdp[u] = ;
for(int v : mp[u]) {
if(!vis[v]) dfs(v, u);
mdp[u] = max(mdp[u], mdp[v] + );
}
}
bool check(int x, int y) {
if(y <= up[x] && y >= low[x]) return true;
if(x <= up[y] && x >= low[y]) return true;
return false;
}
int main(){
int n,m;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) {
scanf("%d", &a[i]);
hi[a[i]].pb(i);
} for(int i=; i<=n; i++) {
while(top > && a[st[top]] < a[i]) top--;
if(top == ) low[i] = ;
else low[i] = st[top] + ;
st[++top] = i;
} top = ;
for(int i=n; i>=; i--) {
while(top > && a[st[top]] < a[i])top--;
if(top == ) up[i] = n;
else up[i] = st[top] - ;
st[++top] = i;
} build(, n, );
int big = query(, n, , n, ); int root = n + ;
queue<int>que;
for(int v : hi[big]) {
mp[root].pb(v);
que.push(v);
} while(!que.empty()) {
int u = que.front(); que.pop();
int le = low[u], ri = up[u];
if(le < u) {
int mx = query(le, u - , , n, );
int id = lower_bound(hi[mx].begin(), hi[mx].end(), le) - hi[mx].begin(); for(int i=id; i<hi[mx].size(); i++) {
if(hi[mx][i] >= u) break;
int v = hi[mx][i];
mp[u].pb(v);
if(used[v] == )
{
que.push(v);
used[v] = ;
}
}
}
if(u < ri) {
int mx = query(u+, ri, , n, );
int id = lower_bound(hi[mx].begin(), hi[mx].end(), u+) - hi[mx].begin();
for(int i=id; i<hi[mx].size(); i++) {
if(hi[mx][i] > ri) break;
int v = hi[mx][i];
mp[u].pb(v);
if(used[v] == ) {
used[v] = ;
que.push(v);
}
}
}
} dfs(root, root); for(int i=; i<=m; i++) {
int x,y;
scanf("%d%d", &x, &y);
if(y == ) {
printf("%d\n", mdp[x]);
}
else {
if(!check(x, y)) puts("");
else printf("%d\n", abs(dp[x] - dp[y]));
}
}
return ;
}
E How Many Groups
K The Stream of Corning 2
# 题意
# 思路
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
struct FastIO {
static const int S = 4e6;
int wpos;
char wbuf[S];
FastIO() : wpos() {}
inline int xchar() {
static char buf[S];
static int len = , pos = ;
if (pos == len)
pos = , len = fread(buf, , S, stdin);
if (pos == len) exit();
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = ;
while (c <= ) c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x;
}
inline int xint()
{
int s = , c = xchar(), x = ;
while (c <= ) c = xchar();
if (c == '-') s = -, c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x * s;
}
inline void xstring(char *s)
{
int c = xchar();
while (c <= ) c = xchar();
for (; c > ; c = xchar()) * s++ = c;
*s = ;
}
inline void wchar(int x)
{
if (wpos == S) fwrite(wbuf, , S, stdout), wpos = ;
wbuf[wpos++] = x;
}
inline void wint(int x)
{
if (x < ) wchar('-'), x = -x;
char s[];
int n = ;
while (x || !n) s[n++] = '' + x % , x /= ;
while (n--) wchar(s[n]);
wchar('\n');
}
inline void wstring(const char *s)
{
while (*s) wchar(*s++);
}
~FastIO()
{
if (wpos) fwrite(wbuf, , wpos, stdout), wpos = ;
}
} io;
inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e6+;
vector<int>v;
int getid(int x){
return lower_bound(v.begin(), v.end(), x) - v.begin() + ;
}
struct ask
{
int op;
int a,b,c;
}e[maxn];
int sum[maxn];
int lowbit(int x){
return x & (-x);
}
void add(int x,int c){
while(x < maxn){
sum[x] += c;
x = x + lowbit(x);
}
}
int cal(int x){
int res = ;
while(x > ){
res += sum[x];
x -= lowbit(x);
}
return res;
}
vector<int>er[maxn]; int main(){
int T,n; //scanf("%d", &T);
T = io.xint();
rep(cas, , T){
printf("Case %d:\n", cas);
// scanf("%d", &n);
n = io.xint();
v.clear();
for(int i=; i<maxn; i++) er[i].clear();
memset(sum, , sizeof(sum));
rep(i, , n) {
// scanf("%d", &e[i].op);
e[i].op = io.xint();
if(e[i].op == ) {
// scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
e[i].a = io.xint(); e[i].b = io.xint(); e[i].c = io.xint();
v.pb(e[i].a),v.pb(e[i].c);
}
else {
// scanf("%d%d", &e[i].a, &e[i].b);
e[i].a = io.xint(); e[i].b = io.xint();
v.pb(e[i].a);
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int la = ;
rep(i, , n){ if(e[i].op == ) {
add(e[i].b, );
int t = getid(e[i].c);
er[t+].pb(e[i].b);
}
else {
for(int k=la; k <= getid(e[i].a); k++)
for(int j=; j<er[k].size(); j++){
add(er[k][j], -);
}
la = getid(e[i].a)+;
int res = -, le = , ri = maxn-; while(le <= ri){
int mid = (le + ri) >> ;
if(cal(mid) >= e[i].b) {res = mid, ri = mid - ;}
else le = mid + ;
}
if(res == -) puts("-1");
else printf("%d\n", res);
}
} } return ;
}
gym/102091的更多相关文章
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
随机推荐
- 【Android】java.lang.StackOverflowError: stack size 8MB
最近遇到的问题,报了两个错误,如下: java.lang.StackOverflowError: stack size 8MB android.os.TransactionTooLargeExcept ...
- 【iOS】UIButton 常用属性
发现 UIButton 的相关属性不熟悉了……常用的一些属性代码如下: UIButton *add = [UIButton buttonWithType:UIButtonTypeCustom]; ad ...
- Mac OS 安装mysqlclient 遇到的坑~
最近在学习Python, 因为Django连接mysql 需要安装mysqlclient, 但Mac安装遇到各种问题,这里记录一下,避免以后再踩坑. 1. 正常情况下,安装mysqlclient ...
- 单纯的xlistview
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private ...
- Docker 前沿概述
目录 Docker 前沿概述 什么是Docker? Docker的基本概念 容器(Container) -- 镜像运行时的实体 镜像(Image) -- 一个特殊的文件系统 仓库(Repository ...
- 004——Netty之高性能IO(Reactor)
一.原始方式 方法一: # 使用while循环,不断监听端口是否有新的套接字链接 while(true){ socket = accept(); handle(socket) } # 做法局限:处理效 ...
- 「雕爷学编程」Arduino动手做(10)——敲击传感器模块
37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...
- Socket通信封装MIna框架--含羞代放
目录 核心类 各个击破 IoService IoFilter IoHandler 总结 # 加入战队 微信公众号 Mina异步IO使用的Java底层JNI框架,Mina提供服务端和客户端,将我们的业务 ...
- 《Java 8 in Action》Chapter 2:通过行为参数化传递代码
你将了解行为参数化,这是Java 8非常依赖的一种软件开发模式,也是引入 Lambda表达式的主要原因.行为参数化就是可以帮助你处理频繁变更的需求的一种软件开发模式.一言以蔽之,它意味 着拿出一个代码 ...
- SpringBoot中关于Shiro权限管理的整合使用
转载:https://blog.csdn.net/fuweilian1/article/details/80309192 在整合Shiro的时候,我们先要确定一下我们的步骤: 1.加入Shiro的依 ...