Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4070 Accepted Submission(s): 1526
Problem Description
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.
There are three different events described in different format shown below:
D x: The x-th village was destroyed.
Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.
There are three different events described in different format shown below:
D x: The x-th village was destroyed.
Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.
Output
Output the answer to each of the Army commanders’ request in order on a separate line.
Sample Input
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
Sample Output
1
0
2
4
题意:
给出 n,m(1 ~ 50000),代表有 n 个连续的城市 和 m 个操作,操作有 3 类,D ans,代表要把编号为 ans 的城市毁灭,R 代表要重建最后毁灭的那么 城市,Q ans 代表询问包含 ans 的完好的连续城市有几个。每次 Q 给出一个结果。
思路:
线段树,区间合并。注意 updata 的时候要判断往左还是往右,如果不判断的话,则会 TLE。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <stack> using namespace std; const int MAX = 500050; int lmax[MAX], rmax[MAX], mmax[MAX]; void push_up(int node, int l, int r) { lmax[node] = lmax[node << 1]; rmax[node] = rmax[node << 1 | 1]; int mid = (r + l) >> 1; if (lmax[node] == mid - l + 1) lmax[node] += lmax[node << 1 | 1]; if (rmax[node] == r - mid) rmax[node] += rmax[node << 1]; mmax[node] = max(rmax[node << 1] + lmax[node << 1 | 1], max(mmax[node << 1], mmax[node << 1 | 1])); } void build (int node, int l, int r) { if (l == r) { lmax[node] = rmax[node] = mmax[node] = 1; } else { int mid = (r + l) >> 1; build(node << 1, l, mid); build(node << 1 | 1, mid + 1, r); push_up(node, l, r); } } void update(int node, int l, int r, int loc, int s) { if (l == r) { if (l == loc) { mmax[node] = lmax[node] = rmax[node] = (s ? 0 : 1); } return; } int mid = (r + l) >> 1; if (loc <= mid) update(node << 1, l, mid, loc, s); else update(node << 1 | 1, mid + 1, r, loc, s); push_up(node, l, r); } int query (int node, int l, int r, int loc) { int mid = (r + l) >> 1; if (mmax[node] == r - l + 1 || !mmax[node]) return mmax[node]; if (loc <= mid) { if (loc >= mid - rmax[node << 1] + 1) return query(node << 1, l, mid, loc) + query(node << 1 | 1, mid + 1, r, mid + 1); else return query(node << 1, l, mid, loc); } else { if (loc <= mid + 1 + lmax[node << 1 | 1] - 1) return query(node << 1, l, mid, mid) + query(node << 1 | 1, mid + 1, r, loc); else return query(node << 1 | 1, mid + 1, r, loc); } } int main() { int n, m; while(~scanf("%d%d", &n, &m)) { stack<int> s; build (1, 1, n); while (m--) { char op; scanf(" %c", &op); if (op == 'D') { int loc; scanf("%d", &loc); update(1, 1, n, loc, 1); s.push(loc); } else if (op == 'Q') { int loc; scanf("%d", &loc); printf("%d\n", query(1, 1, n, loc)); } else { int loc = s.top(); s.pop(); update(1, 1, n, loc, 0); } } } return 0; }
相关推荐
一个简单的并行示例# include < tunnel># include < iostream># include < thread>int main () { // instanciate a new tunnel auto comm = tunnel::make(); // get the channel auto chan = std::move (std::get...
http-tunnel http-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnelhttp-tunnel
IPv6 tunnel broker服務
Tunnelblick_3.8.5.dmg for mac应用文件,下载即可使用
Carpal Tunnel
内附;TUNNEL隧道实验拓扑图,实验步骤和解释,还有实验文档,PDF文档。
httptunnel下载,httptunnel下载地址,httptunnel下载说明
淘宝开源timetunnel入门文档
开源的Httptunnel,版本3.0.5,稳定版
Http-tunnel突破单位网管封杀QQ、MSN端口的方法 ,非常好用,
CISCO官方配置手册GRE Tunnel
BPDU Tunnel技术介绍 华三 就这些了 还写什么?
QinQ-BPDU TUNNEL操作
tunnel 最简单,最安全而且快速的方式:利用SSH + CONNECT
Introduction to Tunnel FET(整理的摘要)
怎样用putty设置SSH tunnel 凡是不晓得SSH为何物的朋友可以略过,也可以google SSH开始了解它。这儿不重复了。
Tunnel Hunter: Detecting application-layer tunnels with statistical fingerprinting
链接:https://www.tunnelblick.net/downloads.html
阿里arthas-tunnel-server-3.7.1-fatjar包
this is a free tunnel software, very portable, fast and eazy to use, please search the web for how to install, configure it.