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. K8s仪表盘

    { "__inputs": [ { "name": "DS_TEST-ENVIORMENT-K8S", "label": ...

  2. discuz安装小云app

    小云app登录注册: https://oauth.apps.xiaoyun.com/auth/login.do?authentication_error=1 from:http://lusongson ...

  3. jquery读取html5的data-属性

    前端代码的工作无非就是接收后端发来的数据,展示到前端页面:又或者,给无数的按钮,图片,段落等绑定各种事件.那么我们在绑定事件是需要拿取HTML页面的元素,以及在拿取的元素给定各式各样的自定义属性.当需 ...

  4. 获取Session和request方法

    action中的几种写法 //第一种很少用public class LoginAction1 extends ActionSupport {        private Map request;   ...

  5. struts2验证规则validation配置文件命名方式总结

    1.Action级别校验命名格式: ActionClassName-validation.xml 2.Action中某个方法的校验命名格式: ActionClassName-ActionAliasNa ...

  6. 51nod 1140 矩阵相乘结果的判断

    给出三个N*N的矩阵A, B, C,问A * B是否等于C?   Input 第1行,1个数N.(0 <= N <= 500) 第2 - N + 1行:每行N个数,对应矩阵A的元素.(0  ...

  7. 【CODEVS】1022 覆盖

    [算法]二分图匹配(最大流) [题解]对i+j进行奇偶染色,就可以保证相邻两格异色. 然后就是二分图了,对相邻格子连边跑最大流即可. #include<cstdio> #include&l ...

  8. Oozie与Coordinator调度讲解及系统时区配置与定时触发两种配置方式

    1:修改本地linux时区 查看时区 - 号代表西  + 号 代表东 北京时间是东八区 设置时区的配置文件所在位置 cd /usr/share/zoneinfo/ 选择以亚洲的上海 的时区为基址 删除 ...

  9. 在Unity中实现屏幕空间反射Screen Space Reflection(4)

    第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...

  10. AES,SHA1,DES,RSA,MD5区别

    AES:更快,兼容设备,安全级别高: SHA1:公钥后处理回传 DES:本地数据,安全级别低 RSA:非对称加密,有公钥和私钥 MD5:防篡改 相关: 公开密钥加密(英语:public-key cry ...