Number of columns of block entry # 2

This question they said no block to use a balanced tree, so it is with the violent dismantling block.

Do block some templates already dawned on the block here put it about two (with vector convenient than using the array md)

. 1  void  / * Modify operation * // * query * / ( int L, int R & lt ....., int Val) / * Modify elements * / 
2  {
 3      / * always the first modification (query) angle interval , is not the kind of monolithic block * / 
. 4      for ( int I = L; I <= (ID [L], n-) / * may be noted that the end of the section is taken so l ~ r min * / ; I ++ ) 
 5      {
 6          / * violence enumeration modification (query) * / 
. 7      } 
 . 8      IF ( / * L r and r are not put the same section where the angle interval enumeration modified (query) * / )
9      {
 10          / * Violence ~ * / 
11          / * but can be half or whatever can do it, it is certainly a change it * / 
12      }
 13      / * last enumerated on the entire piece of it * /  
14 }

This question is to maintain the elements within each block into a monotonous, so that the last time the query can come much faster with a query-half (lower_bound), while the rest is only violence, violence violent again.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 int block;
 5 int id[50009];
 6 int num[50009];
 7 int tag[10009];
 8 vector<int>v[10009];
 9 inline long long kd()
10 {
11     long long x=0,f=1;char ch=getchar();
12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13     while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
14     return x*f;
15 }
16 inline void reset(int now)
17 {
18     int r=min(id[now]*block,n);
19     v[id[now]].clear();/*清空*/
20     for(int i=(id[now]-1)*block+1;i<=r;i++)v[id[i]].push_back(num[i]);/*挼进去*/
21     sort(v[id[now]].begin(),v[id[now]].end());/*排个序*/
22 }
23 inline void add(int l,int r,int c)
24 {
25     int minn=min(r,id[l]*block);
26     for(int i=l;i<=minn;i++)num[i]+=c;/*暴力修改角区间*/
27      reset (l); / * comment with the following reset of * /
 28      IF (! ID [l] = ID [r]) / * If the l and r are not intervals of * /
 29      {
 30          for ( int I = (ID [R & lt] - . 1 ) * Block + . 1 ; I <= R & lt; I ++) NUM [I] + = C; / * same violent * /
 31 is          RESET (R & lt); / * after the angle interval for modification disrupted order, the reordering * /
 32      }
 33 is      for ( int I = ID [L] + . 1 ; I <= ID [R & lt] - . 1 ; I ++) tag [I] + = C; / * directly with the tag to modify the entire block segment * /
 34 is  }
 35  int Query ( int L, int R & lt, int C)
36  {
 37 [      int ANS = 0 ;
 38 is      int Minn = min (R & lt, ID [L] * Block);
 39      for ( int I = L; I <= Minn; I ++ )
 40          IF ([I] NUM + Tag [ID [I]] <C) ANS ++ ; / * violence enumeration angle interval * /
 41 is      IF (ID [L] =! ID [R & lt])
 42 is      {
 43 is          for ( int I = (ID [R & lt] - . 1 ) * Block + . 1 ; I <= R & lt; I ++ )
 44 is          {
 45              IF (NUM [I] + Tag [ID [I]] <C) ++ ANS ; / * * re-enumeration violence /
 46          }
 47     }
 48      for ( int I = ID [L] + . 1 ; I <= ID [R & lt] - . 1 ; I ++ )
 49      {
 50          int X = the C- Tag [I];
 51 is          ANS + = lower_bound (V [I] .begin (), V [I] .end (), X) - V [I] .begin (); / * monolithic direct binary search * /
 52 is      }
 53 is      return ANS;
 54 is  }
 55  int main ()
 56 is  {
 57 is      n-= KD ();
 58      for ( int I = . 1 ; I <= n-; I ++) NUM [I] = KD ();
 59      Block = ( int)sqrt(n);
60     for(int i=1;i<=n;i++)
61     {
62         id[i]=(i-1)/block+1;
63         v[id[i]].push_back(num[i]);
64     }
65     for(int i=1;i<=id[n];i++)
66         sort(v[i].begin(),v[i].end());
67     for(int i=1;i<=n;i++)
68     {
69         int opt=kd();
70         int l=kd(),r=kd(),c=kd();
71         if(opt==0)add(l,r,c);
72         if(opt==1)cout<<query(l,r,c*c)<<endl;
73     }
74     return 0;
75 }

 

Guess you like

Origin www.cnblogs.com/1129-tangqiyuan/p/11779879.html