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 ...
随机推荐
- Java匹马行天下之JavaWeb核心技术——JSP(续一)
十二.JSP表单处理 我们在浏览网页的时候,经常需要向服务器提交信息,并让后台程序处理.浏览器中使用 GET 和 POST 方法向服务器提交数据. GET 方法 GET方法将请求的编码信息添加在网 ...
- python多进程详解
目录 python多进程 序.multiprocessing 一.Process process介绍 例1.1:创建函数并将其作为单个进程 例1.2:创建函数并将其作为多个进程 例1.3:将进程定义为 ...
- Hadoop 系列(四)—— Hadoop 开发环境搭建
一.前置条件 Hadoop 的运行依赖 JDK,需要预先安装,安装步骤见: Linux 下 JDK 的安装 二.配置免密登录 Hadoop 组件之间需要基于 SSH 进行通讯. 2.1 配置映射 配置 ...
- 如何使用dmidecode命令查看硬件信息
引言 当我们需要获取机器硬件信息时,可使用linux系统自带的dmidecode工具进行查询. dmidecode命令通过读取系统DMI表,显示服务器硬件和BIOS信息.除了可使用dmidecode查 ...
- 变量Variable
变量Variable 内存 #conding:utf-8 a = 1 #conding:utf-8 a = 1 b = a #conding:utf-8 a = 1 b = a a = 2 命名规则 ...
- 简单认识Nginx---负载均衡
中大型项目都会考虑到分布式,前面几篇文章着重介绍了数据处理的技术集群.今天来研究一下关于服务器的负载均衡–Nginx.他除了静态资源的处理外还有可以决定将请求置于那台服务上. Nginx的安装 点我下 ...
- python小白手册之字符串的私有方法和公用方法
#字符串方法. name=input('1111') if name.isalnum(): print(是否由数字字母) isdigit isdecimal判断数字 strip去空格或者其他 name ...
- 在CentOS 7 / RHEL 7安装PostgreSQL 10
CentOS 到了7.x版本, PostgreSQL也来到了10.x版本. 前些天MySQL都直接跨到了8.0版本. 本文是一篇在CentOS 7.4上安装安装PostgreSQL 10.3 的教程. ...
- 【已解决】Https请求—未能创建 SSL/TLS 安全通道
在做项目的微信推送消息功能时,由于微信并发量大,导致其它第三方接口调用时直接挂掉报错. 问题: 测试工程师做压测,100个线程同时调用微信和XX站的接口,日志报XX站的“请求被中止: 未能创建 SSL ...
- EOS源码分析:transaction的一生
最近在处理智能合约的事务上链问题,发现其中仍旧有知识盲点.原有的认识是一个事务请求会从客户端设备打包签名,然后通过RPC传到非出块节点,广播给超级节点,校验打包到可逆区块,共识确认最后变为不可逆区块. ...