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. intellij 插件结构(文件结构以及概念层面上的结构)

    1.插件内的文件 2.插件类加载器 3.插件组件(component) 4.插件的扩展以及扩展点(Extensions.Extension Points) 5.插件的Action 6.插件的Servi ...

  2. C++并发编程 等待与唤醒

    C++并发编程 等待与唤醒 条件变量 条件变量, 包括(std::condition_variable 和 std::condition_variable_any) 定义在 condition_var ...

  3. 关于HttpURLConnection/HttpsURLConnection请求出现了io.filenotfoundexception:url的解决方法

    //从输入流读取返回内容InputStream is = null;int status = connection.getResponseCode();if(status>= HttpStatu ...

  4. 怎样安装Command Line Tools in OS x Mavericks&Yosemite(Without xcode)--转载

    How to Install Command Line Tools in OS X Mavericks & Yosemite (Without Xcode) Mac users who pre ...

  5. JS中encodeURI、encodeURIComponent、decodeURI、decodeURIComponent

    js 对文字进行编码涉及2个函数:encodeURI,encodeURIComponent,相应2个解码函数:decodeURI,decodeURIComponent 1.用来编码和解码URI的 统一 ...

  6. 计算方法 -- 解线性方程组直接法(LU分解、列主元高斯消元、追赶法)

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstdlib> ...

  7. 维护后面的position sg函数概念,离线+线段 bzoj 3339

    3339: Rmq Problem Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1160  Solved: 596[Submit][Status][ ...

  8. 最常用的8款 PHP 调试工具,你用过吗?

    Web 开发并不是一项轻松的任务,有超级多服务端脚本语言提供给开发者,但是当前 PHP 因为具有额外的一些强大的功能而越来越流行.PHP 是最强大的服务端脚本语言之一,同时也是 Web 开发者和设计者 ...

  9. JQuery的几个基础操作

    先介绍两个函数(数组) 1.$.map(array,function(element,index)); 对于数组array中的每个元素,调用上面所示的function(element,index)函数 ...

  10. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

    A rooted tree is a well-known data structure in computer science and engineering. An example is show ...