线段树专题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166




敌兵布阵

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 97473    Accepted Submission(s): 41240


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询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
 

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
 

Author
Windbreaker
 

Recommend
Eddy
 

Statistic |  Submit |  Discuss |  Note

解析:裸的线段树单点更新,求区间和,可以用树状数组和线段树写

代码1:

//#include
#include
#include
#include
#include
typedef long long LL;
using namespace std;
const int N = 200009;
int dp[N];
int n;

void add(int k, int c)
{
    while(k <= n)
    {
        dp[k] += c;
        k += k&(-k);
    }
}


int sum(int k)
{
    int ans = 0;
    while(k)
    {
        ans += dp[k];
        k -= k&(-k);
    }
    return ans;
}

int main()
{
    int T, cnt = 0;
    scanf("%d", &T);
    char s[11];
    while(T--)
    {
        memset(dp, 0, sizeof(dp));
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            int num;
            scanf("%d", &num);
            add(i, num);
        }
        int x, y;
        printf("Case %d:\n", ++cnt);
        while(true)
        {
            scanf(" %s", s);
            if(s[0] == 'E') break;
            scanf("%d%d", &x, &y);
            if(s[0] == 'Q') printf("%d\n", sum(y)-sum(x-1));
            else if(s[0] == 'S') add(x, -y);
            else add(x, y);
        }
    }
    return 0;
}

代码2:

//#include
#include
#include
#include

typedef long long LL;
using namespace std;
const int N = 200009;
struct node
{
    int x;
}t[N<<2];


void Creat(int i, int l, int r)
{
    if(l == r)
    {
        scanf("%d", &t[i].x);
        return ;
    }
    int mid = (l + r)>>1;
    Creat(i*2, l, mid);
    Creat(i*2+1, mid+1, r);
    t[i].x = t[i*2].x + t[i*2+1].x;
}

void updata(int i, int l, int r, int x, int y)
{
    if(l <= x && x <= r)
    {
        t[i].x += y;
        if(l == r) return ;
        int mid = (l + r)>>1;
        updata(i*2, l, mid, x, y);
        updata(i*2+1, mid+1, r, x, y);
    }
}

int Find(int i, int l, int r, int x, int y)
{
    if(l == x && r == y) return t[i].x;
    int mid = (l + r)>>1;
    if(mid >= y) return Find(i*2, l, mid, x, y);
    else if(mid < x) return Find(i*2+1, mid+1, r, x, y);
    return Find(i*2, l, mid, x, mid) + Find(i*2+1, mid+1, r, mid+1, y);
}

int main()
{
    int T, cnt = 0, n;
    scanf("%d", &T);
    char s[11];
    while(T--)
    {
        scanf("%d", &n);
        Creat(1, 1, n);
        int x, y;
        printf("Case %d:\n", ++cnt);
        while(true)
        {
            scanf(" %s", s);
            if(s[0] == 'E') break;
            scanf("%d%d", &x, &y);
            if(s[0] == 'Q') printf("%d\n", Find(1, 1, n, x, y));
            else if(s[0] == 'S') updata(1, 1, n, x, -y);
            else updata(1, 1, n, x, y);
        }
    }
    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754



空AI大赛”(报名中...) 

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 81816    Accepted Submission(s): 31472


Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
 

Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0 学生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 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
 

Sample Output
      
        
        
        
        
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
 

Author
linle
 

Source
2007省赛集训队练习赛(6)_linle专场
 

Recommend
lcy
 

Statistic |  Submit |  Discuss |  Note

解析:线段树单点更新,求最值,可以用树状数组(比较麻烦)、线段树

代码1:

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=200009;
int dp[N];
int a[N];
int m,n;

void updata(int k)
{
    while(k<=m)
    {
        int i;
        dp[k]=a[k];
        int lx=k&(-k);
        for( i=1; i=l)
    {
        ans=max(ans,a[r]);
        r--;
        for(; r-(r&(-r))>=l; r-=r&(-r))
            ans=max(ans,dp[r]);
    }
    return ans;
}

void up(int x,int y)
{
   a[x]=y;
   dp[x]=0;
   updata(x);
}

int main()
{
    while( ~scanf("%d%d",&m,&n))
    {
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&a[i]);
            updata(i);
        }
        char cc;
        for(int i=1;i<=n;i++)
        {
            getchar();
            scanf("%c",&cc);
            int x, y;
            scanf("%d%d",&x,&y);
            if(cc=='U')up(x,y);
            else
            {
                int maxx=Max(x,y);
                printf("%d\n",maxx);
            }
        }
    }
    return 0;
}

代码2:

#include
using namespace std;
typedef long long LL;
const int N = 500009;
int dp[N], n, a[N];


void add(int k)
{
    while(k <= n)
    {
        dp[k] = a[k];
        int lx = k&(-k);
        for(int i = 1; i < lx; i <<= 1) dp[k] = max(dp[k], dp[k - i]);
        k += k&(-k);
    }
}


int sum(int x, int y)
{
    int ans = 0;
    while(y >= x)
    {
        ans = max(ans, a[y]);
        y--;
        for(; y - (y&(-y)) >= x; y -= y&(-y)) ans = max(ans, dp[y]);
    }
    return ans;
}

int main()
{
    int m;
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            add(i);
        }
        char s[109];
        int x, y;
        //scanf("%d", &m);
        while(m--)
        {
            scanf(" %s", s);
            scanf("%d%d", &x, &y);
            if(s[0] == 'Q') printf("%d\n", sum(x, y));
            else a[x] = y, add(x);
        }
    }
    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795


Billboard

Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24218    Accepted Submission(s): 9974


Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.

On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.

Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.

When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.

If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).

Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
 

Input
There are multiple cases (no more than 40 cases).

The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.

Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
 

Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
 

Sample Input
  
    
    
    
    
3 5 5 2 4 3 3 3
 

Sample Output
  
    
    
    
    
1 2 1 3 -1
 

Author
hhanger@zju
 

Source
HDOJ 2009 Summer Exercise(5)
 

Recommend
lcy
 

Statistic |  Submit |  Discuss |  Note

解析:线段树去维护一个高度,没遇到一个新的广告牌,就去找最左边的

代码:

#include
using namespace std;
typedef long long LL;
const int N = 200009;


struct node
{
    int x;
}t[N<<2];
int w;

void init(int i, int l, int r)
{
    t[i].x = w;
    if(l != r)
    {
        int mid = (l + r)>>1;
        init(i*2, l, mid);
        init(i*2+1, mid+1, r);
    }
}

int Creat(int i, int l, int r, int x)
{
    if(l == r && t[i].x >= x)
    {
        t[i].x -= x;
        return l;
    }
    int mid = (l + r)>>1, ans = -1;
    if(t[i*2].x >= x) ans = Creat(i*2, l, mid, x);
    else if(t[i*2+1].x >= x) ans = Creat(i*2+1, mid+1, r, x);
    t[i].x = max(t[i*2].x, t[i*2+1].x);
    return ans;
}

int main()
{
    int num, n, h;
    while(~scanf("%d%d%d", &h, &w, &n))
    {
        h = min(h, n);
        init(1, 1, h);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &num);
            printf("%d\n", Creat(1, 1, h, num));
        }
    }
    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540


Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9543    Accepted Submission(s): 3727


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 0 2 4
 

Source
POJ Monthly
 

Recommend
LL
 

Statistic |  Submit |  Discuss |  Note

解析:用线段树求连续区间,维护一个lx,rx,mx,分别是左连续区间,右连续区间,最大连续区间,还有运用到栈暂存破坏的村庄

代码:

#include
using namespace std;
typedef long long LL;
const int N = 50009;


struct node
{
    int mx, lx, rx;
} t[N<<2];

stack s;

void Creat(int i, int l, int r)
{

    t[i].mx = t[i].lx = t[i].rx = r - l + 1;
    if(l != r)
    {
        int mid = (l + r)>>1;
        Creat(i*2, l, mid);
        Creat(i*2+1, mid+1, r);
    }
}

void updata(int i, int l, int r, int x, int f)
{
    if(l == r)
    {
        if(f) t[i].lx = t[i].rx = t[i].mx = 1;
        else t[i].lx = t[i].rx = t[i].mx = 0;
        return ;
    }
    int mid = (l + r)>>1;
    if(x <= mid) updata(i*2, l, mid, x, f);
    else updata(i*2+1, mid+1, r, x, f);

    t[i].lx = t[i*2].lx;
    t[i].rx = t[i*2+1].rx;
    t[i].mx = max(max(t[i*2].mx, t[i*2+1].mx), t[i*2].rx + t[i*2+1].lx);
    if(t[i*2].lx == mid - l + 1) t[i].lx += t[i*2+1].lx;
    if(t[i*2+1].rx == r - mid) t[i].rx += t[i*2].rx;
}

int Find(int i, int l, int r, int x)
{
    if(l == r || t[i].mx == 0 || t[i].mx == r - l + 1) return t[i].mx;
    int mid = (l + r)>>1;
    if(x <= mid)
    {
        if(x >= mid - t[i*2].rx + 1) return Find(i*2, l, mid, x) + Find(i*2+1, mid+1, r, mid+1);
        else return Find(i*2, l, mid, x);
    }
    else
    {
        if(x <= mid+t[i*2+1].lx) return Find(i*2, l, mid, mid) + Find(i*2+1, mid+1, r, x);
        else return Find(i*2+1, mid+1, r, x);
    }
}

int main()
{
    int n, m, x;
    char ch;
    while(~scanf("%d%d", &n, &m))
    {
        Creat(1, 1, n);
        while(!s.empty()) s.pop();
        while(m--)
        {
            scanf(" %c", &ch);
            if(ch == 'R')
            {
                if(!s.empty())
                {
                    int num = s.top();
                    s.pop();
                    updata(1, 1, n, num, 1);
                }
                continue;
            }
            scanf("%d", &x);
            if(ch == 'D')
            {
                s.push(x);
                updata(1, 1, n, x, 0);
            }
            else printf("%d\n", Find(1, 1, n, x));
        }

    }
    return 0;
}


题目链接:http://lightoj.com/volume_showproblem.php?problem=1080


1080 - Binary Simulation
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 64 MB

Given a binary number, we are about to do some operations on the number. Two types of operations can be here.

'I i j'    which means invert the bit from i to j (inclusive)

'Q i'    answer whether the ith bit is 0 or 1

The MSB (most significant bit) is the first bit (i.e. i=1). The binary number can contain leading zeroes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with a line containing a binary integer having length n (1 ≤ n ≤ 105). The next line will contain an integer q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form 'I i j' where i, j are integers and 1 ≤ i ≤ j ≤ n. Or the query will be in the form 'Q i' where i is an integer and 1 ≤ i ≤ n.

Output

For each case, print the case number in a single line. Then for each query 'Q i' you have to print 1 or 0 depending on the ith bit.

Sample Input

Output for Sample Input

2

0011001100

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

1011110111

6

I 1 10

I 2 7

Q 2

Q 1

Q 7

Q 5

Case 1:

0

1

1

0

Case 2:

0

0

0

1

Note

Dataset is huge, use faster i/o methods.


PROBLEM SETTER: JANE ALAM JAN

解析:区间翻转,1变成0,0变成1,我们应该只关心它翻转的次数是奇数还是偶数,所以用树状数组维护区间

代码:

#include
using namespace std;
typedef long long LL;
const int N = 500009;
int dp[N];
char s[N];
int n;


void add(int k, int x)
{
    while(k <= n)
    {
        dp[k] += x;
        k += k&(-k);
    }
}


int sum(int k)
{
    int ans = 0;
    while(k)
    {
        ans += dp[k];
        k -= k&(-k);
    }
    return ans;
}


int main()
{
    int t, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        memset(dp, 0, sizeof(dp));
        scanf(" %s", s);
        int m, l, r;
        char ch;
        scanf("%d", &m);
        n = strlen(s);
        printf("Case %d:\n", ++cnt);
        while(m--)
        {
            scanf(" %c", &ch);
            if(ch == 'I')
            {
                scanf("%d%d", &l, &r);
                add(l, 1);
                add(r+1, -1);
            }
            else
            {
                scanf("%d", &r);
                if(sum(r)&1) printf("%d\n", !(s[r - 1] - '0'));
                else printf("%c\n", s[r-1]);
            }
        }
    }
    return 0;
}

题目链接:http://lightoj.com/volume_showproblem.php?problem=1085


1085 - All Possible Increasing Subsequences
    PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 64 MB

An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold

1.      i1 < i2 < i3 < ... < ik and

2.      Ai1 < Ai2 < Ai3 < ... < Aik

Now you are given a sequence, you have to find the number of all possible increasing subsequences.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.

Output

For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.

Sample Input

Output for Sample Input

3

3

1 1 2

5

1 2 1000 1000 1001

3

1 10 11

Case 1: 5

Case 2: 23

Case 3: 7

Notes

1.      For the first case, the increasing subsequences are (1), (1, 2), (1), (1, 2), 2.

2.      Dataset is huge, use faster I/O methods.


PROBLEM SETTER: JANE ALAM JAN

解析:求前面有多少比它小的数的所有组合,用树状数组去维护前面有多少个比它小的数的种类,最后加一下就好了

代码:

#include
using namespace std;
typedef long long LL;
const int N = 500009;
const int mod = 1000000007;
int dp[N], a[N], b[N];
int n;


void add(int k, int x)
{
    while(k <= n)
    {
        dp[k] += x;
        dp[k] %= mod;
        k += k&(-k);
    }
}


int sum(int k)
{
    int ans = 0;
    while(k)
    {
        ans += dp[k];
        ans %= mod;
        k -= k&(-k);
    }
    return ans;
}


bool cmp(int i, int j)
{
    if(a[i] != a[j]) return a[i] < a[j];
    return i > j;
}

int main()
{
    int t, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        memset(dp, 0, sizeof(dp));
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            b[i] = i;
        }
        sort(b+1, b+n+1, cmp);
        for(int i = 1; i <= n; i++) add(b[i], sum(b[i])+1);
        printf("Case %d: %d\n", ++cnt, sum(n));
    }
    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541


Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10403    Accepted Submission(s): 4163


Problem Description
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. 



For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. 

You are to write a program that will count the amounts of the stars of each level on a given map.
 

Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
 

Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
 

Sample Input
  
    
    
    
    
5 1 1 5 1 7 1 3 3 5 5
 

Sample Output
  
    
    
    
    
1 2 1 1 0
 

Source
Ural Collegiate Programming Contest 1999
 

Recommend
LL
 

Statistic |  Submit |  Discuss |  Note

解析:仔细想想y其实并没有什么用,因为它输入时已经有序了,所以咱用树状数组去维护前面有多少个比它小的,最后输出即可

代码:

#include
using namespace std;
const int N = 32006;
int n;
int dp[N];
int num[N];


void add(int k, int c)
{
    while(k <= N)
    {
        dp[k] += c;
        k += k&(-k);
    }
}

int sum(int k)
{
    int ans = 0;
    while(k)
    {
        ans += dp[k];
        k -= k&(-k);
    }
    return ans;
}


int main()
{
    while(~scanf("%d", &n))
    {
        int x, y;
        memset(dp, 0, sizeof(dp));
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &x, &y);
            num[sum(x+1)]++;
            add(x+1, 1);
        }
        for(int i = 0; i < n; i++) printf("%d\n", num[i]);
    }

    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394


Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21196    Accepted Submission(s): 12715


Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)

You are asked to write a program to find the minimum inversion number out of the above sequences.
 

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 

Output
For each case, output the minimum inversion number on a single line.
 

Sample Input
  
    
    
    
    
10 1 3 6 9 0 8 5 7 4 2
 

Sample Output
  
    
    
    
    
16
 

Author
CHEN, Gaoli
 

Source
ZOJ Monthly, January 2003
 

Recommend
Ignatius.L
 

Statistic |  Submit |  Discuss |  Note

解析:求逆序数,如果单纯的求逆序数,咱可以用归并排序,但这个题是一个环,求最小的逆序数,求出原序列的逆序数之后,咱可以用递推,求其他的逆序数,设原序列逆序数为s,把第一个数a移到最后,多出来比a大的数(n-a)个逆序数,少了比a小的数(n-a-1)个逆序数,递推式: s += n - a[i] - (a[i] - 1); //+ >a[i]  -

代码:

#include
#define N 5500
using namespace std;

int a[N], dp[N];
int n;

void add(int k, int c)
{
    while(k <= n)
    {
        dp[k] += c;
        k += k&(-k);
    }
}

int sum(int k)
{
    int ans = 0;
    while(k)
    {
        ans += dp[k];
        k -= k&(-k);
    }
    return ans;
}


int main()
{
    while(~scanf("%d", &n))
    {
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            a[i]++;
            ans += sum(n) - sum(a[i]);
            add(a[i], 1);
        }
        int s = ans;
        for(int i = 1; i <= n; i++)
        {
            s += n - a[i] - (a[i] - 1); //+ >a[i]  -

题目链接:http://codeforces.com/problemset/problem/272/C


C. Dima and Staircase
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an(1 ≤ a1 ≤ a2 ≤ ... ≤ an).

Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws each box vertically down on the first wi stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., wi. Each thrown box flies vertically down until at least one of the two following events happen:

  • the bottom of the box touches the top of a stair;
  • the bottom of the box touches the top of a box, thrown earlier.

We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width wi cannot touch the stair number wi + 1.

You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a1, a2, ..., an (1 ≤ ai ≤ 109ai ≤ ai + 1).

The next line contains integer m (1 ≤ m ≤ 105) — the number of boxes. Each of the following m lines contains a pair of integers wi, hi(1 ≤ wi ≤ n; 1 ≤ hi ≤ 109) — the size of the i-th thrown box.

The numbers in the lines are separated by spaces.

Output

Print m integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cincout streams or the %I64dspecifier.

Examples
input
5
1 2 3 6 6
4
1 1
3 1
1 1
4 3
output
1
3
4
6
input
3
1 2 3
2
1 1
3 1
output
1
3
input
1
1
5
1 2
1 10
1 10
1 10
1 10
output
1
3
13
23
33
Note

The first sample are shown on the picture.



解析:这是一个线段树区间更新的题,由于题意只放在最左侧,咱可以用一个变量去维护高度,这样这个题就变成一个水题了

代码:

#include
#define N 100009
using namespace std;
typedef long long LL;

LL a[N];

int main()
{
    int n, x, y, m;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    scanf("%d", &m);
    LL h = 1;
    LL mx = 0;
    for(int i = 1; i <= n; i++) mx = max(mx, a[i]);
    while(m--)
    {
        scanf("%d%d", &x, &y);
        if(mx > h) h = max(h, a[x]);
        printf("%I64d\n", h);
        h = h + y;
    }
    return 0;
}


题目链接: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): 34563    Accepted Submission(s): 16879


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
  
    
    
    
    
1 10 2 1 5 2 5 9 3
 

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

Source
2008 “Sunline Cup” National Invitational Contest
 

Recommend
wangye
 

Statistic |  Submit |  Discuss |  Note

解析:线段树区间更新,直接板子

代码:

#include
using namespace std;
typedef long long LL;
const int N = 500009;
const int mod = 1000000007;


struct node
{
    int x, mark;
}t[N<<2];


void Creat(int i, int l, int r)
{
    t[i].mark = 0;
    t[i].x = 1;
    if(l != r)
    {
        int mid = (l + r)>>1;
        Creat(i*2, l, mid);
        Creat(i*2+1, mid+1, r);
        t[i].x = t[i*2].x + t[i*2+1].x;
    }
}


void pushdown(int i, int l, int r)
{
    if(t[i].mark)
    {
        int mid = (l + r)>>1;
        t[i*2].mark = t[i].mark;
        t[i*2+1].mark = t[i].mark;
        t[i*2].x = t[i].mark * (mid-l+1);
        t[i*2+1].x = t[i].mark * (r-mid);
        t[i].mark = 0;
    }
}


void updata(int i, int l, int r, int x, int y, int z)
{
    if(r < x || y < l) return ;

    if(x <= l && r <= y)
    {
        t[i].mark = z;
        t[i].x = (r-l+1)*z;
        return ;
    }
    pushdown(i, l, r);
    int mid = (l + r)>>1;
    updata(i*2, l, mid, x, y, z);
    updata(i*2+1, mid+1, r, x, y, z);
    t[i].x = t[i*2].x + t[i*2+1].x;
}

int main()
{
    int T, cnt = 0, n, m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        Creat(1, 1, n);
        scanf("%d", &m);
        int x, y, z;
        while(m--)
        {
            scanf("%d%d%d", &x, &y, &z);
            updata(1, 1, n, x, y, z);
        }
        printf("Case %d: The total value of the hook is %d.\n", ++cnt, t[1].x);
    }
    return 0;
}


题目链接:http://poj.org/problem?id=3468



A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 116545   Accepted: 36219
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi

[Submit]   [Go Back]   [Status]   [Discuss]


解析:线段树区间更新,求区间和

代码:

//#include
#include
#include
#include

typedef long long LL;
using namespace std;
const int N = 100009;
struct node
{
    LL x, mark;
}t[N<<2];



void Creat(int i, int l, int r)
{
    t[i].mark = 0ll;
    if(l == r)
    {
        scanf("%lld", &t[i].x);
        return ;
    }
    int mid = (l + r)>>1;
    Creat(i*2, l, mid);
    Creat(i*2+1, mid+1, r);
    t[i].x = t[i*2].x + t[i*2+1].x;
}


void pushdown(int i, int l, int r)
{
    if(t[i].mark)
    {
        int mid = (l + r)>>1;
        t[i*2].mark += t[i].mark;
        t[i*2+1].mark += t[i].mark;
        t[i*2].x += t[i].mark*(mid-l+1);
        t[i*2+1].x += t[i].mark*(r-mid);
        t[i].mark = 0;
    }
}

void updata(int i, int l, int r, int x, int y, LL z)
{
    if(r < x || y < l) return ;
    if(x <= l && r <= y)
    {
        t[i].mark += z;
        t[i].x += z*(r-l+1);
        return ;
    }
    pushdown(i, l, r);
    int mid = (l + r)>>1;
    updata(i*2, l, mid, x, y, z);
    updata(i*2+1, mid+1, r, x, y, z);
    t[i].x = t[i*2].x + t[i*2+1].x;
}


LL Find(int i, int l, int r, int x, int y)
{
    if(r < x || y < l) return 0;
    if(x <= l && r <= y) return t[i].x;
    pushdown(i, l, r);
    int mid = (l + r)>>1;
    return Find(i*2, l, mid, x, y) + Find(i*2+1, mid+1, r, x, y);
}

int main()
{
    int n, q;
    scanf("%d%d", &n, &q);
    Creat(1, 1, n);
    char ch;
    int x, y;
    LL z;
    while(q--)
    {
        scanf(" %c", &ch);
        if(ch == 'Q')
        {
            scanf("%d%d", &x, &y);
            printf("%lld\n", Find(1, 1, n, x, y));
        }
        else
        {
            scanf("%d%d%lld", &x, &y, &z);
            updata(1, 1, n, x, y, z);
        }
    }
    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4553


约会安排

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1830    Accepted Submission(s): 511


Problem Description
  寒假来了,又到了小明和女神们约会的季节。
  小明虽为屌丝级码农,但非常活跃,女神们常常在小明网上的大段发言后热情回复“呵呵”,所以,小明的最爱就是和女神们约会。与此同时,也有很多基友找他开黑,由于数量实在过于巨大,怎么安排时间便成了小明的一大心事。
  我们已知小明一共有T的空闲时间,期间会有很多女神或者基友来找小明。
  作为一个操作系统曾经怒考71分的大神,小明想到了一个算法,即“首次适应算法”,根据操作系统课本的描述,就是找一段最靠前的符合要求的连续空间分配给每个请求,由此小明做出了一个决定:
  当一个基友来找小明时,小明就根据“首次适应算法”来找一段空闲的时间来和基友约好,如果找到,就说“X,let’s fly”(此处,X为开始时间),否则就说“fly with yourself”;
  当女神来找小明时,先使用一次“首次适应算法”,如果没有找到,小明就冒着木叽叽的风险无视所有屌丝基友的约定,再次使用“无视基友首次适应算法”,两次只要有一次找到,就说“X,don’t put my gezi”(此处,X为开始时间),否则就说“wait for me”
  当然,我们知道小明不是一个节操负无穷的人,如果和女神约会完,还有剩余时间,他还是会和原来约好的基友去dota的。(举个例子:小西(屌丝)和小明约好在1~5这个时间单位段内打dota,这时候,女神来和小明预约长度为3的时间段,那么最终就是1~3小明去和女神约会,搞定后在4~5和小西打dota)
  小明偶尔也会想要学习新知识,此时小明就会把某一个时间区间的所有已经预定的时间全部清空用来学习并且怒吼“I am the hope of chinese chengxuyuan!!”,不过小明一般都是三分钟热度,再有人来预定的话,小明就会按耐不住寂寞把学习新知识的时间分配出去。
 

Input
输入第一行为CASE,表示有CASE组测试数据;
每组数据以两个整数T,N开始,T代表总共的时间,N表示预约请求的个数;
接着的N行,每行表示一个女神或者基友的预约,“NS QT”代表一个女神来找小明约一段长为QT的时间,“DS QT”则代表一个屌丝的长为QT的请求,当然也有可能是小明想学知识了,“STUDY!! L R”代表清空L~R区间内的所有请求。

[Technical Specification]
1. 1 <= CASE <= 30
2. 1 <= T, N <= 100000
3. 1 <= QT <= 110000
4. 1 <= L <= R <=T
 

Output
对于每一个case,第一行先输出“Case C:”代表是第几个case,然后N行,每行对应一个请求的结果(参照描述)。
输出样本(可复制此处):
“X,let's fly”,”fly with yourself”,”X,don't put my gezi”,”wait for me”,”I am the hope of chinese chengxuyuan!!”
 

Sample Input
  
    
    
    
    
1 5 6 DS 3 NS 2 NS 4 STUDY!! 1 5 DS 4 NS 2
 

Sample Output
  
    
    
    
    
Case 1: 1,let's fly 4,don't put my gezi wait for me I am the hope of chinese chengxuyuan!! 1,let's fly 1,don't put my gezi
 

Source
2013金山西山居创意游戏程序挑战赛——初赛(3)
 

Recommend
liuyiding

解析:线段树区间更新,比较复杂需要考虑优先级和lx,rx,mx

代码:

#include
using namespace std;
typedef long long LL;

const int N = 100010;

struct node
{
    int d, n, s; // mark
    int dlx, drx, dmx;
    int nlx, nrx, nmx;
}t[N<<2];


void ns(int i, int l, int r)
{
    t[i].n = 1; t[i].d = 0;
    t[i].dlx = t[i].drx = t[i].dmx = 0;
    t[i].nlx = t[i].nrx = t[i].nmx = 0;
}

void ds(int i, int l, int r)
{
    t[i].d = 1;
    t[i].dlx = t[i].drx = t[i].dmx = 0;
}

void stu(int i, int l, int r)
{
    t[i].s = 1; t[i].d = t[i].n = 0;
    t[i].dlx = t[i].drx = t[i].dmx = r - l + 1;
    t[i].nlx = t[i].nrx = t[i].nmx = r - l + 1;
}


void pushup(int i, int l, int r)
{
    int mid = (l+r)>>1;
    t[i].dlx = t[i*2].dlx; t[i].drx = t[i*2+1].drx;
    t[i].dmx = max(max(t[i*2].dmx, t[i*2+1].dmx), t[i*2].drx + t[i*2+1].dlx);
    if(t[i*2].dlx == mid - l + 1) t[i].dlx += t[i*2+1].dlx;
    if(t[i*2+1].drx == r - mid) t[i].drx += t[i*2].drx;

    t[i].nlx = t[i*2].nlx; t[i].nrx = t[i*2+1].nrx;
    t[i].nmx = max(max(t[i*2].nmx, t[i*2+1].nmx), t[i*2].nrx + t[i*2+1].nlx);
    if(t[i*2].nlx == mid - l + 1) t[i].nlx += t[i*2+1].nlx;
    if(t[i*2+1].nrx == r - mid) t[i].nrx += t[i*2].nrx;
}

void pushdown(int i, int l, int r)
{
    int mid = (l + r)>>1;
    if(t[i].s)
    {
        stu(i*2, l, mid);
        stu(i*2+1, mid+1, r);
        t[i].s = 0;
    }
    if(t[i].n)
    {
        ns(i*2, l, mid);
        ns(i*2+1, mid+1, r);
        t[i].n = 0;
    }
    if(t[i].d)
    {
        ds(i*2, l, mid);
        ds(i*2+1, mid+1, r);
        t[i].d = 0;
    }
}




void Creat(int i, int l, int r)
{
     stu(i, l, r);
     if(l == r) return ;
     int mid = (l+r)>>1;
     Creat(i<<1,l, mid);
     Creat(i<<1|1, mid+1, r);
     pushup(i, l, r);
}

void study(int i, int l, int r, int x, int y)
{
    if(x == l && r == y)
    {
        stu(i, l, r);
        return ;
    }
    pushdown(i, l, r);
    int mid = (l+r)>>1;
    if(mid >= y) study(i*2, l, mid, x, y);
    else if(mid < x) study(i*2+1, mid+1, r, x, y);
    else
    {
        study(i*2, l, mid, x, mid);
        study(i*2+1, mid+1, r, mid+1, y);
    }
    pushup(i, l, r);
}


void updata(int i, int l, int r, int x, int y, int f)
{
    if(x == l && r == y)
    {
        if(f) ds(i, l, r);
        else ns(i, l, r);
        return ;
    }
    pushdown(i, l, r);
    int mid = (l+r)>>1;
    if(mid >= y) updata(i*2, l, mid, x, y, f);
    else if(mid < x) updata(i*2+1, mid+1, r, x, y, f);
    else
    {
        updata(i*2, l, mid, x, mid, f);
        updata(i*2+1, mid+1, r, mid+1, y, f);

    }
    pushup(i, l, r);
}


int Find(int i, int l, int r, int x, int f)
{
    if(l == r) return l;
    int mid = (l + r)>>1;
    pushdown(i, l, r);
    if(f)
    {
        if(t[i*2].dmx >= x) return Find(i*2, l, mid, x, f);
        else if(t[i*2].drx + t[i*2+1].dlx >= x) return mid - t[i*2].drx + 1;
        else return Find(i*2+1, mid+1, r, x, f);
    }
    else
    {
        if(t[i*2].nmx >= x) return Find(i*2, l, mid, x, f);
        else if(t[i*2].nrx + t[i*2+1].nlx >= x) return mid - t[i*2].nrx + 1;
        else return Find(i*2+1, mid+1, r, x, f);
    }
}


int main()
{
    int T, n, m, cnt = 0;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        Creat(1, 1, n);
        char s[11];
        int x, y;
        printf("Case %d:\n", ++cnt);
        while(m--)
        {
            scanf(" %s", s);
            if(s[0] == 'S')
            {
                scanf("%d%d", &x, &y);
                study(1, 1, n, x, y);
                printf("I am the hope of chinese chengxuyuan!!\n");
                continue;
            }
            scanf("%d", &x);
            if(s[0] == 'D')
            {
                if(t[1].dmx < x)  printf("fly with yourself\n");
                else
                {
                    y = Find(1, 1, n, x, 1);
                    updata(1, 1, n, y, y+x-1, 1);
                    printf("%d,let's fly\n", y);
                }
            }
            else
            {
                if(t[1].dmx < x)
                {
                    if(t[1].nmx < x) printf("wait for me\n");
                    else
                    {
                        y = Find(1, 1, n, x, 0);
                        updata(1, 1, n, y, y+x-1, 0);
                        printf("%d,don't put my gezi\n", y);
                    }
                }
                else
                {
                    y = Find(1, 1, n, x, 1);
                    updata(1, 1, n, y, y+x-1, 0);
                    printf("%d,don't put my gezi\n", y);
                }
            }
        }
    }
    return 0;
}



//2
//5 5
//DS 1
//NS 5
//DS 1
//NS 3
//NS 6


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4267


A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5995    Accepted Submission(s): 1922


Problem Description
Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. The other is to query the value of some element.
 

Input
There are a lot of test cases. 
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)
 

Output
For each test case, output several lines to answer all query operations.
 

Sample Input
  
    
    
    
    
4 1 1 1 1 14 2 1 2 2 2 3 2 4 1 2 3 1 2 2 1 2 2 2 3 2 4 1 1 4 2 1 2 1 2 2 2 3 2 4
 

Sample Output
  
    
    
    
    
1 1 1 1 1 3 3 1 2 3 4 1
 

Source
2012 ACM/ICPC Asia Regional Changchun Online
 

Recommend
liuyiding
 

解析:区间更新,树状数组写,dp[x][k][mod]是第x位置,%k,余数为mod

代码:

#include
using namespace std;

int dp[50009][11][11];
int aa[50009], n;

void add(int x, int k, int mod, int c)
{
    while(x)
    {
        dp[x][k][mod] += c;
        x -= x&(-x);
    }
}

int sum(int x,int a)
{
    int ans = 0;
    while(x <= n)
    {
        for(int i = 1; i <= 10; i++) ans += dp[x][i][a%i];
        x += x&(-x);
    }
    return ans;
}

int main()
{
    while(~scanf("%d", &n))
    {
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++) scanf("%d", &aa[i]);
        int m, op, a, b, c, k;
        scanf("%d", &m);
        while(m--)
        {
            scanf("%d", &op);
            if(op == 1)
            {
                scanf("%d%d%d%d", &a, &b, &k, &c);
                add(b, k, a%k, c);
                add(a-1, k, a%k, -c);
            }
            else
            {
                scanf("%d", &a);
                printf("%d\n", sum(a, a) + aa[a]);
            }
        }
    }
    return 0;
}


题目链接:http://lightoj.com/volume_showproblem.php?problem=1120


1120 - Rectangle Union
    PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 64 MB

Given some axis parallel rectangles, you have to find the union of their area. For example, see the shaded regions in the picture. Each rectangle will be denoted by four integers. They are x1, y1, x2, y2 where (x1, y1) denotes the lower left corner and (x2, y2) denotes the upper right corner.

For the picture above, there are three rectangles. For the yellow rectangle the co-ordinates are (0, 2) and (3, 6). For the blue rectangle the co-ordinates are (1, 3) and (6, 7). For the green rectangle the co-ordinates are (2, 1) and (5, 4). So, the union area is (the shaded region) 31 square units.

Input

Input starts with an integer T (≤ 13), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 30000). Each of the next n lines will contain four integers x1, y1, x2, y2 (0 ≤ x1, y1, x2, y2 ≤ 109, x1 < x2, y1 < y2) denoting a rectangle.

Output

For each case, print the case number and the union area.

Sample Input

Output for Sample Input

2

3

0 2 3 6

1 3 6 7

2 1 5 4

2

0 0 4 4

1 1 2 5

Case 1: 31

Case 2: 17

Notes

Dataset is huge, use faster I/O methods.


PROBLEM SETTER: JANE ALAM JAN
Developed and Maintained by 
JANE ALAM JAN

解析:裸的扫描线,用线段树去维护当前区间,这里其实有图的,还没画,待更新,见下图(矩形的下边为1,上边为-1,用于标记,然后一层一层的计算面积,图为样例1)

线段树专题_第1张图片

扫描线基础题:http://codeforces.com/contest/612/problem/D

题解:http://blog.csdn.net/qq_34287501/article/details/77248371

代码:

#include
using namespace std;
typedef long long LL;

struct node
{
    int x1, x2, y, f;
}mp[60009];

int x[60009];
int f[60009*4], sum[60009*4];


void pushup(int i, int l, int r)
{
    if(f[i])
    {
        sum[i] = x[r+1] - x[l];
    }
    else if(l == r)
    {
        sum[i] = 0;
    }
    else sum[i] = sum[i<<1] + sum[i<<1|1];
}

void updata(int i, int l, int r, int x, int y, int z)
{
    if(y < l || r < x) return ;
    if(x <= l && r <= y)
    {
        f[i] += z;
        pushup(i, l, r);
        return ;
    }
    int mid = (l+r)>>1;
    updata(i<<1, l, mid, x, y, z);
    updata(i<<1|1, mid+1, r, x, y, z);
    pushup(i, l, r);
}



bool cmp(node a, node b)
{
    if(a.y != b.y) return a.y < b.y;
    return a.f > b.f;
}


int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("o.txt", "w", stdout);
    int t, n, Case = 0;
    scanf("%d", &t);
    while(t--)
    {
        memset(sum, 0, sizeof(sum));
        memset(f, 0, sizeof(f));
        scanf("%d", &n);
        int len = 0, x1, y1, x2, y2;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            mp[len] = (node){x1, x2, y1, 1};
            x[len++] = x1;
            mp[len] = (node){x1, x2, y2, -1};
            x[len++] = x2;
        }
        sort(x, x+len);
        sort(mp, mp+len, cmp);
        int cnt = unique(x, x+len) - x;
        int l, r;
        LL ans = 0ll;
        for(int i = 0; i < len; i++)
        {
            l = lower_bound(x, x+cnt, mp[i].x1) - x;
            r = lower_bound(x, x+cnt, mp[i].x2) - x - 1;
            updata(1, 0, cnt - 1, l, r, mp[i].f);
            ans += (LL)sum[1] * (mp[i+1].y - mp[i].y);
        }
        printf("Case %d: %lld\n", ++Case, ans);
    }
    return 0;
}


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255


覆盖的面积

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6086    Accepted Submission(s): 3073


Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.


 

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
 

Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
 

Sample Input
  
    
    
    
    
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
 

Sample Output
  
    
    
    
    
7.63 0.00
 

Author
Ignatius.L & weigang Lee
 

Recommend
Ignatius.L
 

Statistic |  Submit |  Discuss |  Note

解析:要求覆盖大于等于2次的面积,原来求的是所有的面积,维护的是覆盖1次以上的sum1[1],现在需要多加一个维护覆盖2次以上的sum[1],这样就可以写了,把pushup函数改下更新下sum[i]数组就好了

代码:

#include
using namespace std;
typedef long long LL;
const int N = 2009;

struct node
{
    double x1, x2, y;
    int f;
}mp[N];

double x[N], sum[N<<2], sum1[N<<2];
int f[N<<2];


void pushup(int i, int l, int r)
{
    if(f[i]) sum1[i] = x[r+1] - x[l];
    else if(l == r) sum1[i] = 0;
    else sum1[i] = sum1[i<<1] + sum1[i<<1|1];

    if(f[i] > 1) sum[i] = x[r+1] - x[l];
    else if(l == r) sum[i] = 0;
    else if(f[i] == 1) sum[i] = sum1[i<<1] + sum1[i<<1|1];
    else sum[i] = sum[i<<1] + sum[i<<1|1];
}

void updata(int i, int l, int r, int x, int y, int z)
{
    if(y < l || r < x) return ;
    if(x == l && r == y)
    {
        f[i] += z;
        pushup(i, l, r);
        return ;
    }
    int mid = (l+r)>>1;
    if(mid >= y) updata(i<<1, l, mid, x, y, z);
    else if(mid < x) updata(i<<1|1, mid+1, r, x, y, z);
    else
    {
        updata(i<<1, l, mid, x, mid, z);
        updata(i<<1|1, mid+1, r, mid+1, y, z);
    }
    pushup(i, l, r);
}



bool cmp(node a, node b)
{
    if(a.y != b.y) return a.y < b.y;
    return a.f > b.f;
}


int main()
{
    int t, n;
    scanf("%d", &t);
    while(t--)
    {
        memset(sum, 0, sizeof(sum));
        memset(sum1, 0, sizeof(sum1));
        memset(f, 0, sizeof(f));
        scanf("%d", &n);
        int len = 0;
        double x1, y1, x2, y2;
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
            mp[len] = (node){x1, x2, y1, 1};
            x[len++] = x1;
            mp[len] = (node){x1, x2, y2, -1};
            x[len++] = x2;
        }
        sort(x, x+len);
        sort(mp, mp+len, cmp);
        int cnt = unique(x, x+len) - x;
        int l, r;
        double ans = 0.0;
        for(int i = 0; i < len; i++)
        {
            l = lower_bound(x, x+cnt, mp[i].x1) - x;
            r = lower_bound(x, x+cnt, mp[i].x2) - x - 1;
            updata(1, 0, cnt - 1, l, r, mp[i].f);
            ans += sum[1] * (mp[i+1].y - mp[i].y);
        }
        printf("%.2f\n", ans);
    }
    return 0;
}


附加:河南省多校联萌(一)D题

题目链接:http://acm.uestc.edu.cn/#/problem/show/1597

解析:线段树区间更新,更新区间加法和乘法,需要2个延迟标记

代码:

#include
#include
#include

typedef long long LL;
using namespace std;
const int N = 111111;
LL p;


struct node
{
    LL x, mark1, mark2;
}t[N<<2];

void Creat(int i, int l, int r)
{
    t[i].mark1 = 1ll;
    t[i].mark2 = 0ll;
    if(l == r)
    {
        scanf("%lld", &t[i].x);
        return ;
    }
    int mid = (l + r)>>1;
    Creat(i*2, l, mid);
    Creat(i*2+1, mid+1, r);
    t[i].x = (t[i*2].x + t[i*2+1].x) % p;
}


void pushdown(int i, int l, int r)
{
    int mid = (l + r)>>1;
    t[i*2].mark1 = (t[i*2].mark1 * t[i].mark1) % p;
    t[i*2+1].mark1 = (t[i*2+1].mark1 * t[i].mark1) % p;

    t[i*2].mark2 = (t[i*2].mark2 * t[i].mark1 + t[i].mark2) % p;
    t[i*2+1].mark2 = (t[i*2+1].mark2 * t[i].mark1 + t[i].mark2) % p;

    t[i*2].x = (t[i*2].x * t[i].mark1 + t[i].mark2*(mid-l+1ll)) % p;
    t[i*2+1].x = (t[i*2+1].x * t[i].mark1 + t[i].mark2*((LL)r-mid)) % p;
    t[i].mark1 = 1ll;
    t[i].mark2 = 0ll;
}

void updata(int i, int l, int r, int x, int y, LL z, int f)
{
    if(r < x || y < l) return ;
    if(x <= l && r <= y)
    {
        if(f)
        {
            t[i].mark2 = t[i].mark2 * z % p;
            t[i].mark1 = t[i].mark1 * z % p;
            t[i].x = (t[i].x * z) % p;
        }
        else
        {
            t[i].mark2 = (t[i].mark2 + z) % p;
            t[i].x = (t[i].x + z*(r-l+1)) % p;
        }
        return ;
    }
    pushdown(i, l, r);
    int mid = (l + r)>>1;
    updata(i*2, l, mid, x, y, z, f);
    updata(i*2+1, mid+1, r, x, y, z, f);
    t[i].x = (t[i*2].x + t[i*2+1].x) % p;
}


LL Find(int i, int l, int r, int x, int y)
{
    if(r < x || y < l) return 0ll;
    if(x <= l && r <= y) return t[i].x;
    pushdown(i, l, r);
    int mid = (l + r)>>1;
    return (Find(i*2, l, mid, x, y) + Find(i*2+1, mid+1, r, x, y)) % p;
}

int main()
{
    int n, m, op, x, y;
    LL z;
    scanf("%d%lld", &n, &p);
    Creat(1, 1, n);
    scanf("%d", &m);
    while(m--)
    {
        scanf("%d", &op);
        if(op == 3)
        {
            scanf("%d%d", &x, &y);
            printf("%lld\n", Find(1, 1, n, x, y));
            continue;
        }
        scanf("%d%d%lld", &x, &y, &z);
        if(op == 1) updata(1, 1, n, x, y, z, 1);
        else updata(1, 1, n, x, y, z, 0);
    }
    return 0;
}




你可能感兴趣的:(algorithm,线段树)