ACM - UVA - 12532 Interval Product

本文介绍了一种解决区间乘积查询与更新问题的方法。通过构建一种特殊的数据结构,可以在给定的时间复杂度内高效地处理序列中元素的更改及查询区间乘积的正负零属性。文章详细展示了算法实现过程,并附带完整的代码示例。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX 100010
using namespace std;

int n,k,qr,ql,v,p;

int father[MAX*4];

char str[MAX * 4];
int query(int o,int l,int r)
{
    int m = (l+r) / 2;
    int ans = 1;
    if(ql <= l && r <= qr)
        return father[o];
    if(ql<=m)
        ans*=query(o*2,l,m);
    if(m < qr)
        ans*=query(o*2+1,m+1,r);
    return ans;

}

void change(int o,int l,int r)
{
    int m = (l+r)/2;
    if(l==r)
        father[o] = v;
    else
    {
        if(p<=m)
            change(o*2,l,m);
        else
            change(o*2+1,m+1,r);
        father[o] = father [o*2]*father [o*2+1];
    }
}
void build(int l,int r,int o)
{
    int m = (l+r)/2;
    if(l==r)
    {
        int t;
        scanf("%d",&t);
        if(t>0)
            father[o] = 1;
        else if(t==0)
            father[o] = 0;
        else if(t<0)
            father[o] = -1;
        return;
    }
    build(l,m,o*2);
    build(m+1,r,o*2+1);
    father[o] = father [o*2] * father [o*2+1];
}


char ans[MAX*4];

int main()
{

    while(~scanf("%d %d",&n,&k))
    {
        build(1,n,1);
        char s[5];
        int c = 0;
        for(int i =0; i < k; i++)
        {
            scanf("%s",s);
            if(s[0] == 'C')
            {
                scanf("%d%d",&p,&v);
                if(v > 0)
                    v = 1;
                else if (v < 0 )
                    v = -1;
                change(1,1,n);
            }
            else if(s[0]=='P')
            {
                scanf("%d%d",&ql,&qr);
                if(query(1,1,n)==0)
                    printf("0");
                else if(query(1,1,n)>0)
                    printf("+");
                else if(query(1,1,n)<0)
                    printf("-");
            }
        }
        puts("");


    }
    return 0;
}
View Code

 

Interval Product
Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KB
Total submit users: 39, Accepted users: 32
Problem 12756 : No special judgement
Problem description
It's normal to feel worried and tense the day before a programming contest. To relax, you went out for a drink with some friends in a nearby pub. To keep your mind sharp for the next day, you decided to play the following game. To start, your friends will give you a sequence of N integers X1;X2;... ;XN. Then, there will be K rounds; at each round, your friends will issue a command, which can be:
  • a change command, when your friends want to change one of the values in the sequence; or
  • a product command, when your friends give you two values I; J and ask you if the product XI × XI+1 ×... × XJ-1 × XJ is positive, negative or zero.
Since you are at a pub, it was decided that the penalty for a wrong answer is to drink a pint of beer. You are worried this could affect you negatively at the next day's contest, and you don't want to check if Ballmer's peak theory is correct. Fortunately, your friends gave you the right to use your notebook. Since you trust more your coding skills than your math, you decided to write a program to help you in the game. 


Input
Each test case is described using several lines. The first line contains two integers N and K, indicating respectively the number of elements in the sequence and the number of rounds of the game (1 ≤ N;K ≤ 105). The second line contains N integers Xi that represent the initial values of the sequence (-100 ≤ Xi ≤ 100 for i = 1; 2;... ;N). Each of the next K lines describes a command and starts with an uppercase letter that is either "C" or "P". If the letter is "C", the line describes a change command, and the letter is followed by two integers I and V indicating that XI must receive the value V (1 ≤ I ≤ N and -100 ≤ V ≤ 100). If the letter is "P", the line describes a product command, and the letter is followed by two integers I and J indicating that the product from XI to XJ , inclusive must be calculated (1 ≤ I ≤ J ≤ N). Within each test case there is at least one product command. 


Output
For each test case output a line with a string representing the result of all the product commands in the test case. The i-th character of the string represents the result of the i-th product command. If the result of the command is positive the character must be "+" (plus); if the result is negative the character must be "-" (minus); if the result is zero the character must be "0" (zero). 


Sample Input
4 6
-2 6 0 -1
C 1 10
P 1 4
C 3 7
P 2 2
C 4 -5
P 1 4
5 9
1 5 -2 4 3
P 1 2
P 1 5
C 4 -5
P 1 5
P 4 5
C 3 0
P 1 5
C 4 -5
C 4 -5
Sample Output
0+-
+-+-0



用个堆来记录就好了



转载于:https://www.cnblogs.com/wejex/p/3390296.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值