51nod1394 Differences and problems

Source: Internet
Author: User

I will only use line tree to write ... Do not like tree arrays: Actually, it's not too slow? Then all kinds of * when forget Longlong has been wa ... Pills!

And I don't know how to use map discretization ... So, Sort+unique.

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) #define LL long long#define Lson l,mid,x<<1#define Rson mid+1,r,x<<1|1int Read () {int X=0;char c= GetChar (); while (!isdigit (c)) C=getchar () and while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int Nmax=1e5+5;const int INF=0X7F7F7F7F;LL sm[nmax<<3],col[nmax<<3];int A[nmax<<1],b[nmax <<1],ct[nmax<<1];struct node{int u,v;}; Node ns[nmax];void update (int p,int ad,int bd,int l,int r,int x) {if (l==r) {Col[x]+=ad,sm[x]+=bd;return;} int mid= (L+R) >>1;p<=mid?update (P,ad,bd,lson): Update (P,ad,bd,rson); sm[x]=sm[x<<1]+sm[x<<1|1 ];COL[X]=COL[X&LT;&LT;1]+COL[X&LT;&LT;1|1];} struct Nd{int u;ll v;nd (int u,ll v): U (U), V (v) {};nd () {};}; nd query (int tl,int tr,int l,int r,int x) {if (tl<=l&&tr>=r) return nD (Col[x],sm[x]), LL Tm=0;int tc=0,mid= (l+r) >>1;nd o;if (tl<=mid) o=query (Tl,tr,lson), tc+=o.u,tm+=o.v;if (tr >mid) O=query (Tl,tr,rson), tc+=o.u,tm+=o.v;return nd (TC,TM);} void print (int l,int r,int x) {printf ("%d%d%d\n", l,r,sm[x]), if (l==r) return, int mid= (l+r) >>1;print (Lson);p rint ( Rson);} int main () {int n=read (), M=read (), U,v,d,cnt=n;rep (i,1,n) A[i]=b[i]=read (), Rep (i,1,m) {ns[i].u=read (); if (ns[i].u!=3) Ns[i].v=read (), A[++CNT]=B[CNT]=NS[I].V;} Sort (b+1,b+cnt+1); Cnt=unique (b+1,b+cnt+1)-b-1;rep (i,1,n) A[i]=lower_bound (B+1,b+cnt+1,a[i])-b;rep (i,1,n) Update ( a[i],1,b[a[i]],1,cnt,1), Ct[a[i]]++;ll tm;nd o;tm=0;rep (j,2,cnt) {o=query (1,j-1,1,cnt,1); tm+= (LL) ct[j]* (LL) b[j]* O.U-O.V);} Rep (i,1,m) {u=ns[i].u;if (u==3) printf ("%lld\n", TM), else if (u==1) {v=lower_bound (B+1,B+CNT+1,NS[I].V)-b;ct[v]++; Update (v,1,ns[i].v,1,cnt,1), if (v==1) o=nd (0,0), Else O=query (1,v-1,1,cnt,1); tm+= (LL) b[v]*o.u-o.v+ (sm[1]-o.v-(LL) b [V]*ct[v])-(LL) (Col[1]-o.u-ct[v]) *b[v];} Else{v=lower_bound (B+1,B+CNT+1,NS[I].V)-b;if (!ct[V]) puts ("1"); Else{ct[v]--, update (v,-1,-ns[i].v,1,cnt,1), if (v==1) o=nd (0,0); else O=query (1,v-1,1,cnt,1); tm-= (LL) B[v]*o.u-o.v+ (sm[1]-o.v-(LL) b[v]*ct[v])-(LL) (Col[1]-o.u-ct[v]) *b[v];}} printf ("->_->\n");p rint (1,cnt,1);} return 0;}

  

1394 difference and problem base time limit: 1 seconds space limit: 131072 KB score: 80 Difficulty: 5-level algorithm topic collection attention

There is a multiset s (that is, the elements inside can have duplicates), and in the initial state there are n elements that do the following:

1. Add an element with a value of V to S. Input format is 1 V

2. Remove an element with a value of V inside S. Input format is 2 v

3. Ask for the sum of the absolute value of element 22 within S. Input format is 3

For examples,

Operation 3,|1-2|+|1-3|+|2-3|=4

After Operation 1 4, the number in the collection is 1 2 3 4

Operation 3,|1-2|+|1-3|+|2-3|+|1-4|+|2-4|+|3-4|=10

After Operation 2 2, the number in the collection is 1 3 4

Operation 3,|1-3|+|1-4|+|3-4|=6

Input
The first line enters two integers n,q indicates the number of initial elements and the number of operations in the collection. (1<=n,q<=100,000) The second line gives n integers a[0],a[1],a[2],..., a[n-1], which represents the elements in the initial collection. (0<=a[i]<=1,000,000,000) Next Q line, one operation per line. (0<=v<=1,000,000,000)
Output
For class 2nd operations, if an element with a value of V does not exist in the collection for deletion, output-1. For class 3rd operations, output the answer.
Input example
3 51 2 331 432 23
Output example
4106

51nod1394 Differences and problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.