刷题总结——系列维护(ssoi)

题目:

题解:

题解如上图,至于计算大于s的数字的数量和小于s数字的和用权值线段树或者树状数组维护就行了···注意离散化

另外发现cout和puts比printf快好多·····

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
const int N=1e6+5;
int b[N*2],cnt,len,que[N*2];
struct node 
{
  long long sum;
  int cnt;
}tree[N*2];
struct node2
{
  int op;
  int a,b;
}q[N*2];
int n,m;
char s[5];
inline void lsh()
{
  sort(b+1,b+cnt+1);
  len=unique(b+1,b+cnt+1)-b-1;
  for(int i=1;i<=m;i++) 
    q[i].b=lower_bound(b+1,b+len+1,q[i].b)-b;
}
inline int R()
{
  int i=1,f=0;
  char c;
  for(c=getchar();(c<'0'||c>'9');c=getchar());
  for(;c>='0'&&c<='9';c=getchar())
    f=(f<<3)+(f<<1)+c-'0';
  return f*i;
}
int lowbit(int i)
{
  return i&(-i);
}
void insert(int x)
{
  if(x==0)  return;
  for(int i=x;i<=len;i+=lowbit(i))
  {
    tree[i].cnt++;
    tree[i].sum=(long long)tree[i].sum+b[x];
  }
}
void Delete(int x)
{
  if(x==0)  return;
  for(int i=x;i<=len;i+=lowbit(i))
  {
    tree[i].cnt--;
    tree[i].sum=(long long)tree[i].sum-b[x];
  }
}
int calccnt(int x)
{
  if(x==0)  return 0;
  int temp=0;
  for(int i=x;i>=1;i-=lowbit(i))
    temp+=tree[i].cnt;
  return temp;
} 
long long calcsum(int x)
{
  if(x==0)  return 0;
  long long temp=0;
  for(int i=x;i>=1;i-=lowbit(i))
    temp+=tree[i].sum;
  return temp;
}
int main()
{
  //freopen("t3.in","r",stdin);
  //freopen("t3.out","w",stdout);
  n=R(),m=R();
  for(int i=1;i<=m;i++)
  {
    scanf("%s",s);
    if(s[0]=='U')
      q[i].op=1;
    else
     q[i].op=2;
    q[i].a=R(),q[i].b=R(); 
    b[++cnt]=q[i].b; 
  }
  lsh();
  for(int i=1;i<=m;i++)
  {
    if(q[i].op==1)
    {  
      if(que[q[i].a]!=0)
        Delete(que[q[i].a]);
      insert(q[i].b);  
      que[q[i].a]=q[i].b;
    }
    else
    {
      int cntt=calccnt(len)-calccnt(q[i].b-1);
      if(cntt>=q[i].a)  puts("Yes");
      else
      {  
        long long sum=calcsum(q[i].b-1);
        if(sum>=(long long)(q[i].a-cntt)*b[q[i].b])
          puts("Yes");
        else
          puts("No");
      }
    }
  }
  return 0;
}

 

posted @ 2017-07-26 16:18  AseanA  阅读(391)  评论(0编辑  收藏  举报