hdu1698(线段树,简单的成段更新)

来源:互联网 发布:azw3电脑打开软件 编辑:程序博客网 时间:2024/06/07 05:05

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12488    Accepted Submission(s): 6221


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.
You 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 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.
 

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
11021 5 25 9 3
 

Sample Output
Case 1: The total value of the hook is 24.
 

#include<cstdio>#include<cstring>#include<iostream>using namespace std;#define M 100010struct node{int l,r,sum;}tree[3*M];void ori_tree(int i,int l,int r){int m=(l+r)/2;tree[i].sum=1;    //这里赋的值既是hook的材料,也是一个标记,当sum的值大于0时,这一段hook全由一种材料组成 tree[i].l=l;tree[i].r=r;if(l!=r){ori_tree(i*2,l,m);ori_tree(i*2+1,m+1,r);}}void updata(int i,int l,int r,int num){if(tree[i].l==l&&tree[i].r==r) tree[i].sum=num;   //当左节点与右节点相等时,此段hook只能由一种材料组成,所以直接赋值 else{if(tree[i].sum!=-1){            tree[i*2].sum=tree[i].sum;tree[i*2+1].sum=tree[i].sum;  //当区间内存在两种材料时,先把其sum值赋给它的两个子节点             tree[i].sum=-1;                                         //然后再将其sum值变为-1         }int m=(tree[i].l+tree[i].r)/2;if(r<=m)updata(i*2,l,r,num);else if(l>m)updata(i*2+1,l,r,num);else{updata(i*2,l,m,num);updata(i*2+1,m+1,r,num);}}}int sum(int i){if(tree[i].sum>0)return tree[i].sum*(tree[i].r-tree[i].l+1);   //输出时当遇到一段区间的sum值大于0时,即可将重量输出 elsereturn sum(i*2)+sum(i*2+1);                   //否则输出其两个子节点的值 }int main(){int t,m,n,i,l,r,num;scanf("%d",&t);for(i=1;i<=t;i++){scanf("%d",&m);ori_tree(1,1,m);scanf("%d",&n);while(n--){scanf("%d%d%d",&l,&r,&num);updata(1,l,r,num);}printf("Case %d: The total value of the hook is %d.\n",i,sum(1));}return 0;}

如果直接更新到最底层,那么会超时,所以这样优化

 

福州大学oj2105,地址:http://acm.fzu.edu.cn/problem.php?pid=2105

Problem 2105 Digits Count

Accept: 83    Submit: 523
Time Limit: 10000 mSec    Memory Limit : 262144 KB

 Problem Description

Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:

Operation 1: AND opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).

Operation 2: OR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).

Operation 3: XOR opn L R

Here opn, L and R are integers.

For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).

Operation 4: SUM L R

We want to know the result of A[L]+A[L+1]+...+A[R].

Now can you solve this easy problem?

 Input

The first line of the input contains an integer T, indicating the number of test cases. (T≤100)

Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.

Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).

Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)

 Output

For each test case and for each "SUM" operation, please output the result with a single line.

 Sample Input

1
4 4
1 2 4 7
SUM 0 2
XOR 5 0 0
OR 6 0 3
SUM 0 2

 Sample Output

7
18

 Hint

A = [1 2 4 7]

SUM 0 2, result=1+2+4=7;

XOR 5 0 0, A=[4 2 4 7];

OR 6 0 3, A=[6 6 6 7];

SUM 0 2, result=6+6+6=18.

 

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define M 1000050#define max(a,b) a>b?a:bstruct Tree{int l,r,cover,sum,nm;int m(){return (l+r)/2;}int len(){return r-l+1;}}tree[M*3];void pushup(int c){    tree[c].sum=tree[c*2].sum+tree[c*2+1].sum;    if(tree[c*2].cover==1&&tree[c*2+1].cover==1&&tree[c*2].nm==tree[c*2+1].nm)    {        tree[c].cover=1;tree[c].nm=tree[c*2].nm;    }    else tree[c].cover=-1;}void pushdown(int c){    if(tree[c].cover==1)    {        tree[c*2].cover=1;tree[c*2+1].cover=1;        tree[c*2].nm=tree[c].nm;tree[c*2+1].nm=tree[c].nm;        tree[c*2].sum=tree[c*2].nm*tree[c*2].len();        tree[c*2+1].sum=tree[c*2+1].nm*tree[c*2+1].len();    }}void dothing(int n,int k,int c){    if(n==1) tree[c].nm=tree[c].nm&k;    else if(n==2) tree[c].nm=tree[c].nm|k;    else tree[c].nm=tree[c].nm^k;}void ori_tree(int l,int r,int c){    tree[c].l=l;    tree[c].r=r;    if(l==r)    {        tree[c].cover=1;        scanf("%d",&tree[c].nm);        tree[c].sum=tree[c].nm;}else{int m=tree[c].m();ori_tree(l,m,c*2);ori_tree(m+1,r,c*2+1);pushup(c);}}void updata(int l,int r,int n,int k,int c){    if(l<=tree[c].l&&tree[c].r<=r&&tree[c].cover==1)    {        dothing(n,k,c);        tree[c].sum=tree[c].nm*tree[c].len();return ;    }    int m=tree[c].m();    pushdown(c);    if(l<=m) updata(l,r,n,k,c*2);    if(m<r) updata(l,r,n,k,c*2+1);    pushup(c);}int query(int l,int r,int c){    if(l==tree[c].l&&r==tree[c].r)        return tree[c].sum;    int m=tree[c].m();    pushdown(c);if(r<=m) return query(l,r,c*2);else if(m<l) return query(l,r,c*2+1);elsereturn query(l,m,c*2)+query(m+1,r,c*2+1);}int main(){int t,m,n,k,l,r;char str[20];scanf("%d",&t);while(t--){    scanf("%d%d",&m,&n);    ori_tree(1,m,1);    while(n--)    {        scanf("%s",str);        if(str[0]=='S')        {            scanf("%d%d",&l,&r);            int ans=query(l+1,r+1,1);            printf("%d\n",ans);        }        else        {            scanf("%d%d%d",&k,&l,&r);            if(str[0]=='A')            {                updata(l+1,r+1,1,k,1);            }            else if(str[0]=='O')            {                updata(l+1,r+1,2,k,1);            }            else            {                updata(l+1,r+1,3,k,1);            }        }    }}return 0;}