线段树以及数组数组类型各题总结-acm

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

线段树之hdu4027
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4014 Accepted Submission(s): 979
Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
Y ou are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
Input
The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. Y ou can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6
Source
/*一个10^5的序列,有10^5个操作,每个操作为a,b,c
a=0时将b到c区间内的数都开根号下取整,a=1时求b到c段的和
其中所有数的和不超过2^63。

可以发现所有的数最多开7次方,就会变成1了,再开方就不变了。

所以定一个标记allone表示这一段已经全是1了,以后的开房遇到allone为true就不向下进行了,提高效率。

线段树求和的变型,线段树提高效率的关键在于寻找合适的lazy标记,到满足一定条件的时候就不继续更新到点。

*注意在HDOJ里,64位整数,定义用__int64或longlong,输入输出只能用%I64d*/
#include<stdio.h>
#include<math.h>
int n;
struct haha
{
int flag;//有效防止超时
int left;
int right;
__int64 num;
}tree[100000*4];//至少要为4
__int64 a[100000+5];
void build(int node,int left,int right)
{
int mid;
tree[node].flag=0;
tree[node].left=left;
tree[node].right=right;
if(left==right)
{
tree[node].num=a[left];
if(a[left]==1) tree[node].flag=1;
return ;
}
mid=(left+right)/2;
build(node*2,left,mid);
build(node*2+1,mid+1,right);
tree[node].num=tree[node*2].num+tree[node*2+1].num;
if(tree[node].num==1) tree[node].flag=1;
}
void update(int node,int left,int right)
{
int mid;
if(tree[node].flag==1) return;
if(tree[node].left==tree[node].right)
{
tree[node].num=(__int64)sqrt((double)tree[node].num);
if(tree[node].num==1) tree[node].flag=1;
return;
}
mid=(tree[node].left+tree[node].right)/2;
if(right<=mid) update(node*2,left,right); //{tree[node].num=tree[node*2].num;}这句话千万不能加
else if(left>mid) update(node*2+1,left,right); //因为不管怎样当前节点的sum都是由其两子路的sum加出来的
else
if(left>=tree[node].left&&tree[node].right>=right)
{
update(node*2,left,mid);
update(node*2+1,mid+1,right);
}
else return ;
tree[node].num=tree[node*2+1].num+tree[node*2].num;
tree[node].flag=tree[node*2+1].flag&&tree[node*2].flag;//千万不要忘记及时更新标记符号}
__int64 queue(int node,int left,int right)
{
int mid;
if(tree[node].left==left&&tree[node].right==right)
return tree[node].num;
mid=(tree[node].left+tree[node].right)/2;
if(left>mid) return queue(node*2+1,left,right);
else if(right<=mid) return queue(node*2,left,right);
else if(left>=tree[node].left&&tree[node].right>=right)
{
return queue(node*2,left,mid)+queue(node*2+1,mid+1,right);
}
else return 0;
}
int main()
{
int i,cas=0,m,flag,left,right,temp;
while(scanf("%d",&n)!=EOF)
{
printf("Case #%d:\n",++cas);
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
for(i=1;i<=4*n;i++)
tree[i].flag=0;
build(1,1,n);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&flag,&left,&right);
if(left>right) {temp=left;left=right;right=temp;}//这个地方很不要脸很难发现而且没有就RW
if(!flag)
update(1,left,right);
else printf("%I64d\n",queue(1,left,right));
}
printf("\n");
}
return 0;
}
很霸气的线段树hdu3911 难死人啊
Black And White
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1811 Accepted Submission(s): 609
Problem Description
There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she want to know the longest period of consecutive black stones in a range [i, j].
Input
There are multiple cases, the first line of each case is an integer n(1<= n <= 10^5), followed by n integer 1 or 0(1 indicates black stone and 0 indicates white stone), then is an integer M(1<=M<=10^5) followed by M operations formatted as x i j(x = 0 or 1) , x=1 means change the color of stones in range[i,j], and x=0 means ask the longest period of consecutive black stones in range[i,j]
Output
When x=0 output a number means the longest length of black stones in range [i,j].
Sample Input
41 0 1 050 1 41 2 30 1 41 3 30 4 4
Sample Output
120
Source
#include<stdio.h>
struct haha
{
int left;
int right;
int l_b;//、lb表示区间从左边开始连续的1的个数如当前区间为1-10 那么这个就代表包含1的最长1串
int r_b;//、rb表示区间从右边开始连续的1的个数如当前区间为1-10 那么这个就代表包含10的最长1串
int l_w;//、l w表示区间从左边开始连续的0的个数
int r_w;//、rw表示区间从右边开始连续的0的个数
int maxb;
int maxw;
int cnt;
/*cnt表示区间被访问的次数,这里利用它实现lazy思xiang
这里简述一下lazy思想:每次访问的时候不用都更新到最低层的元线段,每次只访问到恰好的区间,
在这里设一个标志,表示这次更新只访问到了该区间!那么当再次访问该区间的时候就需要将该标志往其子区间传递*/
}tree[100000*4];
int max(int a,int b)
{
if(a>b) return a;
else return b;
}
int min(int a,int b)
{
if(a<b) return a;
else return b;
}
void update(int nd)
{
int left_len,right_len;
tree[nd].maxb=max(max(tree[nd*2].maxb,tree[nd*2+1].maxb),tree[nd*2].r_b+tree[nd*2+1].l_b);
tree[nd].maxw=max(max(tree[nd*2].maxw,tree[nd*2+1].maxw),tree[nd*2].r_w+tree[nd*2+1].l_ w);
left_len=tree[nd*2].right-tree[nd*2].left+1; right_len=tree[nd*2+1].right-tree[nd*2+1].left+1; tree[nd].l_b=tree[nd*2].l_b; if(tree[nd*2].l_b==left_len) tree[nd].l_b+=tree[nd*2+1].l_b; tree[nd].l_w=tree[nd*2].l_w; if(tree[nd*2].l_w==left_len) tree[nd].l_w+=tree[nd*2+1].l_w; tree[nd].r_b=tree[nd*2+1].r_b; if(tree[nd*2+1].r_b==right_len) tree[nd].r_b+=tree[nd*2].r_b; tree[nd].r_w=tree[nd*2+1].r_w; if(tree[nd*2+1].r_w==right_len)
tree[nd].r_w+=tree[nd*2].r_w;
}
void build(int nd,int left,int right)
{
int mid,num;
tree[nd].left=left;tree[nd].right=right; tree[nd].cnt=0;
if(left==right)
{
scanf("%d",&num);
if(num)
{tree[nd].maxb=tree[nd].l_b=tree[nd].r_b=1; tree[nd].maxw=tree[nd].l_w=tree[nd].r_w=0; } else
{tree[nd].maxw=tree[nd].l_w=tree[nd].r_w=1; tree[nd].maxb=tree[nd].l_b=tree[nd].r_b=0; } return;
}
mid=(left+right)/2;
build(nd*2,left,mid);
build(nd*2+1,mid+1,right);//是mid+1 RUNTIEM ERROR 了好几次
update(nd);
}
void change(int nd)
{
int temp;
temp=tree[nd].l_b;tree[nd].l_b=tree[nd].l_w;tree[nd].l_w=temp;
temp=tree[nd].maxb;tree[nd].maxb=tree[nd].maxw;tree[nd].maxw=temp;
temp=tree[nd].r_b;tree[nd].r_b=tree[nd].r_w;tree[nd].r_w=temp;
tree[nd].cnt=!tree[nd].cnt;
}
void update2(int nd,int left,int right)
{
int mid;
if(tree[nd].left==left&&tree[nd].right==right)
{
change(nd);
return ;
}
if(tree[nd].cnt) {change(nd*2);change(nd*2+1);tree[nd].cnt=0;}
mid=(tree[nd].left+tree[nd].right)/2;
if(right<=mid) update2(nd*2,left,right);
else if(left>mid) update2(nd*2+1,left,right);
else {update2(nd*2,left,mid);update2(nd*2+1,mid+1,right);}//是mid+1 RUNTIEM ERROR 了好几次
update(nd);
}
int query(int nd,int left,int right)
{
int mid,lr,rr;
if(tree[nd].left==left&&tree[nd].right==right)
return tree[nd].maxb;
if(tree[nd].cnt) {change(nd*2);change(nd*2+1);tree[nd].cnt=0;}
mid=(tree[nd].right+tree[nd].left)/2;
if(right<=mid) return query(nd*2,left,right);
else if(left>mid)return query(nd*2+1,left,right);
else
{
lr=query(nd*2,left,mid);
rr=query(nd*2+1,mid+1,right);
return max(max(min(tree[nd*2].r_b,lr)+min(tree[nd*2+1].l_b,rr),lr),rr);//这句话真心不懂啊! // return max(max(tree[nd*2].r_b+tree[nd*2+1].l_b,lr),rr);
}
}
int main()
{
int n,i,opr,m,left,right;
while(scanf("%d",&n)!=EOF)
{
build(1,1,n);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&opr,&left,&right);
if(opr) update2(1,left,right);
else printf("%d\n",query(1,left,right));
}
}
return 0;
}
敌兵布阵
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15220 Accepted Submission(s): 6639
Problem Description
C 国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。

A国在海岸线沿直线布置了N个工兵营地,Derek 和Tidy的任务就是要监视这些工兵营地的活动情况。

由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。

中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy 就要马上开始计算这一段的总人数并汇报。

但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek 对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm 题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。

"但Windbreaker已经挂掉电话了。

Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
Input
第一行一个整数T,表示有T组数据。

每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。

接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数最多不超过1000000。

Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
#include<stdio.h>
int n;
struct haha
{
int left;
int right;
int num;
}node[50001*4];
void build(int k,int x,int y)
{
int mid;
node[k].left=x;
node[k].right=y;
node[k].num=0;
if(x==y) return ;
mid=(x+y)/2;
build(k*2,x,mid);
build(k*2+1,mid+1,y);//这个地方是mid+1 小心点哦
}
void update(int nod,int x,int y)
{
int mid;
node[nod].num+=y;
if(node[nod].left==x&&node[nod].right==x)
return;
mid=(node[nod].left+node[nod].right)/2;
if(x>mid) update(nod*2+1,x,y);
else if(x<=mid) update(nod*2,x,y);
return ;
}
int query(int nod,int x,int y)
{
int mid;
if(x==node[nod].left&&y==node[nod].right)
return node[nod].num;
mid=(node[nod].left+node[nod].right)/2;
if(y<node[nod].left||x>node[nod].right)
return 0;//这里是或
if(x>mid)
return query(nod*2+1,x,y);
else
if(y<=mid)
return query(nod*2,x,y);
else if(x<=mid&&y>mid)
return query(nod*2,x,mid)+query(nod*2+1,mid+1,y); }
int main()
{
int i,j,x,y,cas;
char s[20];
scanf("%d",&cas);
for(i=1;i<=cas;i++)
{
printf ("Case %d:\n",i);
scanf("%d",&n);
build(1,1,n);//printf("cao");
for(j=1;j<=n;j++)
{
scanf("%d",&x);
update(1,j,x);
}
while(1)
{
scanf("%s",s);
if(s[0]=='E') break;
else if(s[0]=='A')
{
scanf("%d %d",&x,&y);
update(1,x,y);
}
else if(s[0]=='S')
{
scanf("%d %d",&x,&y);
update(1,x,-y);
}
else if(s[0]=='Q')
{
scanf ("%d %d", &x, &y) ;
printf("%d\n",query(1,x,y));
}
}
}
}
看看另外一种方法做的
1/*********************************************************************** ********
2# Author : Neo Fung
3# Email : neosfung@
4# Last modified: 2012-01-18 18:23
5# Filename: HDU1166 敌兵布阵.cpp
6# Description :
7
*************************************************************************** ***/
8#ifdef _MSC_VER
9#define DEBUG
10#endif
11
12#include <fstream>
13#include <stdio.h>
14#include <iostream>
15#include <string.h>
16#include <string>
17#include <limits.h>
18#include <algorithm>
19#include <math.h>
20#include <numeric>
21#include <functional>
22#include <ctype.h>
23#define L(x) (x<<1)
24#define R(x) (x<<1|1)
25#define MAX 50010
26using namespace std;
27
28struct NODE_T
29{
30int l,r,sum;
31}node[MAX*4];
32
33void init()
34{
35memset(node,'\0',sizeof(node));
36}
37
38void build(const int &t,const int &l,const int &r)
39{
40node[t].l=l;
41node[t].r=r;
42node[t].sum=0;
43if(l==r-1)
44return;
45int mid = (l+r)>>1;
46build(L(t),l,mid);
47build(R(t),mid,r); //注意2边都是mid
48}
49
50void update(const int &t,const int &l,const int &r,const int &val) 51{
52node[t].sum+=val;
53if(node[t].l==node[t].r-1)
54return;
55
56int mid=(node[t].l+node[t].r)>>1;
57if(l>=mid)
58update(R(t),l,r,val);
59else if(r<=mid)
60update(L(t),l,r,val);
61else
62{
63update(L(t),l,mid,val);
64update(R(t),mid,r,val);
65}
66}
67
68int query(const int &t,const int &l,const int &r)
69{
70if(node[t].l==l && node[t].r==r)
71return node[t].sum;
72int mid=(node[t].l+node[t].r)>>1;
73if(l>=mid)
74return query(R(t),l,r);
75else if(r<=mid)
76return query(L(t),l,r);
77else
78return query(L(t),l,mid)+query(R(t),mid,r);
79}
80
81int main(void)
82{
83#ifdef DEBUG
84freopen("../stdin.txt","r",stdin);
85freopen("../stdout.txt","w",stdout);
86#endif
87int ncases,n;
88int x,y;
89char str[10];
90
91scanf("%d",&ncases);
92for(int nc=1;nc<=ncases;++nc)
93{
94printf("Case %d:\n",nc);
95init();
96scanf("%d",&n);
97build(1,0,n+1);
98for(int i=1;i<=n;++i)
99{
100scanf("%d",&x);
101update(1,i,i+1,x);
102}
103getchar();
104while(scanf("%s",str) && str[0]!='E')
105{
106scanf("%d%d",&x,&y);
107if(str[0]=='A')
108update(1,x,x+1,y);
109else if(str[0]=='S')
110update(1,x,x+1,-y);
111else
112printf("%d\n",query(1,x,y+1));
113}
114}
115
116return 0;
}
hdu1698线段树之经典
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8167 Accepted Submission(s): 3970
Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
Y ou may consider the original hook is made up of cupreous sticks.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the s econd line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1 10
2 1 5 2 5 9 3
Sample Output
Case 1: The total value of the hook is 24.
题意:有一个棍子很长可以分为很多节每节都有不同的材料价值为1 2 3 组成初始化状态认为全部节价值为用价值为1的材料组成
现在输入第一个数表示case 第二个数是节数第三个是操作数每个操作有3个数x y num 即从x到y 的节材料改成价值为num的材料
注意要用lazy思想否则必超时
#include<stdio.h>
struct haha
{
int left;
int right;
int sum;//当前节点的总价值
int now;//now是现在的金属类型
}node[100000*4];
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
if(left==right)
{
node[nd].sum=1;node[nd].now=1;
return;
}
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
node[nd].sum=node[nd*2].sum+node[nd*2+1].sum;
node[nd].now=1;
}
void update(int left,int right,int num,int nd)
{
int mid,l_len,r_len;
if(node[nd].now==num) return;
if(left==node[nd].left&&right==node[nd].right)
{
node[nd].sum=num*(right-left+1);
node[nd].now=num;
return;
}
if(node[nd].now)
{
l_len=node[nd*2].right-node[nd*2].left+1;
r_len=node[nd*2+1].right-node[nd*2+1].left+1;
node[nd*2].sum=l_len*node[nd].now;
node[nd*2+1].sum=r_len*node[nd].now;
node[nd*2].now=node[nd].now;
node[nd*2+1].now=node[nd].now;
node[nd].now=0;
}
mid=(node[nd].left+node[nd].right)/2;
if(right<=mid) update(left,right,num,nd*2);
else if(left>mid) update(left,right,num,nd*2+1);
else if(left<=mid&&right>mid){update(left,mid,num,nd*2);update(mid+1,right,num,nd*2+1);} else return;
node[nd].sum=node[nd*2].sum+node[nd*2+1].sum;
}
int main()
{
int k,cas,n,oper,left,right,num;
scanf("%d",&cas);
for(k=1;k<=cas;k++)
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&oper);
while(oper--)
{
scanf("%d %d %d",&left,&right,&num);
update(left,right,num,1);
}
printf("Case %d: The total value of the hook is %d.\n",k,node[1].sum);
}
return 0;
}
线段树中等难题之hdu1540
T unnel Warfare
T ime Limit : 4000/2000ms (Java/O ther) Memory Limit : 65536/32768K (Java/O ther)
T otal Submission(s) : 1 Accepted Submission(s) : 2
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
2
4
/*一路参考人家的代码经历无数障碍终于AC了以后要常看主要的对我有启发的应该在query查找*/
#include<stdio.h>
struct haha
{
int left;
int right;
int l_1;//从左边开始的1的连续长度
int r_1;//从右边开始的1的连续长度
int max_1;//最大的连续1的长度
}node[50000*4];
int max(int x,int y)
{
if(x>y) return x;
else return y;
}
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
node[nd].max_1=node[nd].l_1=node[nd].r_1=right-left+1;
if(left==right)
return;
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
}
void update(int pos,int flag,int nd)
{
int mid,l_len,r_len;
if(node[nd].left==pos&&node[nd].right==pos)
{
if(flag)
node[nd].l_1=node[nd].r_1=node[nd].max_1=1;
else node[nd].l_1=node[nd].r_1=node[nd].max_1=0;
return;
}
l_len=node[nd*2].right-node[nd*2].left+1;
r_len=node[nd*2+1].right-node[nd*2+1].left+1;
mid=(node[nd].left+node[nd].right)/2;
if(pos<=mid) update(pos,flag,nd*2);
else if(pos>mid) update(pos,flag,nd*2+1);
node[nd].max_1=max((node[nd*2].r_1+node[nd*2+1].l_1),max(node[nd*2].max_1,node[nd*2+1 ].max_1));
node[nd].l_1=node[nd*2].l_1;
if(node[nd*2].l_1==l_len) node[nd].l_1+=node[nd*2+1].l_1;
node[nd].r_1=node[nd*2+1].r_1;
if(node[nd*2+1].r_1==r_len) node[nd].r_1+=node[nd*2].r_1;
}
int query(int pos,int nd)
{
int mid,len;
len=node[nd].right-node[nd].left+1;
if(node[nd].left==node[nd].right||node[nd].max_1==0||node[nd].max_1==len)//node[nd].max_1== len 查找到某段都是连续的就return
return node[nd].max_1;
mid=(node[nd].left+node[nd].right)/2;
if(pos<=mid)
{
if(pos>mid-node[nd*2].r_1)
/*例如1-10中2-10均为1 pos=4 那么查找4的时候要进入二个分支1个是左分支
1个是右分支在右分支中查找最左边的点*/
return query(pos,nd*2)+query(node[nd*2+1].left,nd*2+1);
else
return query(pos,nd*2);
}
else if(pos>mid)
{
if(pos<node[nd*2+1].left+node[nd*2+1].l_1)
return query(pos,nd*2+1)+query(node[nd*2].right,nd*2);
else
return query(pos,nd*2+1);
}
}
int main()
{
int n,m,k,a[50000],pos,ans;
char s[2];
while(scanf("%d %d",&n,&m)!=EOF)
{
build(1,n,1);
k=0;
while(m--)
{
scanf("%s",s);
if(s[0]=='D')
{
scanf("%d",&pos);
a[k++]=pos;
update(pos,0,1);//如果是被破坏了就修改值为0
}
else
if(s[0]=='R')
{
k--;
pos=a[k];
update(pos,1,1);//如果没有被破坏依然是1
}
else
if(s[0]=='Q')
{
scanf("%d",&pos);
ans=query(pos,1);
printf("%d\n",ans);
}
}
}
return 0;
}
湘潭市赛最后一题
http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1149
Josephus Problem
Accepted : 44 Submit : 334
Time Limit : 5000 MS Memory Limit : 65536 KB Josephus Problem
Do you know the famous Josephus Problem? There are n people standing in a circle waiting to be executed. The counting out begins at the first people in the circle and proceeds around the
circle in the counterclockwise direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.
In traditional Josephus Problem, the number of people skipped in each round is fixed, so it's easy to find the people executed in the i-th round. However, in this problem, the number of people skipped in each round is generated by a pseudorandom number generator:
x[i+1] = (x[i] * A + B) % M.
Can you still find the people executed in the i-th round?
Input
There are multiple test cases.
The first line of each test cases contains s ix integers 2 ≤ n ≤ 100000, 0 ≤ m ≤ 100000, 0 ≤ x[1], A, B < M ≤ 100000. The second line contains m integers 1 ≤ q[i] < n.
Output
For each test case, output a line containing m integers, the people executed in the q[i]-th round.
Sample Input
2 1 0 1 2 3
1
41 5 1 1 0 2
1 2 3 4 40
Sample Output
1
2 4 6 8 35
#include<iostream>
#include<stdio.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int ans[100001];
int xw[100001];
const int maxn=200000;
int w,sum[maxn<<2];
void build(int l,int r,int rt){///建立线段树
sum[rt] = r - l + 1;
if(l == r) return ;
int m = (l+r) >> 1;
build(lson);
build(rson);
}
int update(int p,int l,int r,int rt){///更新单个节点 sum[rt]--;
if(l == r) return l ;
int m = (l + r) >> 1;
if(p <= sum[rt<<1])
return update(p,lson);
else
return update(p-sum[rt<<1],rson);
}
int main()
{
int n,m,i;
int x,A,B,M;
while(scanf("%d%d%d%d%d%d",&n,&m,&x,&A,&B,&M)!=EOF) {
build(1,n,1);
int z = 1;
for(i = 1; i <= n; i++)
{
z = ((int)x+z)%sum[1];
if(z == 0) z = sum[1];
int s = update(z,1,n,1);
ans[i] = s;
x = (int)(((__int64)x * A + B) % M);
}
for(i = 0; i < m; i++)
scanf("%d",&xw[i]);
for(i=0;i<m-1;i++)
printf("%d ",ans[xw[i]]);
if(m!=0) printf("%d",ans[xw[m-1]]);
printf("\n");
}
return 0;
}
线段树+离散化+曾经RE无数边
Mayor's posters 市长的海报
Time Limit:1000MS Memory Limit:65536K
T otal Submissions:26435 Accepted:7624
Description
∙The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduc e the following rules:
Every candidate can place exactly one poster on the wall.
∙All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
∙The wall is divided into segments and the width of each segment is one byte.
∙Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Y our task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed.
The picture below illustrates the case of the sample input.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output。

相关文档
最新文档