codeforces 13E . Holes 分块
2016-03-16 14:05
239 查看
题目链接
nextt数组表示这个位置的下一个位置。
cnt数组表示这个位置 i 到nextt[i]可以弹几次。
end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里。
然后就看代码吧。
nextt数组表示这个位置的下一个位置。
cnt数组表示这个位置 i 到nextt[i]可以弹几次。
end[i] 表示在从 i 弹出去的情况下, 最后一个位置是哪里。
然后就看代码吧。
#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue> #include <stack> #include <bitset> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, n, a) for(int i = a; i<n; i++) #define fi first #define se second typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; const int maxn = 1e5 + 5; int a[maxn], cnt[maxn], nextt[maxn], pos[maxn], block[maxn], n; void update(int x, int y) { if(y >= n) { nextt[x] = n+1; cnt[x] = 1; pos[x] = x; } else { if(block[x] == block[y]) { nextt[x] = nextt[y]; cnt[x] = cnt[y] + 1; } else { nextt[x] = y; cnt[x] = 1; } pos[x] = pos[y]; } } void query(int x) { int i, ans = 0; for(i = x; ; i = nextt[i]) { ans += cnt[i]; if(nextt[i] >= n) { i = pos[i]; break; } } printf("%d %d\n", i+1, ans); } int main() { int m, sign, x, y; cin>>n>>m; int BLOCK = sqrt(n); for (int i = 0; i < n; i++) { scanf("%d", a + i); block[i] = i / BLOCK; } for (int i = n-1; i >= 0; i--) { update(i, i + a[i]); } while (m--) { scanf("%d", &sign); if(sign) { scanf("%d", &x); query(x-1); } else { scanf("%d%d", &x, &y); x--; a[x] = y; int l = block[x] * BLOCK; int r = l + BLOCK; r = min(n, r); for(int i = r-1; i >= l; i--) { update(i, i + a[i]); } } } return 0; }
相关文章推荐
- 将Python脚本打包成可执行文件
- JavaScript 踩坑心得— 为了高速(下)
- 基于栈虚拟机和基于寄存器虚拟机的比较
- Android布局善用tools工具
- Linux---centos6编译安装nginx1.8.1(附:安装脚本)
- Swift - 实现发送短信的功能
- 协同过滤
- 会话技术session
- jmeter函数助手之time函数实操
- 困扰一天的rpm打包问题
- 规约模式Specification Pattern
- StringBuilder和String的区别
- SYN591-C型 时间间隔表
- python超大数计算
- [Java基础] 断言(assert)
- 使用C++实现工厂模式
- 【MongoDB】在windows平台下mongodb的分片集群(五)
- 野心和实力的磨合
- java 中 size() 和 length()
- HDU 1423 最长公共字串+上升子序列