UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>

A - 秋实大哥与小朋友

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

秋实大哥以周济天下,锄强扶弱为己任,他常对天长叹:安得广厦千万间,大庇天下寒士俱欢颜。

所以今天他又在给一群小朋友发糖吃。

他让所有的小朋友排成一行,从左到右标号。在接下去的时间中,他有时会给一段区间的小朋友每人v颗糖,有时会问第x个小朋友手里有几颗糖。

这对于没上过学的孩子来说实在太困难了,所以你看不下去了,请你帮助小朋友回答所有的询问。

Input

第一行包含两个整数nm,表示小朋友的个数,以及接下来你要处理的操作数。

接下来的m行,每一行表示下面两种操作之一:

0 l r v : 表示秋实大哥给[l,r]这个区间内的小朋友每人v颗糖

1 x : 表示秋实大哥想知道第x个小朋友手里现在有几颗糖

1m,v1000001lrn1xn1n100000000

Output

对于每一个1 x操作,输出一个整数,表示第x个小朋友手里现在的糖果数目。

Sample input and output

Sample InputSample Output
3 4
0 1 3 1
1 2
0 2 3 3
1 3
1
4

解题报告

首先由于小朋友数目可以高达1e8,因此我们首先读入所有点,离散化..

之后上线段树还是上树状数组就随意了。。

我是采用的树状数组..这里我的离散化写的水,每次都要二分查位置(好吧,不要在意这些细节)

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <queue>
  6 #include <set>
  7 using namespace std;
  8 typedef long long ll;
  9 const int maxn = 2e5 + 100;
 10 int size;
 11 set<int>cv;
 12 int point[maxn];
 13 
 14 typedef struct oper
 15 {
 16 int l,r,v,ope;    
 17 oper(const int &ope, const int &l,const int &r,const int &v)
 18  {
 19       this->ope = ope , this->l = l , this->r = r , this->v = v;
 20  }
 21 };
 22 
 23 queue<oper>query;
 24 ll c[maxn];
 25 
 26 inline int lowbit(int cur)
 27 {
 28   return cur&(-cur);
 29 }
 30 
 31 void add(int l,ll v)
 32 {
 33   if (!l)
 34    return;
 35   while(l > 0)
 36    {
 37          c[l] += v;
 38          l -= lowbit(l);
 39    }
 40 }
 41 
 42 ll find(int x)
 43 {
 44    ll res = 0;
 45    while(x <= size)
 46     {
 47         res += c[x];
 48         x += lowbit(x);
 49     }
 50    return res;
 51 }
 52 
 53 
 54 int main(int argc,char *argv[])
 55 {
 56   memset(c,0,sizeof(c));
 57   int n,m;
 58   scanf("%d%d",&n,&m);
 59   size = 0;
 60   while(m--)
 61    {
 62          int ope;
 63          scanf("%d",&ope);
 64          if (ope & 1)
 65           {
 66                 int temp;
 67                 scanf("%d",&temp);
 68                 if (!cv.count(temp))
 69               {
 70                     cv.insert(temp);
 71                     point[++size] = temp;
 72            }
 73                 query.push(oper(ope,0,0,temp));
 74        }
 75       else
 76        {
 77              int l,r,v;
 78              scanf("%d%d%d",&l,&r,&v);
 79              if (!cv.count(l))
 80               {
 81                     cv.insert(l);
 82                     point[++size] = l;
 83            }
 84           if (!cv.count(r))
 85            {
 86                  cv.insert(r);
 87                  point[++size] = r;
 88            }
 89              query.push(oper(ope,l,r,v));
 90        }
 91    }
 92   sort(point+1,point+1+size);
 93   while(!query.empty())
 94    {
 95          oper ss = query.front();query.pop();
 96          if (ss.ope & 1)
 97           {
 98                 int pos = lower_bound(point+1,point+size,ss.v) - point;
 99                 printf("%lld
",find(pos));
100        }
101       else
102        {
103              ll v = (ll)ss.v;
104              int pos = lower_bound(point+1,point+size,ss.l) - point;
105              add(pos-1,-v);
106              pos = lower_bound(point+1,point+size,ss.r) - point;
107              add(pos,v);
108        }
109    }
110   return 0;
111 }
No Pain , No Gain.
原文地址:https://www.cnblogs.com/Xiper/p/4470202.html