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

# 题意

有n个事物会出现在河流中,每个事物会出现于le到ri秒。问第i秒第K小是多少。保证le和i在出现顺序中是递增的。

# 思路

先离散化,然后开一个树状数组,出现了的事物就插入权值树状数组,并记下消失的时间ri,等查询操作前把消失的事物清除。 
#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的更多相关文章

  1. 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 ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

随机推荐

  1. vue3.0中的双向数据绑定方法

    熟悉vue的人都知道在vue2.x之前都是使用object.defineProperty来实现双向数据绑定的 而在vue3.0中这个方法被取代了 1. 为什么要替换Object.definePrope ...

  2. DesignPattern系列__01SingletonResponsibility

    单一职责原则 单一职责原则:一个类应该只有一个原因引起改变,即一个类应该只负责一个业务逻辑. 问题由来:类T负责t1, t2两个职责,当因为t1j对类T修改的时候,可能导致类T出现问题而影响职责t2. ...

  3. CMD开放3389端口

    REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t R ...

  4. Java 性能优化(一)

    Java 性能调优(一) 1.衡量程序性能的标准 (1) 程序响应速度: (2) 内存占有情况: 2.程序调优措施 (1) 设计调优 设计调优处于所有调优手段 的上层,需要在软件开发之前进行.在软件开 ...

  5. LeetCode :1.两数之和 解题报告及算法优化思路

    最近开始重拾算法,在 LeetCode上刷题.顺便也记录下解题报告以及优化思路. 题目链接:1.两数之和 题意 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 ...

  6. pytest

    pytest可以生成多种样式的结果:1.生成JunitXML格式测试报告:命令: --junitxml=path(相对路径)2.生成result log 格式的测试报告: 命令:--resultlog ...

  7. Unity的弱联网Json数据传输

    注意事项: 关于dictionary转json的工程中遇到一点问题:要手动添加双引号. 关于json转dictionary:同样需要手动去掉双引号,否则添加到dictionary中的字符串会带有双引号 ...

  8. Selenium+Java - 结合sikuliX操作Flash网页

    前言 前天被一个Flash的轮播图,给玩坏了,无法操作,后来请教了下crazy总拿到思路,今天实践了下,果然可以了,非常感谢! 模拟场景 打开百度地图 切换城市到北京 使用测距工具 测量 奥林匹克森林 ...

  9. 重学计算机组成原理(十)- "烫烫烫"乱码的由来

    程序 = 算法 + 数据结构 对应到计算机的组成原理(硬件层面) 算法 --- 各种计算机指令 数据结构 --- 二进制数据 计算机用0/1组成的二进制,来表示所有信息 程序指令用到的机器码,是使用二 ...

  10. github的详细使用,非常简单!

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/