2018/8/13

线段树,树状数组,lazy优化

好困不多bb了

一:

C - Ultra-QuickSort

 POJ - 2299 

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 

9 1 0 5 4 ,


Ultra-QuickSort produces the output 

0 1 4 5 9 .


Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
typedef long long ll;
int n;
struct node
{
    int num,ci;
}a[500005];
int tree[500005];
int lowbit(int x)
{
    return x&(-x);
}
void change(int p,int x)
{
    for(int i=p;i<=n;i+=lowbit(i))
        tree[i]+=x;
}
int query(int p)
{
    int sum=0;
    for(int i=p;i>0;i-=lowbit(i))
        sum+=tree[i];
    return sum;
}
bool cmp(node a,node b)
{
    return a.num<b.num;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i].num);
                a[i].ci=i;
                change(i,1);
            }
        sort(a+1,a+1+n,cmp);
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=query(a[i].ci-1);
            change(a[i].ci,-1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

二:

D - Can you answer these queries?

 HDU - 4027 

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

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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<memory.h>
#include<math.h>
using namespace std;
typedef long long ll;
int n,m;
ll a[100005];
ll sum;
struct node
{
    int l,r;
    ll sum;
}b[100005*4];
void build(int l,int r,int p)
{
    int mid;
    b[p].l=l;
    b[p].r=r;
    if(l==r)
    {
        b[p].sum=a[l];
        return;
    }
    mid=(l+r)/2;
    build(l,mid,p*2);
    build(mid+1,r,p*2+1);
    b[p].sum=b[p*2].sum+b[p*2+1].sum;
}
void update2(int x,int p)
{
    int mid;
    if(b[p].l==b[p].r)
    {
        b[p].sum=a[b[p].l]=(int)(sqrt(1.0*a[b[p].l]));
        return;
    }
    mid=(b[p].l+b[p].r)/2;
    if(x>mid)update2(x,p*2+1);
    else update2(x,p*2);
    b[p].sum=b[p*2].sum+b[p*2+1].sum;

}
void update(int l,int r,int p)
{
    int mid;
    if(b[p].l==l&&b[p].r==r)
    {
        if(b[p].sum==b[p].r-b[p].l+1)return;
        for(int j=b[p].l;j<=b[p].r;j++)
        {
            if(a[j]==1)continue;
            update2(j,1);
        }
        return ;
    }
    mid=(b[p].l+b[p].r)/2;
    if(r<=mid)update(l,r,p*2);
    else if(l>mid)update(l,r,p*2+1);
    else
    {
        update(l,mid,p*2);
        update(mid+1,r,p*2+1);
    }
}
void query(int l,int r,int p)
{
    int mid;
    if(b[p].l==l&&b[p].r==r)
    {
        sum+=b[p].sum;
        return;
    }
    mid=(b[p].l+b[p].r)/2;
    if(r<=mid)query(l,r,p*2);
    else if(l>mid)query(l,r,p*2+1);
    else
    {
        query(l,mid,p*2);
        query(mid+1,r,p*2+1);
    }

}
int main()
{
    int cnt=1;
    while(~scanf("%d",&n))
    {
        printf("Case #%d:\n",cnt++);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        build(1,n,1);
        cin>>m;
        for(int i=1;i<=m;i++)
        {
            int t,x,y;
            scanf("%d%d%d",&t,&x,&y);
            if(y<x)
            {
                int tmp;
                tmp=x;
                x=y;
                y=tmp;
            }
            if(t==0)
            {
                update(x,y,1);
            }
            else
            {
                sum=0;
                query(x,y,1);
                printf("%lld\n",sum);
            }
        }
        printf("\n");
    }
    return 0;
}

三:

E - Interesting Array

 CodeForces - 482B 

We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers liriqi (1 ≤ li ≤ ri ≤ n) meaning that value  should be equal to qi.

Your task is to find any interesting array of n elements or state that such array doesn't exist.

Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".

Input

The first line contains two integers nm (1 ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of elements in the array and the number of limits.

Each of the next m lines contains three integers liriqi (1 ≤ li ≤ ri ≤ n, 0 ≤ qi < 230) describing the i-th limit.

Output

If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.

If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.

Examples

Input

3 1
1 3 3

Output

YES
3 3 3

Input

3 2
1 3 3
1 3 2

Output

NO
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int sum[maxn<<2],lazy[maxn<<2];
int l[maxn],r[maxn],c[maxn];
int now;
void pushdown(int i)
{
    if(lazy[i])
    {
        sum[2*i]|=lazy[i];
        sum[2*i+1]|=lazy[i];
        lazy[2*i]|=lazy[i];
        lazy[2*i+1]|=lazy[i];
        lazy[i]=0;
    }
}
void update(int i,int l,int r,int x,int y,int c)
{
    if(x<=l&&r<=y)
    {
        sum[i]|=c;
        lazy[i]|=c;
        return;
    }
    pushdown(i);
    int mid=(l+r)/2;
    if(x<=mid) update(2*i,l,mid,x,y,c);
    if(y>mid) update(2*i+1,mid+1,r,x,y,c);
    sum[i]=sum[2*i]&sum[2*i+1];
    return;
}
void query(int i,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)
    {
        now&=sum[i];
        return;
    }
    pushdown(i);
    int mid=(l+r)/2;
    if(x<=mid) query(2*i,l,mid,x,y);
    if(y>mid) query(2*i+1,mid+1,r,x,y);
    sum[i]=sum[2*i]&sum[2*i+1];
    return;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&l[i],&r[i],&c[i]);
            update(1,1,n,l[i],r[i],c[i]);
        }
        for(int i=1;i<=m;i++)
        {
            now=(1<<30)-1;
            query(1,1,n,l[i],r[i]);
            if(now!=c[i])
            {
                cout<<"NO"<<endl;
                return 0;
            }
        }
        cout<<"YES"<<endl;
        for(int i=1;i<=n;i++)
        {
            now=(1<<30)-1;
            query(1,1,n,i,i);
            cout<<now<<" ";
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值