线段树-区间最大字串(正解)
https://blog.csdn.net/u013480600/article/details/22509467
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1540
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14427 Accepted Submission(s): 5713
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!
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.
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
Source
Recommend
LL
正解:暴力模拟
题意:
D代表破坏村庄,
R代表修复最后被破坏的那个村庄,
Q代表询问包括x在内的最大连续区间是多少
直接上模拟,见代码
This is the code
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
int ret=0, flag=0;
char ch;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
ret = ch - '0';
while((ch=getchar())>='0'&&ch<='9')
ret=ret*10+(ch-'0');
return flag ? -ret : ret;
}
int main()
{
//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
int n,m;
while(~scanf("%d%d",&n,&m))
{
int per;
set<int > S;
S.insert(0);//查找时使用
S.insert(n+1);
stack<int> ST;
for(int i=0; i<m; i++)
{
int x;
char str;
scanf(" %c",&str);
if(str=='D')
{
scanf("%d",&x);
ST.push(x);
S.insert(x);
}
else if(str=='Q')
{
scanf("%d",&x);
if(S.find(x)!=S.end())//表示被破坏
puts("0");
else
{
set<int>::iterator it1= S.upper_bound(x);//第一个大于的,当前村庄右边那个被破坏的
set<int>::iterator it2=it1;
it2--;//小于的位置,因为当前村庄没有被破坏,所以减一就是被破坏的左边的那个
cout<<(*it1-*it2-1)<<endl;
}
}
else if(!ST.empty())
{
S.erase(ST.top());//删除最后一个被破坏的
ST.pop();
}
}
}
}