F - New Year and Cleaning

这题简直是丧心病狂折磨王。。

思路:容易想到这样一个转换,把整个矩形一起移动,矩形移出去的时候相当于一行或者一列。

为了优化找到下一个消去的点,我先把原数组扩大两倍,用了st表加二分去找,然后就MLE, 我又换了

线段树TLE,最后不把数组扩大两倍ST表+二分过的。。

每次消去的点都是不变的,所以可以做到线性复杂度。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 5e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int k, n, m, X[N], Y[N], Log[N];
char s[N]; struct ST {
int dp[N][],ty;
void build(int n, int b[], int _ty) {
ty = _ty;
for(int i = ; i <= n; i++) dp[i][] = ty * b[i];
for(int j = ; j <= Log[n]; j++)
for(int i = ; i+(<<j)- <= n; i++)
dp[i][j] = max(dp[i][j-], dp[i+(<<(j-))][j-]);
}
int query(int x, int y) {
int k = Log[y - x + ];
return ty * max(dp[x][k], dp[y-(<<k)+][k]);
}
} mxx, mnx, mxy, mny; void walk(int &x, int &y, char c) {
if(c == 'R') y++;
else if(c == 'L') y--;
else if(c == 'U') x--;
else x++;
} bool check(int l, int r, int x, int y, int lenr, int lenc) {
int mxX = mxx.query(l, r) - X[l-];
int mnX = mnx.query(l, r) - X[l-];
int mxY = mxy.query(l, r) - Y[l-];
int mnY = mny.query(l, r) - Y[l-];
if(x+mxX+lenr > n || x+mnX < || y+mxY+lenc > m || y+mnY < ) return true;
return false;
} int main() {
for(int i = -(Log[]=-); i < N; i++)
Log[i] = Log[i - ] + ((i & (i - )) == ); scanf("%d%d%d%s", &k, &n, &m, s + );
int x = , y = , L = , R = , D = , U = , p = ;
for(int i = ; i <= k; i++) {
walk(x, y, s[i]);
L = min(L, y); R = max(R, y);
U = min(U, x); D = max(D, x);
}
if(!x && !y) {
if(R - L + <= m && D - U + <= n) {
puts("-1");
return ;
}
}
x = , y = ;
for(int i = ; i <= k; i++) {
walk(x, y, s[i]);
X[i] = x; Y[i] = y;
} mxx.build(k, X, );
mnx.build(k, X, -);
mxy.build(k, Y, );
mny.build(k, Y, -); x = , y = ;
int lenr = n, lenc = m;
LL ans = , t = ;
while(lenr && lenc) {
int l = p + , r = min(k, p + k), pos = -;
while(l <= r) {
int mid = l + r >> ;
if(check(p + , mid, x, y, lenr, lenc)) r = mid - , pos = mid;
else l = mid + ;
} if(pos != -) {
x += X[pos] - X[p];
y += Y[pos] - Y[p];
t = (t + pos - p) % mod;
p = pos;
if(s[p] == 'U') {
lenr--; ans = (ans + 1ll*t*lenc) % mod;
x = ;
} else if(s[p] == 'D') {
lenr--; ans = (ans + 1ll*t*lenc) % mod;
} else if(s[p] == 'L') {
lenc--; ans = (ans + 1ll*t*lenr) % mod;
y = ;
} else {
lenc--; ans = (ans + 1ll*t*lenr) % mod;
}
if(p == k) p = ;
} else {
x += X[k] - X[p];
y += Y[k] - Y[p];
t = (t + k - p) % mod;
p = ;
} }
printf("%lld\n", ans);
return ;
} /*
*/

Good Bye 2015 F - New Year and Cleaning的更多相关文章

  1. Codeforces Round #597 (Div. 2) F. Daniel and Spring Cleaning 数位dp

    F. Daniel and Spring Cleaning While doing some spring cleaning, Daniel found an old calculator that ...

  2. Good Bye 2015 D. New Year and Ancient Prophecy

    D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...

  3. Good Bye 2015 B. New Year and Old Property 计数问题

    B. New Year and Old Property   The year 2015 is almost over. Limak is a little polar bear. He has re ...

  4. Good Bye 2015 A. New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...

  5. Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP

    B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...

  6. Codeforces Good Bye 2015 A. New Year and Days 水题

    A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...

  7. 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...

  8. Looksery Cup 2015 F - Yura and Developers 单调栈+启发式合并

    F - Yura and Developers 第一次知道单调栈搞出来的区间也能启发式合并... 你把它想想成一个树的形式, 可以发现确实可以启发式合并. #include<bits/stdc+ ...

  9. Good Bye 2014 F - New Year Shopping

    F - New Year Shopping 对于一种特殊的不可逆的dp的拆分方法.. 也可以用分治写哒. #include<bits/stdc++.h> #define LL long l ...

随机推荐

  1. socketpair + signal + select 的套路

    1:起因 最近在看代码时连续两次看到这三个函数的组合使用,为方便以后借鉴和回忆,先记录下来. 这三个函数的应用场景是这样的: 1.1 首先socketpair函数创建一对已连接套接字,返回的两个描述符 ...

  2. 问题分析 - 电容的ESR

    ESR,是Equivalent Series Resistance三个单词的缩写,翻译过来就是“等效串连电阻” 理论上,一个完美的电容,自身不会产生任何能量损失,但是实际上,因为制造电容的材料有电阻, ...

  3. 前端PHP入门-005-爱情是常量还是变量

    常量 常--汉语字面为:长久,经久不变. 常量那就好翻译了:长久不变的值. 常量的使用范围非常广泛. 我们在以后,定义我们的工作目录.定义一些特点的帐户密码.版本号等我们都会使用到常量.所以这一块的知 ...

  4. git操作图

  5. Windows 安装 RabbitMQ

    RabbitMQ概述 RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.Ra ...

  6. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  7. MySQl学习-——Mysql体系结构与Mysql存储引擎

    Mysql体系结构与Mysql存储引擎 Mysql体系结构 mysql体系结构图:

  8. 在asp.net 中生成PDF的方法

    近期要用asp.net 2.0生成PDF,看了下书,查了下资料,发现可以有组件帮得上忙,可以下载itextsharp(https://sourceforge.net/projects/itextsha ...

  9. 【CodeForces】960 F. Pathwalks 主席树+动态规划

    [题目]F. Pathwalks [题意]给定n个点m条边的有向图,可能不连通有重边有自环.每条边有编号 i 和边权 wi ,求最长的路径(可以经过重复节点)满足编号和边权都严格递增.n,m,wi&l ...

  10. 【CodeForces】679 B. Bear and Tower of Cubes

    [题目]B. Bear and Tower of Cubes [题意]有若干积木体积为1^3,2^3,...k^3,对于一个总体积X要求每次贪心地取<=X的最大积木拼上去(每个只能取一次)最后总 ...