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询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。 Sample Input
1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End
Sample Output
Case 1:63359 简单的线段树模板的应用,具体的细节可以看代码的注释。这题涉及到到区间求和和单点修改。
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=50005;const int INF=0x3f3f3f3f;int n,p,q,c;int a[maxn];char s[10];struct Tree//存储这个线段的端点值和线段维护的信息{ int l,r; int sum;}tree[maxn<<2];void build(int k,int l,int r)//建树的过程{ tree[k].l=l;tree[k].r=r; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}void change(int k,int l,int r,int val)/*更新的过程,我习惯性将区间更新和单点更新记一起,区间更新就是多了一个pushdown,单点更新的时候只需要在传参数的时候把l,r传同一个值就可以了。*/{ if(tree[k].l==l&&tree[k].r==r) { tree[k].sum+=val; return; } if(tree[k].l==tree[k].r) return; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid >1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans;}int main(){ int T,casee=1; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); printf("Case %d:\n",casee++); while(scanf("%s",s)) { if(s[0]=='E') break; else if(s[0]=='A') { scanf("%d %d",&p,&c); change(1,p,p,c); } else if(s[0]=='S') { scanf("%d %d",&p,&c); change(1,p,p,-c); } else if(s[0]=='Q') { scanf("%d %d",&p,&q); printf("%d\n",query(1,p,q)); } } } return 0;}
Input本题目包含多组测试,请处理到文件结束。 在每个测试的第一行,有两个正整数 N 和 M ( 0<=200000,0 <5000 ),分别代表学生的数目和操作的数目。 学生ID编号分别从1编到N。 第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。 接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。 当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。 当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。 Output对于每一次询问操作,在一行里面输出最高成绩。Sample Input
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output
5659 区间查询和单点更新
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=200005;const int INF=0x3f3f3f3f;int n,m,p,q,c;int a[maxn];char s[10];struct Tree{ int l,r; int sum;}tree[maxn<<2];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=max(tree[k<<1].sum,tree[k<<1|1].sum);}void change(int k,int l,int r,int val){ if(tree[k].l==l&&tree[k].r==r) { tree[k].sum=val; return; } if(tree[k].l==tree[k].r) return; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid >1; if(r<=mid) ans=query(k<<1,l,r); else if(l>=mid+1) ans=query(k<<1|1,l,r); else { ans=max(query(k<<1,l,mid),query(k<<1|1,mid+1,r)); } return ans;}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d %d",&p,&q); printf("%d\n",query(1,p,q)); } else { scanf("%d %d",&p,&c); change(1,p,p,c); } } } return 0;}
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.Each of the next Q lines represents an operation."C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000."Q a b" means querying the sum of Aa, Aa+1, ... , Ab.Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915 区间更新和区间查询,要用到lazy标记。 题意:支持两种操作,一个是把一个区间内每个人都加上或都减去同一个数,一个是查询一个区间内的最大值。
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=100005;const int INF=0x3f3f3f3f;int n,m,p,q,c;int a[maxn];char s[10];struct Tree{ int l,r; LL sum,lazy;}tree[maxn*8];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r;tree[k].lazy=0; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}void pushdown(int x){ if(tree[x].lazy!=0) { tree[x<<1].lazy+=tree[x].lazy; tree[x<<1|1].lazy+=tree[x].lazy; tree[x<<1].sum+=tree[x].lazy*(tree[x<<1].r-tree[x<<1].l+1); tree[x<<1|1].sum+=tree[x].lazy*(tree[x<<1|1].r-tree[x<<1|1].l+1); tree[x].lazy=0; }}void change(int k,int l,int r,int val){ pushdown(k); if(tree[k].l==l&&tree[k].r==r) { tree[k].lazy+=val; tree[k].sum+=val*(r-l+1); return; } if(tree[k].l==tree[k].r) return; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid >1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans;}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { scanf("%s",s); if(s[0]=='Q') { scanf("%d %d",&p,&q); printf("%lld\n",query(1,p,q)); } else { scanf("%d %d %d",&p,&q,&c); change(1,p,q,c); } } } return 0;}
- 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.
Input
Output
Sample Input
151 42 68 103 47 10
Sample Output
4 区间覆盖,但是这题需要离散化。
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=10005;const int INF=0x3f3f3f3f;int n,m,p,q,c;int li[maxn],ri[maxn];int x[maxn<<1],hashh[10000010];struct Tree{ int l,r; int lazy;}tree[maxn<<3];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r;tree[k].lazy=0; if(l==r) {// tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r);// tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}//void pushdown(int x)//{// if(tree[x].l!=tree[x].r)// {// tree[x<<1].lazy+=tree[x].lazy;// tree[x<<1|1].lazy+=tree[x].lazy;// tree[x<<1].sum+=tree[x].lazy*(tree[x<<1].r-tree[x<<1].l+1);// tree[x<<1|1].sum+=tree[x].lazy*(tree[x<<1|1].r-tree[x<<1|1].l+1);// }// tree[x].lazy=0;//}bool change(int k,int l,int r){ if(tree[k].lazy) return false; if(tree[k].l==l&&tree[k].r==r) { tree[k].lazy=true;// tree[k].sum+=val*(r-l+1); return true; } bool temp; int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) temp=change(k<<1,l,r); else if(mid >1;// if(r<=mid)// ans+=query(k<<1,l,r);// else if(l>=mid+1)// ans+=query(k<<1|1,l,r);// else// {// ans+=query(k<<1,l,mid);// ans+=query(k<<1|1,mid+1,r);// }// return ans;//}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(tree,0,sizeof(tree)); int cnt=0; for(int i=0;i =0;i--) { if(change(1,hashh[li[i]],hashh[ri[i]])) ans++; } printf("%d\n",ans); } return 0;}
InputThe 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 second 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. OutputFor 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
11021 5 25 9 3
Sample Output
Case 1: The total value of the hook is 24.
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=10005;const int INF=0x3f3f3f3f;int n,m,p,q,c;int li[maxn],ri[maxn];int x[maxn<<1],hashh[10000010];struct Tree{ int l,r; LL sum,lazy;}tree[maxn<<3];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r;tree[k].lazy=0; if(l==r) { tree[k].sum=1; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}void pushdown(int x){ if(tree[x].l!=tree[x].r) { tree[x<<1].lazy=tree[x].lazy; tree[x<<1|1].lazy=tree[x].lazy; tree[x<<1].sum=tree[x].lazy*(tree[x<<1].r-tree[x<<1].l+1); tree[x<<1|1].sum=tree[x].lazy*(tree[x<<1|1].r-tree[x<<1|1].l+1); } tree[x].lazy=0;}void change(int k,int l,int r,int val){ if(tree[k].lazy) pushdown(k); if(tree[k].l==l&&tree[k].r==r) { tree[k].sum=val*(r-l+1); tree[k].lazy=val; return; } int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid >1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans;}int main(){ int T,casee=1; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(tree,0,sizeof(tree)); build(1,1,n); scanf("%d",&m); while(m--) { scanf("%d %d %d",&p,&q,&c); change(1,p,q,c); } printf("Case %d: The total value of the hook is %d.\n",casee++,query(1,1,n)); } return 0;}
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 cx1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input 5 0 4 4 0 3 1 3 4 2 0 2 2 0 2 3 4 0 1 1 3 4 1 1 3 2 1 3 1 6 0 1 0 1 2 1 2 3 1 1 2 0 2 3 0 1 2 1
Sample Output 1 1 2 1 3 1
1 1
0 2
1 1#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=8005;const int INF=0x3f3f3f3f;int n,m,p,q,c;int a[maxn],ans[maxn];struct Tree{ int l,r; int lazy;}tree[maxn<<2];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r;tree[k].lazy=-1; if(l==r) { return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r);}void pushdown(int x){ if(tree[x].l!=tree[x].r) { tree[x<<1].lazy=tree[x].lazy; tree[x<<1|1].lazy=tree[x].lazy; tree[x].lazy=-1; }}void change(int k,int l,int r,int val){ if(tree[k].lazy!=-1) pushdown(k); if(tree[k].l==l&&tree[k].r==r) { tree[k].lazy=val; return; } int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r,val); else if(mid >1; if(r<=mid) query(k<<1,l,r); else if(l>=mid+1) query(k<<1|1,l,r); else { query(k<<1,l,mid); query(k<<1|1,mid+1,r); }}int main(){ while(scanf("%d",&n)!=EOF) { memset(tree,0,sizeof(tree)); memset(ans,0,sizeof(ans)); memset(a,-1,sizeof(a)); build(1,1,8001); for(int i=0;i
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Output
Sample Input
6 31734251 54 62 2
Sample Output
630
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=50005;const int INF=0x3f3f3f3f;int n,m,p,q,c;int a[maxn];int ansmax,ansmin;struct Tree{ int l,r; int maxx,minn;}tree[maxn<<3];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r;tree[k].minn=INF;tree[k].maxx=0; if(l==r) { tree[k].maxx=a[l]; tree[k].minn=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].minn=min(tree[k<<1].minn,tree[k<<1|1].minn); tree[k].maxx=max(tree[k<<1].maxx,tree[k<<1|1].maxx);}void query(int k,int l,int r){ if(tree[k].l==l&&tree[k].r==r) { ansmax=max(ansmax,tree[k].maxx); ansmin=min(ansmin,tree[k].minn); return; } int mid=(tree[k].l+tree[k].r)>>1; if(r<=mid) query(k<<1,l,r); else if(l>=mid+1) query(k<<1|1,l,r); else { query(k<<1,l,mid); query(k<<1|1,mid+1,r); }}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(tree,0,sizeof(tree)); build(1,1,n); while(m--) { scanf("%d %d",&p,&q); ansmax=0;ansmin=INF; query(1,p,q); printf("%d\n",ansmax-ansmin); } } return 0;}
InputThe 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. You can assume that the sum of all endurance value is less than 2 63. 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. OutputFor 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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
Sample Output
Case #1:1976
#include#include #include #include #include using namespace std;typedef long long LL;const int maxn=100005;const int INF=0x3f3f3f3f;int n,m,p,q,flag;LL a[maxn],ans[maxn];struct Tree{ int l,r; LL sum,len;}tree[maxn<<2];void build(int k,int l,int r){ tree[k].l=l;tree[k].r=r;tree[k].len=r-l+1; if(l==r) { tree[k].sum=a[l]; return; } int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}void pushdown(int k,int l,int r){ if(tree[k].l==tree[k].r) { tree[k].sum=(LL)sqrt(tree[k].sum); return; } int mid=(tree[k].l+tree[k].r)>>1; pushdown(k<<1,l,mid); pushdown(k<<1|1,mid+1,r); tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}void change(int k,int l,int r){ if(tree[k].l==l&&tree[k].r==r) { if(tree[k].sum==tree[k].len) return; pushdown(k,l,r); return; } int mid=(tree[k].l+tree[k].r)>>1; if(mid>=r) change(k<<1,l,r); else if(mid >1; if(r<=mid) ans+=query(k<<1,l,r); else if(l>=mid+1) ans+=query(k<<1|1,l,r); else { ans+=query(k<<1,l,mid); ans+=query(k<<1|1,mid+1,r); } return ans;}int main(){ int casee=1; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) scanf("%lld",&a[i]); build(1,1,n); scanf("%d",&m); printf("Case #%d:\n",casee++); while(m--) { scanf("%d %d %d",&flag,&p,&q); if(p>q) swap(p,q); if(!flag) change(1,p,q); else printf("%lld\n",query(1,p,q)); } printf("\n"); } return 0;}